SmtpClient无法发送; 雷鸟可以

Sha*_*ica 9 c# email smtp sendmail smtpclient

这应该是非常常规的,对吧?

public static SmtpClient GetSmtpClient()
{
    var client = new SmtpClient
    {
        Host = _smtpServer,
        UseDefaultCredentials = false,
        Credentials = new NetworkCredential(_smtpUsername, _smtpPassword),
        Port = _smtpPort, // port 25
        EnableSsl = _smtpSsl // false
    };
    return client;
}
...
var m = new MailMessage 
{
    // contents of mail message
}
using (var server = GetSmtpClient())
{
    server.Send(m);
}
Run Code Online (Sandbox Code Playgroud)

但是server.Send()扔了一个SmtpException:

邮件发送失败.---> System.IO.IOException:无法从传输连接中读取数据:net_io_connectionclosed

我的凭证出了什么问题?SMTP服务器上的IP阻止?也许我的ISP阻止了对端口25的传出请求?我在与调试环境相同的机器上下载并安装了Thunderbird,并在那里设置了相同的SMTP凭据,并且它运行良好.

那么为什么它在Thunderbird中有效,而在我的代码中呢?

更新:按照@ tzachs的建议,我下载了Wireshark,发现交易进度是相同的,直到某一点.

雷鸟:

TCP 62  25 ? 50924 [SYN, ACK] Seq=0 Ack=1 Win=5840 Len=0 MSS=1360 SACK_PERM=1
SMTP    143 S: 220 smtp.somedomain.com ESMTP Sendmail 8.13.8/8.13.8; Sat, 5 Dec 2015 15:46:22 -0500
TCP 54  25 ? 50924 [ACK] Seq=90 Ack=18 Win=5840 Len=0
SMTP    289 S: 250 smtp.somedomain.com Hello my-computer.myisp.com [x.x.60.61], pleased to meet you | 250 ENHANCEDSTATUSCODES | 250 PIPELINING | 250 8BITMIME | 250 SIZE | 250 DSN | 250 ETRN | 250 AUTH LOGIN PLAIN | 250 DELIVERBY | 250 HELP
SMTP    82  S: 235 2.0.0 OK Authenticated
etc.
Run Code Online (Sandbox Code Playgroud)

SmtpClient:

TCP 62  25 ? 50889 [SYN, ACK] Seq=0 Ack=1 Win=5840 Len=0 MSS=1360 SACK_PERM=1
SMTP    143 S: 220 smtp.somedomain.com ESMTP Sendmail 8.13.8/8.13.8; Sat, 5 Dec 2015 15:45:08 -0500
TCP 54  25 ? 50889 [ACK] Seq=90 Ack=21 Win=5840 Len=0
SMTP    289 S: 250 smtp.somedomain.com Hello my-computer.myisp.com [x.x.60.61], pleased to meet you | 250 ENHANCEDSTATUSCODES | 250 PIPELINING | 250 8BITMIME | 250 SIZE | 250 DSN | 250 ETRN | 250 AUTH LOGIN PLAIN | 250 DELIVERBY | 250 HELP
TCP 54  25 ? 50889 [FIN, ACK] Seq=325 Ack=62 Win=5840 Len=0
TCP 54  25 ? 50889 [ACK] Seq=326 Ack=63 Win=5840 Len=0
Run Code Online (Sandbox Code Playgroud)

请注意,在SmtpClient跟踪中的"高兴见到你"之后,没有任何关于身份验证的信息.不是"接受",不是"失败" - 没有.只是终止连接.

这有什么亮点吗?

另一个更新: Smtp服务器正在运行SendMail,FWIW.该错误特定于此特定Smtp主机; 当我指向不同的服务器时,代码工作正常.

Jho*_*n P 2

我在使用不同的 SMTP 服务器时确实遇到过一些问题。根据我的经验,默认的 .net smtpclient API 有时不会告诉您到底发生了什么(公平地说,这可能是我们简化事情的副作用)。 过去对我有帮助的,特别是在故障排除阶段,是使用 .Net SMTP API,它允许我将其调试到 rcpt 命令并为我提供更丰富的异常信息。

为此,我建议您下载该项目的源代码并调试其 SMTP 示例: MailSystem.Net 有一个名为 ActiveUp.Net.Samples 的 Windows 窗体项目,只需构建并执行它,您就可以继续进行几分钟。

如果您更喜欢以编程方式使用它,它与 .net smtpclient 非常相似(并且它确实具有传递凭据的重载):

        ActiveUp.Net.Mail.Message message = new ActiveUp.Net.Mail.Message();

        // We assign the sender email
        message.From.Email = this.fromEmailTextbox.Text;

        // We assign the recipient email
        message.To.Add(this.toEmailTextbox.Text);

        // We assign the subject
        message.Subject = this.subjectTextbox.Text;

        // We assign the body text
        message.BodyText.Text = this.bodyTextTextbox.Text;

        // We send the email using the specified SMTP server
        this.AddLogEntry("Sending message.");

        try
        {
            SmtpClient.Send(message, this.smtpServerAddressTextbox.Text);

            this.AddLogEntry("Message sent successfully.");
        }
        catch (SmtpException ex)
        {
            this.AddLogEntry(string.Format("Smtp Error: {0}", ex.Message));
        }
        catch (Exception ex)
        {
            this.AddLogEntry(string.Format("Failed: {0}", ex.Message));
        }
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你。