smtp.office365.com 发送邮件失败。无法从传输连接读取数据:net_io_connectionclosed
smtp.office365.com Failure sending mail. Unable to read data from the transport connection: net_io_connectionclosed
我有一个支持票网络应用程序,非常旧,在 ASP.NET 网络表单中完成
直到 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 = "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();
}
相同的代码在使用其他电子邮件提供商时工作得很好,但在 SendGrid 或 Gmail 上却不行。
可能是什么原因?
有什么方法可以从 SMTP 获取更多的谈话消息错误吗?
经过多次尝试,我发现问题出在 .Net 框架的 System.Net.Mail 对象中。
我还发现微软强烈建议不要使用它!:
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.
在使用 SendGrid 对其进行测试后,我得到了相同的行为。
因此唯一的解决方案是移动到 MailKit
我修改了代码,用 MailKit 发送邮件,经过数百次尝试,从未失败。
我有一个支持票网络应用程序,非常旧,在 ASP.NET 网络表单中完成 直到 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 = "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();
}
相同的代码在使用其他电子邮件提供商时工作得很好,但在 SendGrid 或 Gmail 上却不行。
可能是什么原因?
有什么方法可以从 SMTP 获取更多的谈话消息错误吗?
经过多次尝试,我发现问题出在 .Net 框架的 System.Net.Mail 对象中。
我还发现微软强烈建议不要使用它!: 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.
在使用 SendGrid 对其进行测试后,我得到了相同的行为。
因此唯一的解决方案是移动到 MailKit
我修改了代码,用 MailKit 发送邮件,经过数百次尝试,从未失败。