使用带有 TLS 1.2 的 SmtpClient 将未经身份验证的邮件发送到 Microsoft 365

问题描述 投票:0回答:1

我的理解是,从 2021 年 1 月开始,Microsoft 将要求 TLS 1.2 使用 365 连接器发送邮件。 我有一个工作完美的连接器,可以中继来自我网络的公共 IP 的消息。多年来我一直在大型网络应用程序、文档扫描仪和其他本地应用程序中使用它。我使用 365 服务共享邮箱([电子邮件受保护])作为发件人,未使用身份验证。

我从未成功将其设置为与 TLS 配合使用,因此大多数流量未加密。我发现了很多类似的问题,但没有一个具有相同的配置,也绝对没有一个有可接受的答案(大多数答案只是说“不要使用 SSL”)。

我想开始修复我的 Web 应用程序上的 TLS,然后修复其余部分。这是一个 c# 中的 .Net 4.8 Web 表单项目。

web.config 文件包含以下设置:

<system.net>
    <mailSettings>
      <smtp deliveryMethod="Network">
        <network host="mydomain-com.mail.protection.outlook.com" port="25" enableSsl="true" defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>

在网页中,我是这样发送消息的:

using System.Net.Mail;
            using SmtpClient client = new SmtpClient();
            
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    
            MailMessage msg = new MailMessage
            {
                From = new MailAddress("[email protected]", "Tester"),
                IsBodyHtml = true,
                Subject = "Test message",
                Body = "Test message."
            };
    
            msg.To.Add("[email protected]");
            client.Send(msg);

现在是有趣的部分。这在我的客户端计算机(Windows 11 计算机)上运行得很好。邮件已正确传送,并且经检查,所有退回邮件均使用 TLS 1.2。

当我使用相同的 web.config 设置在 Windows 2016 生产服务器上移动相同的代码时,这不起作用。我收到错误:服务器不支持安全连接(在事件查看器中记录为事件 1309、SmtpException、堆栈跟踪:

 in System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
 in System.Net.Mail.SmtpClient.GetConnection()
 in System.Net.Mail.SmtpClient.Send(MailMessage message)

将端口更改为587不起作用。让服务器发送消息的唯一方法是将 web.config 部分设置为

enableSsl="true"
。这当然是不可接受的,因为一旦微软停止转发不安全的邮件,它就会停止工作。

防火墙允许流量。这肯定是 Windows Server 设置中的问题,但同一服务器中的其他地方使用了 TLS 1.2,所以我不明白可能是什么原因。

.net webforms office365 smtpclient .net-4.8
1个回答
0
投票

您不能将 .NET

SmtpClient
用于 SSL

我使用了这个库https://github.com/nilnull/AIM。我必须在 .NET core 中重建它,但不需要更改代码。

然后您可以:

var smtpClient = new MimeMailer(host) {
  Port = 465,
  User = ,
  Password = ,
  SslType = SslMode.Ssl,
  AuthenticationMode = AuthenticationType.Base64
};

并发送。

© www.soinside.com 2019 - 2024. All rights reserved.