how to send email in Asp.net mvc 5 [duplicate]

南楼画角 提交于 2019-12-14 03:02:50

问题


What code should I use to send email in C#?

I tried to find a specific code so that I could send an email from my website. And then I get an error: "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"

public void sendEmail(string toEmail, string subject, string emailBody)
{
     string senderEmail = "My_Email";
     string senderPassword = "My_Email_Password";
     SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
     client.EnableSsl = true;
     client.Timeout = 500000;
     client.DeliveryMethod = SmtpDeliveryMethod.Network;
     client.UseDefaultCredentials = false;
     client.Credentials = new NetworkCredential(senderEmail, senderPassword);
     MailMessage mailMessage = new MailMessage(senderEmail, toEmail, subject, emailBody);
     mailMessage.IsBodyHtml = true;
     mailMessage.BodyEncoding = UTF8Encoding.UTF8;
     client.Send(mailMessage);
}

Do I need to use Google API??


回答1:


Its a security issue, Gmail by default prevents access for your e-mail account from custom applications. You can set it up to accept the login from your application.

You need to go to security settings at the followig link https://www.google.com/settings/security/lesssecureapps and enable less secure apps. So that you will be able to login from all apps.



来源:https://stackoverflow.com/questions/57484926/how-to-send-email-in-asp-net-mvc-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!