Labels

slider

Recent

Navigation

SMTP Server: Authentication the Server Response was 5.5.1 Authentication required in gmail

5.5.1 Authentication, SMTP server, secruity issue in Gmail, System.Net.Mail.SmtpException, Access for less secure apps, Gmail Settings

Introduction

I was working on MVC (C#) application sending email through Gmail Server, meanwhile popped up me issue the server response was 5.5.1 authentication required, So we required action to set on security issue which is default off in Gmail Email settings. I have published an article on How to send SMS using C#, now a days it has become very necessary to integrate SMS API into our application.

Error Description

An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code
Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

Gmail Code Snippet

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 = "youtemail@gmail.com";
    string senderPassword = "demo#123";
    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.

Now go to Gmail Settings Feature, it comes in your Gmail Inbox on right side, as you see in picture below. 
Gmail Settings

Move to Security Feature

Now click on Gmail Setting Feature, move to security feature or alternatively can hit/paste this URL in your browser https://www.google.com/settings/security/lesssecureapps where you can see settings. 

Access for less secure apps

  1. Turn off (default) 
  2. Turn On  
Access for less secure apps

Click on Turn On option to authorize emails sending through while programming (MVC, C#, PHP, Java etc.) with required credentials in Gmail account.

Video: Authentication the Server Response was 5.5.1 Authentication required in gmail

Summary

Above will help to resolve issue the server response was 5.5.1 authentication required while sending email through Gmail credentials in programming languages like ASP.Net, MVC, C#, PHP, Java etc.

Relevant Reading

Share

Anjan kant

Outstanding journey in Microsoft Technologies (ASP.Net, C#, SQL Programming, WPF, Silverlight, WCF etc.), client side technologies AngularJS, KnockoutJS, Javascript, Ajax Calls, Json and Hybrid apps etc. I love to devote free time in writing, blogging, social networking and adventurous life

Post A Comment:

49 comments:

  1. how to change the settings for other servers? i'm not using GMAIL, i am using internail mail server

    ReplyDelete
    Replies
    1. If you are not using like Gmail, Yahoo, Hotmail etc then you can ask from your hosting company to forward you SMTP server and Port, here we are using following detail, you need to change only.
      smtp.Host = "smtp.gmail.com";
      smtp.Port = 587;

      Delete
    2. Thank U sir...u just solved my problem with in a min.

      Delete
  2. your tutorial was useful to me
    iranians bless you

    ReplyDelete
    Replies
    1. Thanks Mohammad. Sure, If any help more required, let me know.

      Delete
  3. what are the steps for ymail and live mail sir?

    ReplyDelete
    Replies
    1. Change this code smtp.Host = "smtp.gmail.com"; for both (Live/Yahoo) below:
      // For Live
      smtp.Host = "smtp.live.com";
      We can use both port 25 or 587

      // For Yahoo
      smtp.Host = "smtp.mail.yahoo.com";
      We can use both port 25 or 587

      Delete
  4. Hello, i've done everything but all seems not working.Error says

    "smtp server requires a secure connection or the client was not authenticated.the server response was : 5.5.1 authentication required"

    Here's my code....


    Private Function SendErrorThroughEmail(FilePath As String) As String
    Dim UserName As String = "myemail@gmail.com"
    Dim tmpString As String
    Dim client As SmtpClient = New SmtpClient("smtp.gmail.com", 587)
    client.EnableSsl = True
    client.Credentials = New System.Net.NetworkCredential(UserName, "password")
    client.DeliveryMethod = SmtpDeliveryMethod.Network
    client.UseDefaultCredentials = False
    Dim mail As MailMessage = New MailMessage
    Dim attach As New Attachment(FilePath)
    mail.From = New MailAddress(UserName)
    mail.To.Add(New MailAddress(UserName))
    mail.Subject = "Ok"
    mail.Body = "This is a test message from myemail.gmail.com"
    If FilePath <> " then" Then
    mail.Attachments.Add(attach)
    End If
    mail.IsBodyHtml = True


    Try
    client.Send(mail)
    tmpString = "OK"
    Catch ex As Exception
    tmpString = ex.Message
    End Try
    Return tmpString
    End Function

    ReplyDelete
    Replies
    1. remove these both lines and then try again
      client.DeliveryMethod = SmtpDeliveryMethod.Network;
      client.UseDefaultCredentials = false;

      Delete
    2. hi I am doing the same program for sending the email and getting after removing those two lines I am getting this message
      System.Net.Mail.SmtpException: SSL must not be enabled for pickup-directory delivery methods. at System.Net.Mail.SmtpClient.Send(MailMessage message)

      Delete
  5. try to replace this line
    smtp.EnableSsl = false;

    ReplyDelete
  6. 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



    plz help my contact ni 9714411582

    ReplyDelete
    Replies
    1. Did you try all required steps provided above in this article.

      Delete
    2. hi anjan,...i have cofigued mail server in sql management...i have created a SSIS PKG & TRIED TO SEND MAIL USING SMTP TASK ...BUT GIVEN AN ERROR AS below..
      "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

      Delete
    3. yes that means you did not configured your email SMTP setting as mentioned in this article. try this one and let me know.

      Delete
  7. Thanks for your quick reply...MY REQUIREMENT IS not as in above article.......can u please tell me how can we send a mail using SMTP task(not using any code like c#/vb.net) from SSIS Package using sql server SSDT(SQL SERVER 2012).from our laptop/machines(local system)...Thanks in advance........

    ReplyDelete
    Replies
    1. here are articles which demonstrate how can we send email through sql server http://www.codeproject.com/Articles/485124/Configuring-Database-Mail-in-SQL-Server, http://blog.sqlauthority.com/2008/08/23/sql-server-2008-configure-database-mail-send-email-from-sql-database/

      Delete
    2. Thanks anjan for replay...as u mentioned paths above are usefull but they are not provided full information...my requirement is need to send email notifIycation using SMTP task IN SSIS PACKAge(SQL SERVER 2012) not using any code like c#/vb.net....

      Delete
    3. yes, here is explained SMTP in C#,MVC,VB etc but not mentioned for SSIS, next time will include it.

      Delete
  8. 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
    plz resolve my problem

    ReplyDelete
    Replies
    1. already provided resolution of this issue, if any more facing issue then let me know.

      Delete
  9. Hi Sir,
    i am getting this error msg
    Mailbox unavailable. The server response was: Access denied - Invalid HELO name (See RFC2821 4.1.1.1)

    while sending mail from specified domain
    its working perfectly while i m using gmail.
    how to fix it when using a specified host??

    thanks in advance

    ReplyDelete
    Replies
    1. It is clearly mean that you have to pass correct your specified domain given below information.
      Cross check in your hosting environment of Port No for SMTP, smtp.EnableSsl = true (if SSL have then true otherwise false or you can also remove this line) and SMTP Host and user name & password, moreover you can contact your hosting provider directly to confirm your mentioned info.

      Delete
  10. sir but when client sending mail he dont know how to change settings. is there any way to bypass this method and send email directly

    ReplyDelete
    Replies

    1. Client don't need to make any changes, website owner have only permissions to make necessary changes in your domain settings. Alternatively, you can also use this link
      Send Mail to send email directly.

      Delete
  11. 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
    plizz help me and my code is
    private void button1_Click(object sender, EventArgs e)
    {
    try
    {
    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
    client.EnableSsl = true;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Credentials = new NetworkCredential(textBox4.Text,textBox5.Text);
    MailMessage msg = new MailMessage();
    msg.To.Add(textBox1.Text);
    msg.From = new MailAddress(textBox4.Text);
    msg.Subject = textBox2.Text;
    msg.Body = textBox3.Text;

    client.Send(msg);
    }
    catch(SqlException ex)
    {
    MessageBox.Show(ex.Message);
    }
    }

    ReplyDelete
    Replies
    1. Now working fine your code given below, only add your emailid and password:

      try
      {
      SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
      client.EnableSsl = true;
      client.DeliveryMethod = SmtpDeliveryMethod.Network;
      client.UseDefaultCredentials = false;
      client.Credentials = new NetworkCredential("mygamil@gmail.com", "********");

      MailMessage msg = new MailMessage();
      msg.IsBodyHtml = true;

      msg.To.Add("sendingemail@gmail.com");
      msg.From = new MailAddress("test@gmail.com");
      msg.Subject = "Test";
      msg.Body = "testing body";

      client.Send(msg);
      }
      catch (Exception ex)
      {
      MessageBox.Show(ex.Message);
      }

      Delete
  12. Hello
    I also meet trouble with this issue. I work on Sitefinity CMS with these configuration in the backend of CMS. Can you suggest me some solution for this :( Thanks in advance!

    My configuration is here
    Host: smtp.gmail.com
    Port: 587
    UserName: {myGmail}
    Password: {myPassword}
    Domain: gmail.com
    DeliveryMethod: Network
    EnableSSL: true
    Timeout: 100000
    PickupDirectoryLocation: {N/A}
    DefaultSenderEmailAddreess: {the same as Gmail above}
    EmailSubjectEncoding: utf-8
    EmailBodyEncoding: utf-8

    ReplyDelete
    Replies
    1. Can you try with this code, once this code send email properly then after add other detail.

      try
      {
      SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
      client.EnableSsl = true;
      client.DeliveryMethod = SmtpDeliveryMethod.Network;
      client.UseDefaultCredentials = false;
      client.Credentials = new NetworkCredential("mygamil@gmail.com", "********");

      MailMessage msg = new MailMessage();
      msg.IsBodyHtml = true;

      msg.To.Add("sendingemail@gmail.com");
      msg.From = new MailAddress("test@gmail.com");
      msg.Subject = "Test";
      msg.Body = "testing body";

      client.Send(msg);
      }
      catch (Exception ex)
      {
      MessageBox.Show(ex.Message);
      }

      Delete
  13. Hello
    I also meet trouble with this issue. I work on Sitefinity CMS with these configuration in the backend of CMS. Can you suggest me some solution for this :( Thanks in advance!

    My configuration is here
    Host: smtp.gmail.com
    Port: 587
    UserName: {myGmail}
    Password: {myPassword}
    Domain: gmail.com
    DeliveryMethod: Network
    EnableSSL: true
    Timeout: 100000
    PickupDirectoryLocation: {N/A}
    DefaultSenderEmailAddreess: {the same as Gmail above}
    EmailSubjectEncoding: utf-8
    EmailBodyEncoding: utf-8

    ReplyDelete
    Replies
    1. Did you try same above given example. It is working for so many time and moreover can you specify your issue so that I can look into issue.

      Delete
  14. Very helpful tutorial..Thankyou

    ReplyDelete
  15. System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated.

    Above said error came on web server (Znet). My code is working properly on localhost only web server through this error.

    ReplyDelete
    Replies
    1. Check with your hosting provider, can you send email through your domain.

      Delete
    2. In localhost it works fine,
      in live provide error msg:
      Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.


      help me my code is:
      public ActionResult Email(RegInfo registration)
      {
      try
      {
      var from = System.Configuration.ConfigurationManager.AppSettings["UserName"];
      var smtpHost = System.Configuration.ConfigurationManager.AppSettings["Host"];
      var mailPwd = System.Configuration.ConfigurationManager.AppSettings["MailPwd"];
      var smtpPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Port"]);
      var callBackUrl = System.Configuration.ConfigurationManager.AppSettings["CallBackUrl"] + registration.ROLEBYID;
      var msg = new MailMessage(from, registration.EMAIL) { Subject = "Verify Your Account" };
      var Body = string.Format("Hi {0}
      Your Account Registerd.
      Your Id is :{1}
      Please confirm your account by clicking here", registration.NAME, registration.ROLEBYID, callBackUrl);
      msg.Body = Body;
      msg.IsBodyHtml = true;
      var smtp = new SmtpClient()
      {
      Host = smtpHost,
      Port = smtpPort,
      DeliveryMethod = SmtpDeliveryMethod.Network,
      UseDefaultCredentials = false,
      Credentials = new System.Net.NetworkCredential(from, mailPwd), EnableSsl = true
      };
      smtp.Send(msg);
      return RedirectToAction("Create");



      }
      catch (Exception ex)
      {
      TempData["error"] = ex.Message;
      return RedirectToAction("Create");
      }
      }

      Delete
    3. you need to a change in your web.config, set , it will resolve your problem.

      Delete
  16. hello Sir ,
    I want to send mail with out password.......only provide gmail id not password.....can it possible
    can you help me.....urgently....

    ReplyDelete
    Replies
    1. Without authentication, you can't send email.

      Delete
  17. I am getting this error of "An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code
    Additional information: Failure sending mail. "

    Also Turned on the option of ( Allow less secure apps: ON ) but still getting same error

    here is my below code:
    protected void SendEmail(object sender, EventArgs e)
    {
    string to = txtTo.Text;
    string from = txtEmail.Text;
    string subject = txtSubject.Text;
    string body = txtBody.Text;




    using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))
    {

    mm.Subject = txtSubject.Text;
    mm.Body = txtBody.Text;
    if (fuAttachment.HasFile)
    {
    string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
    mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
    }
    mm.IsBodyHtml = false;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.EnableSsl = false;
    NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = NetworkCred;
    smtp.Port = 587;
    smtp.Send(mm);
    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
    }
    }

    please help..

    ReplyDelete
    Replies
    1. update your code line: smtp.EnableSsl = true; it should work for you.

      Delete
  18. First login into Gmail Account from which Gmail credentials are you using to send email... this is the solution. thank you

    ReplyDelete
  19. after doing the less secure apps is ON,i m also getting the same error like The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

    ReplyDelete
  20. i am using zoho mail for sending emails and getting following error :
    "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. "-
    please give a solution

    ReplyDelete