none
How to Find SMTP Mail Server Name and Port

    Question

  • Hi
    There
    How to Find SMTP Mail Server Name and Port Number 
    if i have the ip of server in C# asp.net

    thanks in advance

    SALIL NAYAK
    Tuesday, October 06, 2009 3:34 PM

Answers

  • You may also choose to implement a 3rd party port scanner which can be launched as a Process. After you use the 3rd party scanner you can analyze the subset of available ports by querying each with a helo command to verify SMTP existence.

    Be forwarned that this activity is often viewed as hacking and may get you banned.  As far as legality, you should consult a lawyer.
    • Proposed as answer by Harry Zhu Monday, October 12, 2009 3:14 AM
    • Marked as answer by Harry Zhu Tuesday, October 13, 2009 1:59 AM
    Tuesday, October 06, 2009 4:46 PM
  • On top of that in today's world of SPAM, you are blocked from sending or relaying of an SMTP server unless your IP Address is authorized.  You might not even get a valid HELO response if you aren't authorized.  Also, as Brian said that an advanced security system would likely lock you once you started a port scan.  What are you trying to accomplish?  Not to be a cynic but if I wanted you relaying mail of my server I would give you this the host address and port, this sound like something a botnet would do to send out spam.
    • Proposed as answer by Harry Zhu Monday, October 12, 2009 3:14 AM
    • Marked as answer by Harry Zhu Tuesday, October 13, 2009 1:58 AM
    Wednesday, October 07, 2009 1:23 PM

All replies

  • Hostname is easy .  Port not so much...  Without checking every Port for a valid SMTP response I think you are pretty much out of luck on that.
    Tuesday, October 06, 2009 4:07 PM
  • name means SMTP or MAIL  what are the basic port no have to be check
    Tuesday, October 06, 2009 4:26 PM
  • By using the class below you can get the mx record of a domain.

    After that you can try port 25 first and then other ports.

        public class Mx
        {
            public Mx()
            {
            }
            [DllImport("dnsapi", EntryPoint = "DnsQuery_W", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
            private static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref string pszName, QueryTypes wType, QueryOptions options, int aipServers, ref IntPtr ppQueryResults, int pReserved);
    
            [DllImport("dnsapi", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType);
    
            public static string[] GetMXRecords(string domain)
            {
    
                IntPtr ptr1 = IntPtr.Zero;
                IntPtr ptr2 = IntPtr.Zero;
                MXRecord recMx;
                if (Environment.OSVersion.Platform != PlatformID.Win32NT)
                {
                    throw new NotSupportedException();
                }
                ArrayList list1 = new ArrayList();
                int num1 = Mx.DnsQuery(ref domain, QueryTypes.DNS_TYPE_MX, QueryOptions.DNS_QUERY_BYPASS_CACHE, 0, ref ptr1, 0);
                if (num1 != 0)
                {
                    throw new Win32Exception(num1);
                }
                for (ptr2 = ptr1; !ptr2.Equals(IntPtr.Zero); ptr2 = recMx.pNext)
                {
                    recMx = (MXRecord)Marshal.PtrToStructure(ptr2, typeof(MXRecord));
                    if (recMx.wType == 15)
                    {
                        string text1 = Marshal.PtrToStringAuto(recMx.pNameExchange);
                        list1.Add(text1);
                    }
                }
                Mx.DnsRecordListFree(ptr2, 0);
                return (string[])list1.ToArray(typeof(string));
            }
    
            private enum QueryOptions
            {
                DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE = 1,
                DNS_QUERY_BYPASS_CACHE = 8,
                DNS_QUERY_DONT_RESET_TTL_VALUES = 0x100000,
                DNS_QUERY_NO_HOSTS_FILE = 0x40,
                DNS_QUERY_NO_LOCAL_NAME = 0x20,
                DNS_QUERY_NO_NETBT = 0x80,
                DNS_QUERY_NO_RECURSION = 4,
                DNS_QUERY_NO_WIRE_QUERY = 0x10,
                DNS_QUERY_RESERVED = -16777216,
                DNS_QUERY_RETURN_MESSAGE = 0x200,
                DNS_QUERY_STANDARD = 0,
                DNS_QUERY_TREAT_AS_FQDN = 0x1000,
                DNS_QUERY_USE_TCP_ONLY = 2,
                DNS_QUERY_WIRE_ONLY = 0x100
            }
    
            private enum QueryTypes
            {
                DNS_TYPE_MX = 15
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct MXRecord
            {
                public IntPtr pNext;
                public string pName;
                public short wType;
                public short wDataLength;
                public int flags;
                public int dwTtl;
                public int dwReserved;
                public IntPtr pNameExchange;
                public short wPreference;
                public short Pad;
            }
        }
    Tuesday, October 06, 2009 4:42 PM
  • You may also choose to implement a 3rd party port scanner which can be launched as a Process. After you use the 3rd party scanner you can analyze the subset of available ports by querying each with a helo command to verify SMTP existence.

    Be forwarned that this activity is often viewed as hacking and may get you banned.  As far as legality, you should consult a lawyer.
    • Proposed as answer by Harry Zhu Monday, October 12, 2009 3:14 AM
    • Marked as answer by Harry Zhu Tuesday, October 13, 2009 1:59 AM
    Tuesday, October 06, 2009 4:46 PM
  • howto get port no
    Tuesday, October 06, 2009 5:23 PM
  • Commonly used smtp port is 25.

    As Brian mentioned using an smtp server unauthorized may cause you some legal problems.. Sending helo command is a good idea. You can use this method to send helo command and check the response.

    foreach (string str in Mx.GetMXRecords(domainName))
                {
                    TcpClient insTcpClient=null;
                        //MessageBox.Show(str);
                        insTcpClient = new TcpClient(str, 25);
                        NetworkStream ns = insTcpClient.GetStream();

                        byte[] recievedMessage = new byte[insTcpClient.ReceiveBufferSize];
                        ns.Read(recievedMessage, 0, insTcpClient.ReceiveBufferSize);
                        //MessageBox.Show(Encoding.ASCII.GetString(recievedMessage));

                        string messageToSend = "HELO\r\n";
                        byte[] buffer = System.Text.Encoding.GetEncoding(1254).GetBytes(messageToSend);
                        ns.Write(buffer, 0, buffer.Length);

                        recievedMessage = new byte[insTcpClient.ReceiveBufferSize];
                        ns.Read(recievedMessage, 0, insTcpClient.ReceiveBufferSize);
                        //MessageBox.Show(Encoding.ASCII.GetString(recievedMessage));

                       insTcpClient.GetStream().Close();
                        insTcpClient.Close();
                    }

                }

    Tuesday, October 06, 2009 5:29 PM
  • As Brian mentioned using an smtp server unauthorized may cause you some legal problems.. Sending helo command is a good idea.

    That is certainly true, but in this case I was referring to the fact that advanced firewalls watch for port scanning-like activity and will report such activity to the network administrator along with your IP address.
    Good coding involves knowing one's logical limits and expanding them as necessary.
    Tuesday, October 06, 2009 5:40 PM
  • On top of that in today's world of SPAM, you are blocked from sending or relaying of an SMTP server unless your IP Address is authorized.  You might not even get a valid HELO response if you aren't authorized.  Also, as Brian said that an advanced security system would likely lock you once you started a port scan.  What are you trying to accomplish?  Not to be a cynic but if I wanted you relaying mail of my server I would give you this the host address and port, this sound like something a botnet would do to send out spam.
    • Proposed as answer by Harry Zhu Monday, October 12, 2009 3:14 AM
    • Marked as answer by Harry Zhu Tuesday, October 13, 2009 1:58 AM
    Wednesday, October 07, 2009 1:23 PM