简体   繁体   中英

smtp.office365.com Failure sending mail. Unable to read data from the transport connection: net_io_connectionclosed

I have a support ticket web application, very old, done in ASP.NET webforms Until 5 October 2021 it has worked perfectly, then has started to miss, sometimes to send out emails.

In the last few days has started to miss sending out most of the emails.

I'm using office365.com as SMTP and POP3 server. POP3 has never given any issue. I'm using the same account for sending and for reading. The workload is very very low: I read the POP3 every 5 minutes, and I send out emails just to confirm we have taken in charge the request. And we are talking about 1 email every 1~2 hrs, therefore is not a heavy workload.

This is the code:


private static string sSMTP = "smtp.office365.com";
private static string sPOP3 = "outlook.office365.com";
private static string sEmailAddress = "sender-email@domain.com";
private static string sEmailAccount = "sender-email@domain.com";
private static string sEmailName = "ACME";
private static string sPassword = "SomePassword";

SendMailConfirm("test.user1@gmail.com", "this is a test", "" + DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss"), 1);
SendMailConfirm("test.user2@some-domain.com", "this is a test", "" + DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss"), 1);
SendMailConfirm("test.user2@another-domain.com", "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("no-reply@domain.com");
    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();
}

The same code works perfectly fine using another email provider, but not on SendGrid or Gmail.

Which could be the cause?

Is there any way to get a more talking message error from SMTP?

After several tries, I've found that the issue is in the System.Net.Mail object of the.Net framework.

I've also found that Microsoft strongly suggest to not use it:: https://docs.microsoft.com/en-gb/dotnet/api/system.net.mail.smtpclient?redirectedfrom=MSDN&view=netframework-4.8

We don't recommend that you use the SmtpClient class for new development because SmtpClient doesn't support many modern protocols. Use MailKit or other libraries instead. For more information, see SmtpClient shouldn't be used on GitHub.

After testing it also with SendGrid I've obtained the same behaviour.

Therefore the only solution is to move to MailKit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM