为什么在ASP.NET应用程序中使用异常的SMTPClient发送邮件失败?

发布于 2025-01-25 00:40:40 字数 1365 浏览 2 评论 0原文

我正在尝试通过SmtpClient(“ SMTP.Office365.com”)发送电子邮件,并遇到问题。

考虑以下代码段:

var fromAddress = new MailAddress("[email protected]", "TestFrom");
var toAddress = new MailAddress("[email protected]", "TestSend");
string fromPassword = "SomePassword";

var smtp = new SmtpClient
{
    Host = "smtp.office365.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};

using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = "Hey There Did this Test Work?",
    Body = "Hey There Did this Test Work?",
    IsBodyHtml = true,
})

smtp.Send(message);

此代码从.NET控制台应用程序中正常工作。 我也可以使用相同的凭据,并通过PowerShell发送电子邮件。

当我尝试在ASP.NET应用程序中使用相同的代码段时,它会失败。 我已经尝试使用本地部署的应用程序和Azure尝试了此操作。 我得到相同的结果。

我得到以下例外:

失败发送邮件。无法从运输中读取数据 连接:net_io_connectionclosed。

有人告诉我过去在过去三个月之前的某个时候正常工作。 我在这里做错了吗?我应该使用其他配置设置吗? ASP.NET是否需要设置某些东西(本地和在Azure内部)才能工作?

谢谢

I am trying to send an email via SmtpClient ("smtp.office365.com") and am running into issues.

Consider the following code snippet:

var fromAddress = new MailAddress("[email protected]", "TestFrom");
var toAddress = new MailAddress("[email protected]", "TestSend");
string fromPassword = "SomePassword";

var smtp = new SmtpClient
{
    Host = "smtp.office365.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};

using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = "Hey There Did this Test Work?",
    Body = "Hey There Did this Test Work?",
    IsBodyHtml = true,
})

smtp.Send(message);

This code works correctly from a .Net console application.
I can also use the same credentials and send an email via PowerShell.

When I attempt to use the same code snippet in an Asp.Net application, it fails.
I've tried this with the application deployed locally and to Azure.
I get the same results.

I get the following exceptions:

Failure sending mail. Unable to read data from the transport
connection: net_io_connectionclosed.

I am told that used to work correctly at some point previous to the past 3 months.
Am I doing something wrong here? Are there additional configuration settings I should be using? Does Asp.Net require something to bet set up (locally and within Azure) in order for this to work?

Thx

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

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

发布评论

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

评论(1

旧人哭 2025-02-01 00:40:40

我们遇到了类似的发送电子邮件的问题,事实证明这是因为我们没有执行TLS 1.2。您可以使用以下代码来执行它:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

您只需要在应用程序中一次调用一次,因此application_start回调在global.asax中是一个可能提出的位置。

We had similar issues sending emails and it turned out that it was because we weren't enforcing TLS 1.2. You can enforce it with following code:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

You only need call it once in your application, so Application_Start callback in global.asax is one possible place to put it.

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