SmtpClient.Send 卡住了

wez*_*ten 1 c# smtp

string host = "gauntlet.asoshared.com", username= "a@a.test", pwd = "abc",
    from = "a@a.test", to = "b@b.test";
int port = 465;

var mm = new MailMessage(from, to);
mm.Body = "b1";
mm.Subject = "s1";
mm.IsBodyHtml=false;
using (var smtp=new SmtpClient(host, port)) {
    smtp.EnableSsl = true;
    smtp.Credentials = new NetworkCredential (username, pwd);
    smtp.Send(mm);
}
Run Code Online (Sandbox Code Playgroud)

这一直停留在smtp.Send. 大约 2 分钟后,我收到超时错误。使用 Outlook,它可以完美运行(使用我的真实凭据)。查看 Wireshark,在运行此代码时,我看到 TCP SYN、SYN-ACK、ACK,然后什么也没有。使用 Outlook 时,这些后跟一个TLSv1.2 Client Hello等。

尝试在代码开头添加以下几行:

ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.Expect100Continue=false;
Run Code Online (Sandbox Code Playgroud)

为什么 C# 卡住了,甚至不发送Client Hello?

当我使用不同的主机时它工作正常。

编辑:您可以通过按原样运行上面的代码来自己重现问题。您将收到超时错误而不是凭据错误错误,并且使用wireshark,您可以看到 C# 甚至不会尝试 TLS 连接。

S.S*_*ker 5

我认为问题是隐式 SSL。

System.Net.Mail 仅支持“显式”SSL,如下所述:

http://blogs.msdn.com/webdav_101/archive/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465.aspx

通常这是由惯例管辖的。在端口 25(正常情况)上运行的 SMTP 使用显式 SSL。在端口 465 上运行的 SMTPS 使用隐式 SSL。在端口 587 上运行的邮件提交使用显式 SSL。

25如果允许显式 SSL,您可以尝试端口。