通过MailKit失败发送电子邮件 - 连接尝试失败,因为连接的方在一段时间后没有正确响应

发布于 2025-02-07 16:11:43 字数 3181 浏览 1 评论 0原文

我已经陷入困境了很长时间了。 我的应用程序可以从我的本地和另一台服务器中发送电子邮件,但是有一台服务器总是会失败此错误:

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", "[email protected]"));
        message.To.Add(new MailboxAddress("Recipient Name", "[email protected]"));
        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,但仍然失败。

为了隔离服务器,我能够通过具有相似设置的PowerShell(同一服务器,端口和TLS)从有问题的服务器发送电子邮件。

我希望有人可以在这里阐明一些灯光,或者至少对这个问题给出一些新的视角。谢谢!

I've been stuck with this issue for a long time now.
My application can send email without problem from my local and another server but there's this one server where it always fails with this error:

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

Sample Code:

    public static void SendEmail(EmailCreateViewModel viewModel)
    {
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Sender Name", "[email protected]"));
        message.To.Add(new MailboxAddress("Recipient Name", "[email protected]"));
        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

It feels like I've tried everything and don't know what else to do and the exception itself doesn't say much. I've also tried turning off the firewall and windows defender but it still fails.

To isolate the server, I've been able to send an email from the problematic server via PowerShell with similar settings (same server, port and TLS).

I hope someone can shed some light here or at least give some fresh perspective on this issue. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

季末如歌 2025-02-14 16:11:43

最后,我们无法修复代码本身。经过多次试用和错误后,我们尝试使用精确代码从 .NET Core 控制台程序发送电子邮件。令我们惊讶的是,它奏效了。

我们最终做的是将旧的 .NET框架4.5.2服务转换为最新的 .NET 6 Worker Service 。在有问题的服务器中再次对其进行了测试,一切正常。

出于某种原因,我们的直觉是,.NET Framework的旧库被SMTP服务器(或沿着SMTP服务器的途中)视为不安全并拒绝连接。

In the end, we weren't able to fix the code itself. After multiple trial and errors, we tried sending the email from a .NET Core console program using the exact code. To our surprise, it worked.

What we ended up doing is convert the old .NET Framework 4.5.2 Service to the latest .NET 6 Worker Service. Tested it again in the problematic server and everything is working fine.

Our hunch is for some reason, the old libraries of .NET Framework was deemed unsecure by the SMTP server (or along the way to the SMTP server) and rejects the connection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文