smtp.office365.com 发送邮件失败。无法从传输连接读取数据:net_io_connectionclosed

问题描述 投票:0回答:2

我有一个支持票 Web 应用程序,非常老,在 ASP.NET webforms 中完成 直到 2021 年 10 月 5 日,它一直运行良好,然后开始错过,有时 发送电子邮件。

最近几天开始错过发送大部分电子邮件。

我使用 office365.com 作为 SMTP 和 POP3 服务器。 POP3 从未给出任何问题。 我使用同一个帐户发送和阅读。 工作量非常非常低:我每 5 分钟阅读一次 POP3,我发送电子邮件只是为了确认我们已经负责该请求。而且我们说的是每 1~2 小时发送一封电子邮件,因此工作量并不大。

这是代码:


private static string sSMTP = "smtp.office365.com";
private static string sPOP3 = "outlook.office365.com";
private static string sEmailAddress = "[email protected]";
private static string sEmailAccount = "[email protected]";
private static string sEmailName = "ACME";
private static string sPassword = "SomePassword";

SendMailConfirm("[email protected]", "this is a test", "" + DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss"), 1);
SendMailConfirm("[email protected]", "this is a test", "" + DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss"), 1);
SendMailConfirm("[email protected]", "this is a test", "" + DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss"), 1);


private  void SendMailConfirm(string sTo, string sSubj, string sBody, int iCallID)
{
    SmtpClient client = new SmtpClient(sSMTP);
    //authentication
    client.Credentials = new System.Net.NetworkCredential(sEmailAccount, sPassword);
    client.EnableSsl = true;
    client.Port = 587; //tried also 25 with same result.

    MailAddress from = new MailAddress(sEmailAddress, sEmailName);
    MailAddress to = new MailAddress(sTo);
    MailMessage message = new MailMessage(from, to);

    message.Subject = sSubj;
    message.ReplyTo = new MailAddress("[email protected]");
    message.Body = "Dear user your support ticket has been inserted.\r\n " +
        "Request ID: " + iCallID.ToString() + ".\r\n\r\n-----------------\r\n" + sBody;
    SendMessage(client, message);

}

private  void SendMessage(SmtpClient client, MailMessage message)
{ // here I've tried to add some delay and see what happen but I always get randomly, 90% of the time "Failure sending mail" as exception.
    try
    {
        client.Send(message);
        output.Text += "<br/>" + DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss") + " 1st message to " + message.To + " succesfully sent!!!!!!!!!!!!!! ";
    }
    catch (Exception ex)
    {
        output.Text += "<br/>" + DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss") + " 1st message to " + message.To + " " + ex.Message + "<hr/>"+ex.StackTrace+"<hr/>";
        output.Text += "<br/>" + DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss") + "  -> retry in 1500ms.";
        try
        {
            Thread.Sleep(1500);
            client.Send(message);
            output.Text += "<br/>" + DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss") + " 2nd message to " + message.To + " succesfully sent!!!!!!!!!! ";
        }
        catch (Exception ex2)
        {
            output.Text += "<br/>" + DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss") + " 2nd message to " + message.To + " " + ex2.Message;
            output.Text += "<br/>" + DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss") + "   --->  retry in 3000ms.";
            try
            {
                Thread.Sleep(3000);
                client.Send(message);
                output.Text += "<br/>" + DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss") + " 3rd message to " + message.To + " succesfully sent!!!! ";
            }
            catch (Exception ex3)
            {
                output.Text += "<br/>" + DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss") + " 3rd message to " + message.To + " " + ex3.Message;
            }
        }
    }
    message.Dispose();
}

相同的代码在使用其他电子邮件提供商时工作得很好,但在 SendGrid 或 Gmail 上却不行。

这可能是什么原因?

有什么方法可以从 SMTP 获取更多的谈话消息错误?

c# asp.net smtp office365
2个回答
1
投票

经过几次尝试,我发现问题出在.Net框架的System.Net.Mail对象上。

我还发现微软强烈建议不要使用它!: https://learn.microsoft.com/en-gb/dotnet/api/system.net.mail.smtpclient?redirectedfrom=MSDN&view=netframework-4.8

我们不建议您使用 SmtpClient 类进行新的开发,因为 SmtpClient 不支持许多现代协议。请改用 MailKit 或其他库。有关详细信息,请参阅不应在 GitHub 上使用 SmtpClient。

在使用 SendGrid 对其进行测试后,我获得了相同的行为。

因此唯一的解决方案是迁移到 MailKit

我更改了代码,用 MailKit 发送消息,经过数百次尝试,它从未失败过。


0
投票

尝试在方法的开头添加以下行,它对我有用:

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
© www.soinside.com 2019 - 2024. All rights reserved.