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

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??

This question is related to c# smtp

The answer is


Ensure you set SmtpClient.Credentials after calling SmtpClient.UseDefaultCredentials = false.

The order is important as setting SmtpClient.UseDefaultCredentials = false will reset SmtpClient.Credentials to null.


I had the same issue, it was resolved by change password to strong password.


Try it this way, I just made some light changes:

MailMessage msg = new MailMessage();

msg.From = new MailAddress("[email protected]");
msg.To.Add("[email protected]");
msg.Subject = "test";
msg.Body = "Test Content";
//msg.Priority = MailPriority.High;


using (SmtpClient client = new SmtpClient())
{
    client.EnableSsl = true;
    client.UseDefaultCredentials = false;
    client.Credentials = new NetworkCredential("[email protected]", "mypassword");
    client.Host = "smtp.gmail.com";
    client.Port = 587;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;

    client.Send(msg);
}

Also please show your app.config file, if you have mail settings there.


If it's a new google account, you have to send an email (the first one) through the regular user interface. After that you can use your application/robot to send messages.


I was also facing the issue like 'The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.0 Authentication Required' then went through so much internet materials but it didn't helped me fully. How I solved it like

step1:smtp.gmail.com is gmail server so go to your account gmail settings->click on see all settings->Forwarding and IMAP/POP->check pop and imap is enabled ,if not enable it->Save changes. step2-click on your gmail profile picture->click on Manage your google account->go to security tab->check for Access to less secure apps(this option will be available if you havent opt for two step verification)->by default google will set it as disable, make it enable to use your real gmail password working for sending email. note:-Enabling gmail access for less secure apps,may be dangerous for you so i dont recommend this

step3:-if your account has two step verification enabled or want to use password other than your gmail Real password using app specific password then try this:- click on your gmail profile picture->click on Manage your google account->go to security tab->search for APP PASSWORD->select any app name given->select any device name->click on generate->copy the 16-digit password and paste it into your app where you have to enter a gmail password in place of your real gmail password.


I have used the steps in this question and it worked for me.

My issue was that G suite didn't consider my password as strong and after changing it it worked perfectly.


App Passwords helped me.

  1. Go to https://myaccount.google.com/security.
  2. Scroll down to "Signing in to Google".
  3. Enable 2-Step Verification.
  4. Add App Password.
  5. Use the generated password in your code.

I created a Microsoft 365 Developer subscription (E5) today morning and used it to send a test email using the following settings

using (SmtpClient client = new SmtpClient())
{
    client.EnableSsl = true;
    client.UseDefaultCredentials = false;
    client.Credentials = new NetworkCredential(username, password);
    client.Host = "smtp.office365.com";
    client.Port = 587;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;

    client.Send(msg);
}

It did not work in the beginning, as I kept getting this error message with the exception thrown by this code. Then, I spent about 4+ hours playing with the Microsoft 365 admin centre settings and reading articles to figure out the issue. Ultimately I changed my Microsoft 365 admin centre password and it worked like a charm. So, it is worth to try changing the password when you get this message, before thinking about any advance solution.

Note that the password wasn't invalid for sure as I logged on to my Microsoft 365 account without any issues. however, the password change solved the issue.


I've searched and tried different things for hours.. To summarize, I had to take into consideration the following points:

  1. Use smtp.gmail.com instead of smtp.google.com
  2. Use port 587
  3. Set client.UseDefaultCredentials = false; before setting credentials
  4. Turn on the Access for less secure apps
  5. Set client.EnableSsl = true;

If these steps didn't help, check this answer.
Perhaps, you can find something useful on this System.Net.Mail FAQ too.


If you face this issue with Office 365 account. In Exchange Online, by default, the SMTP Client Authentication will be disabled for all Office 365 mailbox accounts. You have to manually enable SMTP Auth for the problematic account and check the case again. Check the below threads.

https://morgantechspace.com/2021/01/the-smtp-server-requires-a-secure-connection-or-the-client.html

https://docs.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/authenticated-client-smtp-submission


You should consider to specify SMTP configuration data in config file and do not overwrite them in a code - see SMTP configuration data at http://www.systemnetmail.com/faq/4.1.aspx

<system.net>
            <mailSettings>
                <smtp deliveryMethod="Network" from="[email protected]">
                    <network defaultCredentials="false" host="smtp.example.com" port="25" userName="[email protected]" password="password"/>
                </smtp>
            </mailSettings>
        </system.net>

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);
    }
}

-------------------
Info shared by Michael Freidgeim in below comments area:
Similar answer with screenshots https://stackoverflow.com/a/32457468/52277


I have encountered the same problem several times. After enabling less secure app option the problem resolved. Enable less secure app from here: https://myaccount.google.com/lesssecureapps

hope this will help.


If you are in a test environment and do not want to set security settings you have to allow less secure apps via. this link in Gmail.

https://myaccount.google.com/lesssecureapps


Below is my code.I also had the same error but the problem was that i gave my password wrong.The below code will work perfectly..try it

            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");              
            mail.From = new MailAddress("[email protected]");
            mail.To.Add("[email protected]");
            mail.To.Add("[email protected]");
            mail.Subject = "Password Recovery ";
            mail.Body += " <html>";
            mail.Body += "<body>";
            mail.Body += "<table>";

            mail.Body += "<tr>";
            mail.Body += "<td>User Name : </td><td> HAi </td>";
            mail.Body += "</tr>";

            mail.Body += "<tr>";
            mail.Body += "<td>Password : </td><td>aaaaaaaaaa</td>";
            mail.Body += "</tr>";

            mail.Body += "</table>";
            mail.Body += "</body>";
            mail.Body += "</html>";

            mail.IsBodyHtml = true;
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("sendfrommailaddress.com", "password");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);

You can reffer it in Sending mail


Try to login in your gmail account. it gets locked if you send emails by using gmail SMTP. I don't know the limit of emails you can send before it gets locked but if you login one time then it works again from code. make sure your webconfig setting are good.


try to enable allow less secure app access.

Here, you can enable less secure app after login with your Gmail.

https://myaccount.google.com/lesssecureapps

Thanks.


After turning less secure option on and trying other solutions, if you are still facing the same problem try to use this overload:

client.Credentials = new NetworkCredential("mymailid", "mypassword");

instead of:

client.Credentials = new NetworkCredential("mymailid", "mypassword", "smtp.gmail.com");

enter image description here Make sure that Access less secure app is allowed.

        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("[email protected]");
        mail.Sender = new MailAddress("[email protected]");
        mail.To.Add("external@emailaddress");
        mail.IsBodyHtml = true;
        mail.Subject = "Email Sent";
        mail.Body = "Body content from";

        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
        smtp.UseDefaultCredentials = false;

        smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "xx");
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.EnableSsl = true;

        smtp.Timeout = 30000;
        try
        {

            smtp.Send(mail);
        }
        catch (SmtpException e)
        {
            textBox1.Text= e.Message;
        }

Turn on less secure app from this link and boom...


I encountered the same problem even I set "UseDefaultCredentials" to false. Later I found that the root cause is that I turned on "2-step Verification" in my account. After I turned it off, the problem is gone.


some smtp servers (secure ones) requires you to supply both username and email, if its gmail then most chances its the 'Less Secure Sign-In' issue you need to address, otherwise you can try:

public static void SendEmail(string address, string subject, 
    string message, string email, string username, string password, 
    string smtp, int port)
{
    var loginInfo = new NetworkCredential(username, password);
    var msg = new MailMessage();
    var smtpClient = new SmtpClient(smtp, port);

    msg.From = new MailAddress(email);
    msg.To.Add(new MailAddress(address));
    msg.Subject = subject;
    msg.Body = message;
    msg.IsBodyHtml = true;

    smtpClient.EnableSsl = true;
    smtpClient.UseDefaultCredentials = false;
    smtpClient.Credentials = loginInfo;
    smtpClient.Send(msg);
}

notice that the email from and the username are different unlike some implementation that refers to them as the same.

calling this method can be done like so:

SendEmail("[email protected]", "test", "Hi it worked!!", 
   "from-mail", "from-username", "from-password", "smtp", 587);