测试SMTP服务器通过C#运行 [英] Testing SMTP server is running via C#
本文介绍了测试SMTP服务器通过C#运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何测试SMTP启动并通过C#运行没有发送消息。
How can I test SMTP is up and running via C# without sending a message.
我当然可以尝试:
try{
// send email to "nonsense@example.com"
}
catch
{
// log "smtp is down"
}
有必须要做到这一点更整洁的方式。
There must be a more tidy way to do this.
推荐答案
您可以尝试说 EHLO
您的服务器,看看它是否与响应250 OK
。当然,本次测试没有向你保证,你一定会成功后发送邮件,但它是一个很好的迹象。
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.
这是一个示例:
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
}
}
}
}
}
和这里的codeS ,以期望列表
And here's the list of codes to expect.
这篇关于测试SMTP服务器通过C#运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文