3

Sorry, I saw a lot of similar post regarding this matter but never found any solution to my problem, so I decided to post it.

I am using ASP.NET c# to send email programmatically using gmail with the following code.

 string EmailAddress = senderemail;
    MailMessage mailMessage = new MailMessage(EmailAddress, EmailAddress);
    mailMessage.Subject = "This is a test email";
    mailMessage.Body = "This is a test email. Please reply if you receive it.";

    SmtpClient smtpClient = new SmtpClient();
    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtpClient.EnableSsl = true;
    smtpClient.Host = "smtp.gmail.com";
    smtpClient.Port = 587;

    smtpClient.Credentials = new System.Net.NetworkCredential()
    {
        UserName = EmailAddress,
        Password = senderpassword
    };
    smtpClient.UseDefaultCredentials = false;
    smtpClient.Send(mailMessage);

I received this error like everybody else

Error:

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.

Already did below 2 action from GMAIL.

-less secure app : turn on

-2 step verification : off

I don't care whether is this gmail account safe or what. I don't need any security for this account. What else should i do?

DeanOC
  • 7,142
  • 6
  • 42
  • 56
ideas xchange
  • 39
  • 1
  • 1
  • 3
  • Have you verified that the values being passed into the NetworkCredential constructor are the same that work when logging in? Aside from `smtpClient.UseDefaultCredentials = false`, I have a project that uses more-or-less the same code without errors. – Tieson T. Apr 30 '15 at 03:07
  • Also need to make sure IMAP has been enabled for the account. – Tieson T. Apr 30 '15 at 03:11
  • This seems to be a common problem. I would read through the following post carefully and try all of the suggestions there: http://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp – Rajeev Goel Apr 30 '15 at 03:33
  • It's a common issue.You can try this http://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not?lq=1 – Rajeesh Menoth Apr 30 '15 at 04:32

8 Answers8

17

sign up Gmail and go to https://www.google.com/settings/security/lesssecureapps where you can see settings. Access for less secure apps

Turn off (default) ==> Turn On

after add this code,

  MailMessage mail = new MailMessage("fromm@gmail.com", "toaddress@gmail.com");
                mail.Subject = "TestEmailImportant";
                mail.Body = "This mail is to test if this program is working";

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

                smtpClient.Credentials = new System.Net.NetworkCredential()
                {
                    UserName = "XXXXXX@gmail.com",
                    Password = "YYYYYYYY"
                };

                smtpClient.EnableSsl = true;
                System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object s,
                        System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                        System.Security.Cryptography.X509Certificates.X509Chain chain,
                        System.Net.Security.SslPolicyErrors sslPolicyErrors)
                {
                    return true;
                };

                smtpClient.Send(mail);
MJ Vakili
  • 2,798
  • 1
  • 19
  • 25
  • Believe or not but the part with replacing the `ServicePointManager.ServerCertificateValidationCallback` helped. Thank you very much ! – michal Apr 22 '16 at 09:12
2

This generally happens when you try login from different time zone/IP/Computer. You production server and the mail id you have used both are in different time zone. Choose any of the one solution:

  1. Log in to production server via remote access, and sing in to gmail once with your credentials. They will ask for the confirmation, confirm it and log out.

Or

  1. Log in gmail to your local computer, Follow this Link and choose review this activity and take proper actions.
Krunal Mevada
  • 1,637
  • 1
  • 17
  • 28
  • 1
    I had no idea why sending worked fine on my localhost but didn't work on remote server... The first solution resolved the problem for me! Thanks! – Victor Sharovatov Sep 29 '16 at 12:36
0

I ran this code in my machine,it works. Probably Commented Line Not needed.....

MailMessage mailMessage = new MailMessage("From_User_Email", "To_Whom_Email");
        mailMessage.Subject = "This is a test email";
        mailMessage.Body = "This is a test email. Please reply if you receive it.";

        SmtpClient smtpClient = new SmtpClient();
        //smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpClient.EnableSsl = true;
        smtpClient.Host = "smtp.gmail.com";
        smtpClient.Port = 587;

        smtpClient.Credentials = new System.Net.NetworkCredential()
        {
            UserName = "From_User_Email",
            Password = "User_Password"
        };
        //smtpClient.UseDefaultCredentials = false;
        smtpClient.Send(mailMessage);
0

Try the following code :

UseDefaultCredentials = false;
Credentials = new NetworkCredential("xxx@gmail.com", "xxxpassword");

When you try to send mail from code and you find the error "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required", than the error might occur due to the cases which are beutifully explained here ,please have a look

Also check blocked sign ins here,security devices setting here

Also you can try the below code :

 MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress("xxx@gmail.com");

                mail.To.Add("xxx@gmail.com");
                mail.Subject = "xxxxx ";
                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>xxxxxx</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("xxx@gmail.com", "xxxpassword");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);

Thanks

Community
  • 1
  • 1
Tharif
  • 13,794
  • 9
  • 55
  • 77
0

Login to the following gmail account using a browser and follow the validation process. You may have sent a lot of emails and the account was blocked temporary.

Hussein Zawawi
  • 2,907
  • 2
  • 26
  • 44
0

Make sure you have a strong password. I had the problem with gmail because my password had only letters and numbers. I changed to use Upper and lower case letters, numbers and symbols, now it is working: "Using numbers, symbols and mix of upper and lower case letters in your password makes it harder for someone to guess your password. For example, an eight-character password with numbers, symbols and mixed-case letters is harder to guess because it has 30,000 times as many possible combinations than an eight-character password with only lower case letters." https://support.google.com/accounts/answer/32040?hl=en

mqueirozcorreia
  • 905
  • 14
  • 33
0

My case was solved this this solution6

Commit the following line:

smtpClient.UseDefaultCredentials = false;

If it not working try port 25 instead of 578.

https://productforums.google.com/forum/#!topic/gmail/w8hNo56tivE;context-place=topicsearchin/gmail/The$20SMTP$20server$20requires$20a$20secure$20connection$20or$20the$20client$20was$20not$20authenticated.$20The$20server$20response$20was$3A$205.5.1$20Authentication$20Required

0

Some times you only need to login gmail from this computer , if you did not login before from this PC . gmail will ask you to verify and confirm the new PC.