1

Im Develop the Asp.net web application. im Add the Email part, i had a following error cant sent the email.

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

Im try to fix it more than 5 hours, but i cant fix this, i was refer all of Stack overflow question

class

 public static void SendEmail(string receientEmail, string subject, string body) {
            //Email
            using (MailMessage mailMessage = new MailMessage()) {

                mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["UserName"]);

                mailMessage.Subject = subject;
                mailMessage.Body = body;
                mailMessage.IsBodyHtml = true;
                mailMessage.To.Add(new MailAddress(receientEmail));
                SmtpClient smtp = new SmtpClient();
                smtp.Host = ConfigurationManager.AppSettings["Host"];
                smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
                System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
                NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"];
                NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
                smtp.Send(mailMessage);
 }

WebConfig

 <appSettings>
    <add key="AdminLoginID" value="admin"/>
    <add key="AdminPassword" value="123"/>
<!--Email Setting-->
    <add key ="Host" value="smtp.gmail.com"/>
       <add key ="EnableSsl" value="true"/>
    <add key ="UserName" value="cafe****@gmail.com"/>
      <add key ="Password" value="****"/>
    <add key ="Port" value="587"/>     
  </appSettings>
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • Try following things: 1. Remove the line `smtp.UseDefaultCredentials = true;` 2. Confirm that username and password provided are correct. – Kamalesh Wankhede Nov 25 '16 at 12:23
  • Sir, Im Removed, but not work –  Nov 25 '16 at 12:38
  • Instead of getting values from ConfigurationManager, try hard coding values directly first. See if email is being sent. – Kamalesh Wankhede Nov 25 '16 at 13:37
  • Ooh, I forgot to mention one more thing. For sending mail using gmail server, you have to first set access to less secure apps. You can do that [here](https://www.google.com/settings/security/lesssecureapps) from google's settings. If you don't do that, you get "Authentication required" error. – Kamalesh Wankhede Nov 25 '16 at 13:41
  • sir, in try to sign in the google, what happen this, `This setting is not available for accounts with 2-Step Verification enabled. Such accounts require an application-specific password for less secure apps access` –  Nov 25 '16 at 15:06

2 Answers2

1

You supply NetworkCredentials and then overwrite them with UseDefaultCredentials that is set to true.

smtp.UseDefaultCredentials = false;

See the Microsoft Site for more detail

VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • 1
    Then take a look at [this](http://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp). – VDWWD Nov 25 '16 at 12:49
1

For sending mail using gmail smtp server, you have to first set access to less secure apps. You can do that here from google's settings. If you don't do that, you get "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required." error.

You can not turn on access to less secure apps if google two step verification is enabled. For sake of code simplicity(Prone to risk), I suggest you should turn off two step verification on your google account and then turn on access to less secure apps.

Refer this question to know more about this.

The only problem with your code is smtp.UseDefaultCredentials = true;. If you remove that line then, your code should work with other smtp servers which do not have two step verification enabled.

Community
  • 1
  • 1
Kamalesh Wankhede
  • 1,475
  • 16
  • 37