In previous tutorial, we have written an article about how to use Gmail to send email in Asp.net Core. We have received few users experience error message when try to setup it. So, in this tutorial, we will advise how to fix error the server response was 5.5.1 authentication required
Error Message
Gmail Code
Below is the code snippet using to send Email through Gmail Host:
[HttpPost] public JsonResult SendEmail(string _name, string _phone, string _email, string _description) { string senderID = "[email protected]"; string senderPassword = "hello123"; string result = "Email Sent Successfully"; string body = " " + _name + " has sent an email from " + _email; body += "Phone : " + _phone; body += _description; try { MailMessage mail = new MailMessage(); mail.To.Add(senderID); mail.From = new MailAddress(senderID); mail.Subject = "My Test Email!"; mail.Body = body; mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address smtp.Credentials = new System.Net.NetworkCredential(senderID, senderPassword); smtp.Port = 587; smtp.EnableSsl = true; smtp.Send(mail); } catch (Exception ex) { result = "problem occurred"; Response.Write("Exception in sendEmail:" + ex.Message); } return Json(result); }
First login into Gmail Account from which Gmail credentials are you using to send email.
Move to Security Feature
Access for less secure apps
- Turn off (default)
- Turn On
Click on Turn On option to authorize emails sending through while programming (MVC, C#, PHP, Java etc.) with required credentials in Gmail account.
Summary
Yury Sobolev is Full Stack Software Developer by passion and profession working on Microsoft ASP.NET Core. Also he has hands-on experience on working with Angular, Backbone, React, ASP.NET Core Web API, Restful Web Services, WCF, SQL Server.