Testing Smtp Server Is Running via C#

Testing SMTP server is running via C#

You can try saying EHLO to your server and see if it responds with 250 OK. Of course this test doesn't guarantee you that you will succeed sending the mail later, but it is a good indication.

And here's a sample:

class Program
{
static void Main(string[] args)
{
using (var client = new TcpClient())
{
var server = "smtp.gmail.com";
var port = 465;
client.Connect(server, port);
// As GMail requires SSL we should use SslStream
// If your SMTP server doesn't support SSL you can
// work directly with the underlying stream
using (var stream = client.GetStream())
using (var sslStream = new SslStream(stream))
{
sslStream.AuthenticateAsClient(server);
using (var writer = new StreamWriter(sslStream))
using (var reader = new StreamReader(sslStream))
{
writer.WriteLine("EHLO " + server);
writer.Flush();
Console.WriteLine(reader.ReadLine());
// GMail responds with: 220 mx.google.com ESMTP
}
}
}
}
}

And here's the list of codes to expect.

Testing the availability of an smtp mail server using c#

Yes, ping is a good start, but to determine if the SMTP gateway is available, issue the HELO command through the SMTP port.

I'll try to find a good example.

Edit:

You're in luck. Here's a complete TELNET client written in C#. https://www.nuget.org/packages/Telnet

Can I test SmtpClient before calling client.Send()?

I think this is a case where exception handling would be the preferred solution. You really don't know that it will work until you try, and failure is an exception.

Edit:

You'll want to handle SmtpException. This has a StatusCode property, which is an enum that will tell you why the Send() failed.

How to validate SMTP server

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:<example@example.com>. 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:<cjs@cynic.net>
250 2.1.0 Ok
RCPT TO:<example@example.com>
554 5.7.1 <example@example.com>: Relay access denied
QUIT
221 2.0.0 Bye
$


Related Topics



Leave a reply



Submit