tags:

views:

722

answers:

2

Hi there, I am working on a project where I have to validate the given SMTP server i.e in a textbox user will provide the detail and then he will click on a test button. I want to check whether the server entered by the user is an Smtp Server or not?

Any Idea??

+4  A: 

Attempt to connect to the SMTP port, and ensure you get a line back from it that starts with "220 " and contains the letters "SMTP". A typical example response would be:

220 prod.monadic.cynic.net ESMTP Postfix (2.5.5)

Then be polite and send "QUIT\r\n" to hang up.

You can do some further testing, if you like, such as testing that the user can likely deliver messages. For this, you'd send a HELO command with your hostname (or any string, really), a MAIL FROM command using the user's e-mail address, and a RCPT TO:<[email protected]>. Most servers at that point will tell you if relaying is not allowed. (I'm assuming you're doing this from the computer from which you will later be sending mail.) So long as you QUIT after that, rather than issuing a DATA command and the message data, nothing will be sent.

Here's an example session, done from the shell using the "netcat" command, showing that my server exists, but will not relay mail for people from random IP addresses.

    $ nc prod.monadic.cynic.net. 25
    220 prod.monadic.cynic.net ESMTP Postfix (2.5.5)
    HELO cynic.net
    250 prod.monadic.cynic.net
    MAIL FROM:<[email protected]>
    250 2.1.0 Ok
    RCPT TO:<[email protected]>
    554 5.7.1 <[email protected]>: Relay access denied
    QUIT
    221 2.0.0 Bye
    $
Curt Sampson
As Curt already told, try a connection and check if you can open a port. Then take a look if you got a response, but if configured a server won't send you initially something. So to be sure first start with a 'HELO' or 'EHLO' command and check it's response. Any further commands you could try for checking are listed in RFC 821, 2821 and 5321.
Oliver
A server will indeed initially send you a 220 line as soon as you connect. I've added an example above to make this more clear.
Curt Sampson
Hi Curt this is really but I can follow the first part properly but I am not able to understand the synatx RCPT TO:<[email protected]> can you give some code in c# for this.
Vinay Pandey
All servers should send a 220 reply, you should wait for that to happen. In the spec, the first 3 characters of a line are the response code, characters after that are configurable and may not contain SMTP. Quite a lot of servers will wait an arbitrary time before sending that 220 reply as part of a spam filter, if you send the HELO or EHLO before receiving the 220 response, you stand a much greater chance of running into spam filtering.
skirmish
Vinay, everything between the two lines starting with a dollar sign is just network traffic. You need to send "RCPT TO:<[email protected]>" across a network connection. Play with the netcat (nc) command yourself and perhaps this will make more sense. Also, I strongly recommend going through a few chapters of Richard Stevens' _TCP/IP Illustrated Volume 1_; it will give you a much better understanding of how these sorts of network protocols work.
Curt Sampson
+1  A: 

You might want to improve on this quick code with proper exception handling and maybe also setting the timeouts - it takes about 15 seconds to fail if it can't connect but that might be a limitation of the TCP/IP handshaking.

And sending a QUIT command as Curt suggested would be nice.

private bool ValidSMTP(string hostName)
{
    bool valid = false;
    try
    {
        TcpClient smtpTest = new TcpClient();
        smtpTest.Connect(hostName, 25);
        if (smtpTest.Connected)
        {
            NetworkStream ns = smtpTest.GetStream();
            StreamReader sr = new StreamReader(ns);
            if (sr.ReadLine().Contains("220"))
            {
                valid = true;
            }
            smtpTest.Close();
        }
    }
    catch
    {

    }
    return valid;
}
Bing
If you don't want to wait this much, set smtpTest.ReceiveTimeout = 500;
ulrichb