如何在不发送消息的情况下通过 C# 测试 SMTP 是否启动并运行。
我当然可以尝试:
try{
// send email to "[email protected] /cdn-cgi/l/email-protection"
}
catch
{
// log "smtp is down"
}
必须有一种更简洁的方法来做到这一点。
你可以试试saying EHLO http://dpotter.net/Technical/2009/04/testing-an-smtp-server/到您的服务器并查看它是否响应250 OK
。当然,此测试并不能保证您稍后会成功发送邮件,但它是一个很好的指示。
这是一个示例:
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
}
}
}
}
}
这是代码列表 http://www.greenend.org.uk/rjk/2000/05/21/smtp-replies.html期待。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)