c# - MailKit C# SmtpClient.Connect() 到 Office 365 生成异常 : "An existing connection was forcibly closed by the remote host"

标签 c# office365 mailkit

我在通过 Office 365 SMTP 和 MailKit 发送电子邮件时遇到问题。我得到的异常(exception)是:

Unhandled Exception: System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host

https://github.com/jstedfast/MailKit

代码:

var smtpClient = new SmtpClient();

smtpClient.Connect("smtp.office365.com", 587, true);

Microsoft Office 365 设置应该正确:

https://support.office.com/en-us/article/POP-and-IMAP-settings-for-Outlook-Office-365-for-business-7fc677eb-2491-4cbc-8153-8e7113525f6c

奇怪的是,如果我使用以下一切正常,即使 Office 365 说需要 SSL。

smtpClient.Connect("smtp.office365.com", 587, false);

最佳答案

发布这个问题后又出现了另一个错误,这让我找到了答案:

Handshake failed due to an unexpected packet format

解决方案是像这样连接到 Office 365:

smtpClient.Connect("smtp.office365.com", 587, SecureSocketOptions.StartTls);

关于c# - MailKit C# SmtpClient.Connect() 到 Office 365 生成异常 : "An existing connection was forcibly closed by the remote host",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41872812/

相关文章:

c# - 上传到天蓝色存储返回空文件

c# - 将 C# DateTime 对象转换为 libpcap 捕获文件时间戳

c# - 如何从 Microsoft Delve 获取用户的技能

c# - Office 365 REST API

c# - MailKit 连接没有反应

c - MailKit 将 HtmlBody 以字符串形式写入数据库

email - 使用 SMTP 时,X-Priority 与 Priority header 有什么区别?

c# - 从 Windows C# Canon SDK 与 PTP 或 MTP 捕捉图片

c# - 为什么要将 2 类的引用放入 1 类的对象中?

java - 如何使用 java 和 oauth 2.0 在 Azure AD 中注册应用程序来阅读我的 Outlook 邮件