从 docker 连接到 office365 SMTP 时出现异常

Posted

技术标签:

【中文标题】从 docker 连接到 office365 SMTP 时出现异常【英文标题】:Exception when connect to office365 SMTP from a docker 【发布时间】:2020-06-18 17:41:34 【问题描述】:

我正在使用 MailBee SMTP v11.2 发送电子邮件。我在尝试从 Docker 上托管的 API 连接到 office365 smtp 服务时遇到问题。我在 Centos 7 服务器上运行 docker。相同的代码在 IIS、窗口服务器上托管的 API 中工作。 我的配置: 服务器:smtp.office365.com 安全性:StartTLS,端口 587 异常:发生 SocketException。 InnerException 消息如下:对等方重置连接。

我尝试使用 StartTLS,端口 587 连接到 Yahoo SMTP。它工作正常,所以防火墙没有问题。 我还尝试使用错误的密码连接到office365,它返回了身份验证错误的异常,所以我认为它仍然成功连接到了office365,但之后发生了一些错误。 我坚持了几天。

谁有解决办法?

【问题讨论】:

无代码,无日志文件内容。请同时提供。 【参考方案1】:

对不起,我缺乏信息, 我的演示代码

        var smtpServer = new SmtpServer()
        
            Name = "smtp.office365.com",
            AccountName = "my account",
            Password = "my password",
            Port = 587
        ;
        smtpServer.SslMode = MailBee.Security.SslStartupMode.UseStartTls;
        smtpServer.SslProtocol = MailBee.Security.SecurityProtocol.Auto;
        smtpServer.AuthMethods = MailBee.AuthenticationMethods.Auto;

        var smtp = new Smtp("key");
        smtp.SmtpServers.Add(smtpServer);
        smtp.Message.Builder.htmlToPlainMode = MailBee.Mime.HtmlToPlainAutoConvert.IfHtml;
        smtp.Message.XMailer = "Testing tool";
        smtp.Message.Charset = "utf8";

        smtp.Connect();
        smtp.Hello();
        smtp.Login();

Mailbee 日志文件:

[01:54:37.88] [INFO] Assembly version: 11.2.0 build 590 for .NET Core 2.0. 
[01:54:37.88] [INFO] Will resolve host "smtp.office365.com".
[01:54:37.94] [INFO] Host "smtp.office365.com" resolved to IP address(es) 40.100.54.226, 40.100.54.18, 52.98.85.194, 52.98.77.98, 2603:1046:c02:1820::2, 2603:1046:201:16::2, 2603:1046:202::2, 2603:1046:202:1::2.
[01:54:37.94] [INFO] Will connect to host "smtp.office365.com" on port 587.
[01:54:37.98] [INFO] Socket connected to IP address 40.100.54.226 on port 587.
[01:54:38.01] [RECV] 220 HK2PR02CA0150.outlook.office365.com Microsoft ESMTP MAIL Service ready at Sat, 7 Mar 2020 01:53:58 +0000\r\n
[01:54:38.02] [INFO] Connected to mail service at host "smtp.office365.com" on port 587 and ready.
[01:54:38.02] [INFO] Will send Hello command (HELO or EHLO).
[01:54:38.02] [SEND] EHLO [172.17.0.28]\r\n
[01:54:38.05] [RECV] 250-HK2PR02CA0150.outlook.office365.com Hello [103.53.91.136]\r\n250-SIZE 157286400\r\n250-PIPELINING\r\n250-DSN\r\n250-ENHANCEDSTATUSCODES\r\n250-STARTTLS\r\n250-8BITMIME\r\n250-BINARYMIME\r\n250-CHUNKING\r\n250 SMTPUTF8\r\n
[01:54:38.06] [INFO] SMTP Hello completed.
[01:54:38.06] [INFO] Notify server that we are ready to start TLS/SSL negotiation.
[01:54:38.06] [SEND] STARTTLS\r\n
[01:54:38.09] [RECV] 220 2.0.0 SMTP server ready\r\n
[01:54:38.11] [INFO] Will start TLS/SSL negotiation sequence.
[01:54:38.36] [INFO] TLS/SSL negotiation completed.
[01:54:38.36] [INFO] Will send Hello command (HELO or EHLO).
[01:54:38.36] [SEND] EHLO [172.17.0.28]\r\n
[01:54:38.39] [RECV] 250-HK2PR02CA0150.outlook.office365.com Hello [103.53.91.136]\r\n250-SIZE 157286400\r\n250-PIPELINING\r\n250-DSN\r\n250-ENHANCEDSTATUSCODES\r\n250-AUTH LOGIN XOAUTH2\r\n250-8BITMIME\r\n250-BINARYMIME\r\n250-CHUNKING\r\n250 SMTPUTF8\r\n
[01:54:38.39] [INFO] SMTP Hello completed.
[01:54:38.40] [INFO] Will login as "my Office365 account".
[01:54:38.40] [INFO] Will try SASL LOGIN authentication method.
[01:54:38.41] [SEND] AUTH LOGIN ********\r\n
[01:54:38.44] [RECV] 334 UGFzc3dvcmQ6\r\n
[01:54:38.44] [SEND] ********\r\n
[01:54:40.66] [RECV] 235 2.7.0 Authentication successful\r\n
[01:54:40.66] [INFO] Logged in as "my Office365 account".
[01:54:40.67] [SEND] QUIT\r\n
[01:54:40.77] [RECV] 221 2.0.0 Service closing transmission channel\r\n
[01:54:40.91] [INFO] Error: SocketException occurred. InnerException message follows: Connection reset by peer
[01:54:40.91] [INFO] Will disconnect from host "smtp.office365.com".
[01:54:40.91] [INFO] Disconnected from host "smtp.office365.com".

【讨论】:

您可以更新到最新版本的MailBee,看看是否有帮助。

以上是关于从 docker 连接到 office365 SMTP 时出现异常的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 队列,连接到 tcp://smtp.office365.com:587 超时

为啥 Office 365 连接到 office 按钮不再适用于新的 Microsoft Teams 组?

通过 EWS API 连接到 Office 365

将 ucma 3.0 连接到 Office 365

Outlook 2013连接到Office 365时缓存模式与联机模式下的流量问题

使用 O365 帐户连接到 Azure VM 中的 SMTP 中继失败