Home > Software design >  smtp.office365.com Failure sending mail. Unable to read data from the transport connection: net_io_c
smtp.office365.com Failure sending mail. Unable to read data from the transport connection: net_io_c

Time:02-08

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 = "[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();
}

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?

CodePudding user response:

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

I've made the code changes, to send the message out with MailKit, and after hundreds of tries, it has never failed.

  •  Tags:  
  • Related