首页 文章

Azure Web App无法从smtp.office365.com发送电子邮件

提问于
浏览
0

我正在尝试使用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 回答

相关问题