当前位置: 首页 > 知识库问答 >
问题:

SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:5.7.57 SMTP;Office 365错误

乐修远
2023-03-14

尝试通过 MVC 5 C# 发送电子邮件。此新创建的电子邮件地址位于 Office 365 服务器上。在线尝试了许多解决方案,但无济于事,我收到以下错误消息:SMTP 服务器需要安全连接或客户端未经过身份验证。服务器响应为:5.7.57 SMTP;客户端未经过身份验证以在 MAIL FROM [LO3P265CA0018.GBRP265.PROD.OUTLOOK.COM] 期间发送匿名邮件。我的代码如下:

  public void ConcernConfirmEmail(Appointments a, EmailConfig ec)
        {
            Dictionary<string, string> tokens = new Dictionary<string, string>();
            tokens.Add("Name", a.sirenDetail.FirstName);
            tokens.Add("Time", a.start.ToString("HH:mm"));
            tokens.Add("Date", a.start.ToString("dd/MM/yyyy"));
            tokens.Add("Location", a.site.SiteDescription);

            using (SmtpClient client = new SmtpClient()
            {
                Host = "smtp.office365.com",
                Port = 587,
                UseDefaultCredentials = false,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                Credentials = new NetworkCredential(ec.EmailUser, ec.EmailPassword), 
                TargetName = "STARTTLS/smtp.office365.com",
                EnableSsl = true
            })
            {
                MailMessage message = new MailMessage()
                {
                    From = new MailAddress("emailadress@myorg.net"),
                    Subject = "Subject",
                    Sender = new MailAddress("emailadress214@myorg.net", "password"),
                    Body = PopulateTemplate(tokens, GetTemplate("ConfirmTemplate.html")),
                    IsBodyHtml = true,
                    BodyEncoding = System.Text.Encoding.UTF8,
                    SubjectEncoding = System.Text.Encoding.UTF8,
                };
                message.To.Add(a.sirenDetail.EmailAddress.ToString());
                client.Send(message);
            }

        }

共有1个答案

洪富
2023-03-14

根据微软官方文档,SmtpClass已经过时了一段时间,微软鼓励开发人员使用新的开放souce Smtp实现,如MailKit。

使用带有用户名和密码身份验证的MailKit时,必须将身份验证机制设置为使用NTLM。

下面是一个工作示例:

 public async Task Send(string emailTo, string subject, MimeMessage mimeMessage, MessagePriority messagePriority = MessagePriority.Urgent)
        {
            MimeMessage mailMessage = mimeMessage;
            mailMessage.Subject = subject;
            mailMessage.Priority = messagePriority;

            if (emailTo.Contains(';'))
            {
                foreach (var address in emailTo.Split(';'))
                {
                    mailMessage.To.Add(new MailboxAddress("", address));
                }
            }
            else
            {
                mailMessage.To.Add(new MailboxAddress("", emailTo));
            }

            mailMessage.From.Add(new MailboxAddress("Sender", _smtpCredentials.SenderAddress));

            using var smtpClient = new SmtpClient
            {
                SslProtocols = SslProtocols.Tls,
                CheckCertificateRevocation = false,
                ServerCertificateValidationCallback = (s, c, h, e) => true,
            };

            await smtpClient.ConnectAsync(_smtpCredentials.Server, _smtpCredentials.Port, SecureSocketOptions.StartTlsWhenAvailable);
            await smtpClient.AuthenticateAsync(new SaslMechanismNtlm(new NetworkCredential(_smtpCredentials.User, _smtpCredentials.Password)));

            try
            {
                await smtpClient.SendAsync(mailMessage);
            }
            catch (Exception ex)
            {

            }
            finally
            {
                if (smtpClient.IsConnected) await smtpClient.DisconnectAsync(true);
            }
        }

这里最重要的一行是

await smtpClient.AuthenticateAsync(new SaslMechanismNtlm(new NetworkCredential(_smtpCredentials.User, _smtpCredentials.Password)));

也就是将NTLM设置为客户端使用的身份验证机制。

如果您现在无法更改Smtp库,您可以尝试将您的代码更改为如下所示,就像在旧服务中使用的那样,并且工作正常:

using (var smtpClient = new SmtpClient(smtpServer)
                {
                    UseDefaultCredentials = false,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    Credentials = new NetworkCredential(user, password),
                    EnableSsl = false,
                    Port = 587
                })

请注意,更改只是在设置为false的EnableSsl上进行,而不需要设置TargetName属性。

希望这有所帮助

 类似资料: