我正在尝试使用Office365从Azure Web App发送电子邮件 . 稍后,我们将其移至Azure Web Job或Azure Functions .
以下示例代码适用于 local development machine ,以及使用 on-premise SMTP server at same port 587 的Azure . 但是,与本地计算机相同的凭据会从Azure中的 smtp.office365.com 引发异常 .
It seems like the problem lies between Azure and Office 365. 非常感谢任何帮助!
Sending email from an Azure App Service using an O365 SMTP server
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("To Email Address", "Benjamin"));
msg.From = new MailAddress("From Email Address", "You");
msg.Subject = "Azure Web App Email using smtp.office365.com";
msg.Body = "Test message using smtp.office365.com on Azure from a Web App";
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("O365 UID", "O365 PASS");
client.Port = 587;
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Send(msg); <-- Line 34
例外
System.Net.Mail.SmtpException:SMTP服务器需要安全连接或客户端未经过身份验证 . 服务器响应是:5.7.57 SMTP;客户端未经过身份验证,无法在MAIL FROM [XXXXX.namprd02.prod.outlook.com]期间在System.Net.Mail.MailCommand.Send上的System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode,String response)发送匿名邮件( System.Net.Mail.SmtpClient.Send上的System.Net.Mail.SmtpTransport.SendMail(MailAddress sender,MailAddressCollection recipients,String deliveryNotify,Boolean allowUnicode,SmtpFailedRecipientException&exception)中的SmtpConnection conn,Byte []命令,MailAddress,布尔allowUnicode)第34行(MailMessage消息)
1 回答
您必须修改expeditor域上的DNS记录(SPF) .
例如,如果expeditor是test@mydomain.com .
您必须验证“mydomain.com”的DNS TXT记录,它必须包含:
您可以通过以下网站查看DNS记录:https://www.whois.com.au/whois/dns.html
输入您的域并单击“执行实时DNS查找”按钮,将显示DNS记录 .
如果没有这个TXT,你必须修改它...
看这个链接:https://answers.microsoft.com/en-us/msoffice/forum/msoffice_outlook-mso_winother-mso_o365b/client-was-not-authenticated-to-send-anonymous/d405bcb0-f40c-42fa-b1b2-477597100123