Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required?

Tags:

c#

smtp

I want to send an email from my application and i have written following code for sending mail

    MailMessage msg = new MailMessage();

    msg.From = new MailAddress("mymailid");
    msg.To.Add("receipientid");
    msg.Subject = "test";
    msg.Body = "Test Content";
    msg.Priority = MailPriority.High;

    SmtpClient client = new SmtpClient();

    client.Credentials = new NetworkCredential("mymailid", "mypassword", "smtp.gmail.com");
    client.Host = "smtp.gmail.com";
    client.Port = 587;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.EnableSsl = true;
    client.UseDefaultCredentials = true;

    client.Send(msg);

I am running it on localhost so what mistake i am doing to send it.

When i send button it gives an error like

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

Code in Web.config file

 <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />   
    <add key="smtpServer" value="smtp.gmail.com" />
    <add key="EnableSsl" value = "true"/>
    <add key="smtpPort" value="587" />
    <add key="smtpUser" value="[email protected]" />
    <add key="smtpPass" value="mypassword" />
    <add key="adminEmail" value="[email protected]" />
  </appSettings>
  <system.net>
    <mailSettings>
      <smtp from="[email protected]">
        <network host="smtp.gmail.com" password="mypassword" port="587" userName="[email protected]"  enableSsl="true"/>
      </smtp>
    </mailSettings>
  </system.net>

what should i do to solve this error and send mail??

like image 699
Abhay Andhariya Avatar asked Oct 06 '22 20:10

Abhay Andhariya


2 Answers

I have the same problem.

I have found this solution:

Google may block sign in attempts from some apps or devices that do not use modern security standards. Since these apps and devices are easier to break into, blocking them helps keep your account safer.

Some examples of apps that do not support the latest security standards include:

  • The Mail app on your iPhone or iPad with iOS 6 or below
  • The Mail app on your Windows phone preceding the 8.1 release
  • Some Desktop mail clients like Microsoft Outlook and Mozilla Thunderbird

Therefore, you have to enable Less Secure Sign-In (or Less secure app access) in your google account.

After sign into google account, go to:

https://www.google.com/settings/security/lesssecureapps
or
https://myaccount.google.com/lesssecureapps

In C#, you can use the following code:

using (MailMessage mail = new MailMessage())
{
    mail.From = new MailAddress("[email protected]");
    mail.To.Add("[email protected]");
    mail.Subject = "Hello World";
    mail.Body = "<h1>Hello</h1>";
    mail.IsBodyHtml = true;
    mail.Attachments.Add(new Attachment("C:\\file.zip"));

    using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
    {
        smtp.Credentials = new NetworkCredential("[email protected]", "password");
        smtp.EnableSsl = true;
        smtp.Send(mail);
    }
}
like image 218
mjb Avatar answered Oct 23 '22 00:10

mjb


First check for gmail's security related issues. You may have enabled double authentication in gmail. Also check your gmail inbox if you are getting any security alerts. In such cases check other answer of @mjb as below

Below is the very general thing that i always check first for such issues

client.UseDefaultCredentials = true;

set it to false.

Note @Joe King's answer - you must set client.UseDefaultCredentials before you set client.Credentials

like image 83
Ronak Patel Avatar answered Oct 23 '22 01:10

Ronak Patel