[英]Error trying to use MailKit to send Email "A connection attempt failed because the connected party did not properly respond after a period of time"
[英]Sending of email via MailKit fails - A connection attempt failed because the connected party did not properly respond after a period of time
我已经被这个问题困扰了很长时间了。 我的应用程序可以从我的本地服务器和另一台服务器毫无问题地发送电子邮件,但是有一台服务器总是失败并出现以下错误:
System.IO.IOException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
示例代码:
public static void SendEmail(EmailCreateViewModel viewModel)
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Sender Name", "test@senderemail.com"));
message.To.Add(new MailboxAddress("Recipient Name", "test@recipientemail.com"));
message.Subject = viewModel.Subject;
message.Body = new TextPart("plain") { Text = viewModel.TextBody };
using (var client = new SmtpClient(new ProtocolLogger("C:\\smtp.log")))
{
try
{
if(viewModel.BypassServerCertificateValidationCallback)
client.ServerCertificateValidationCallback = (sender, certificate, certChainType, errors) => true;
client.SslProtocols = System.Security.Authentication.SslProtocols.Tls | System.Security.Authentication.SslProtocols.Tls11 | System.Security.Authentication.SslProtocols.Tls12;
client.Connect("smtp.office365.com", 587, SecureSocketOptions.StartTls);
client.Authenticate(viewModel.User, viewModel.Password);
client.Send(message);
client.Disconnect(true);
}
catch (Exception ep)
{
Log("failed to send email with the following error:");
Log(ep.ToString());
}
}
}
smtp.log:
Connected to smtp://smtp.office365.com:587/?starttls=always
S: 220 SI2PR02CA0002.outlook.office365.com Microsoft ESMTP MAIL Service ready at Thu, 16 Jun 2022 03:02:14 +0000
C: EHLO <Insert Server Name>
S: 250-SI2PR02CA0002.outlook.office365.com Hello [<Insert Server IP>]
S: 250-SIZE 157286400
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-STARTTLS
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250 SMTPUTF8
C: STARTTLS
S: 220 2.0.0 SMTP server ready
C: EHLO <Insert Server Name>
S: 250-SI2PR02CA0002.outlook.office365.com Hello [<Insert Server IP>]
S: 250-SIZE 157286400
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-AUTH LOGIN XOAUTH2
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250 SMTPUTF8
C: AUTH LOGIN
感觉就像我什么都试过了,不知道还能做什么,异常本身并没有说太多。 我也试过关闭防火墙和 Windows Defender,但仍然失败。
为了隔离服务器,我已经能够通过具有类似设置(相同的服务器、端口和 TLS)的 PowerShell 从有问题的服务器发送一封电子邮件。
我希望有人可以在这里阐明一些观点,或者至少对这个问题提出一些新的看法。 谢谢!
最后,我们无法修复代码本身。 经过多次试验和错误,我们尝试使用确切的代码从.NET Core控制台程序发送电子邮件。 令我们惊讶的是,它奏效了。
我们最终要做的是将旧的.NET Framework 4.5.2 Service转换为最新的.NET 6 Worker Service 。 在有问题的服务器中再次对其进行了测试,一切正常。
我们的预感是出于某种原因,.NET Framework 的旧库被 SMTP 服务器(或在通往 SMTP 服务器的途中)视为不安全并拒绝连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.