【问题标题】:Cannot send email using SmtpClient in ASP.NET无法在 ASP.NET 中使用 SmtpClient 发送电子邮件
【发布时间】:2020-07-08 06:39:20
【问题描述】:

我想在 ASP.NET MVC 网站中发送电子邮件,但它不起作用。

我认为问题出在此异常

'client.ServicePoint.Address' 引发了类型为 System.Uri {System.NotSupportedException} 的类型为“System.NotSupportedException”的异常。

代码之后

var client = new SmtpClient("smtp.office365.com", 25)

在此文档https://docs.microsoft.com/en-us/dotnet/api/system.net.servicepoint.address?view=netcore-3.1 中,它说异常是由于“ServicePoint 处于主机模式”。我不知道。

谁能解释一下并告诉我如何解决?

谢谢,

【问题讨论】:

  • 真正的问题是我使用的端口号不正确......应该使用587......

标签: asp.net-mvc smtpclient servicepoint


【解决方案1】:

在 .NET 中,SMTP 设置在 app.config 和 web.config 文件中有自己的配置部分(在 system.net 元素下方):

<mailSettings>
    <smtp deliveryMethod="Network">
      <network host="smtp.office365.com" port="25" enableSsl="true" />
    </smtp>
</mailSettings>

您可以阅读有关 SMTP 的更多信息here

社区中的这个answer 可能会对您有所帮助。

更新:

端口应该是 587

【讨论】:

  • 谢谢,问题是我使用的端口号不正确......
  • 完美!我会用你的解决方案更新这个答案:>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-11
  • 2010-11-21
  • 1970-01-01
  • 2020-03-20
  • 1970-01-01
  • 2013-12-04
  • 2015-02-26
相关资源
最近更新 更多