[英]Getting an error when trying send an email using SmtpClient in asp.net core application
I am trying to send an email from asp.net core application but I keep getting an error that reads我正在尝试从 asp.net 核心应用程序发送 email 但我不断收到错误消息
SmtpException: The SMTP server requires a secure connection or the client was not authenticated.
SmtpException:SMTP 服务器需要安全连接或客户端未通过身份验证。 The server response was: 5.7.57 Client not authenticated to send mail.
服务器响应为:5.7.57 客户端未通过身份验证发送邮件。 [JN2P275CA0038.ZAFP275.PROD.OUTLOOK.COM]
[JN2P275CA0038.ZAFP275.PROD.OUTLOOK.COM]
I have also tried checking firewall rules and increasing timeout and that has not helped so I am not sure what is wrong我也尝试过检查防火墙规则并增加超时,但没有帮助,所以我不确定出了什么问题
This is my code to send email这是我发送 email 的代码
SmtpClient client = new SmtpClient("smtp.office365.com")
{
//Port = 587, //outlook port 587
Port = 587,
EnableSsl = true,
//UseDefaultCredentials
UseDefaultCredentials = false,
TargetName = "STARTTLS/smtp.office365.com",
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("myemail@myemails.com","password"),
};
MailMessage message = new MailMessage()
{
From = new MailAddress("myemail@myemails.com"),
Subject = "New Requisition",
Body = "New requisition has been created:" + " " + requisition.RequisitionNo,
IsBodyHtml = true,
};
message.To.Add(SubmissionNotfication.To);
//message.CC.Add(SubmissionNotfication.Cc);
//var attachment = new System.Net.Mail.Attachment(filePath); //Attachment(string fileName, string mediaType)
//message.Attachments.Add(attachment);
client.Send(message);
}
Have a look at How to set up an application to send email using Microsoft 365 or Office 365查看如何设置应用程序以使用 Microsoft 365 或 Office 365 发送 email
You might have to use your MX endpoint as the sending domain and try port 25您可能必须使用 MX 端点作为发送域并尝试使用端口 25
SmtpClient client = new SmtpClient("yourorganisation.mail.protection.outlook.com")
{
Port = 25,
EnableSsl = true,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("myemail@myemails.com","password"),
};
It could be few reasons you encounter above error message您遇到上述错误消息的原因可能有几个
You may need to check tutorial about send email using outlook.您可能需要查看有关使用 outlook 发送 email 的教程。 If you are using Gmail, then you can refer to this post https://dotnetblog.asphostportal.com/using-gmail-to-send-email-in-asp-net-core/
如果您使用的是 Gmail,那么您可以参考这篇文章https://dotnetblog.asphostportal.com/using-gmail-to-send-email-in-asp-net-core/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.