0

I have a C# .Net code that sends email through gmail when I am using one of the gmail accounts like below:

        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new NetworkCredential("test@example.com","Test12345");
        smtp.Timeout = 9000;
        smtp.Send(MMsg);

The email that is working is the one I was using in past but we need to change this email to the new one. Any of the new email credentials is not working. It gives me the error as:

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 

Line 120:            smtp.Credentials = new NetworkCredential("test@example.com", "Test12345");
Line 121:            smtp.Timeout = 9000;
Line 122:            smtp.Send(MMsg);

Any idea what I am missing? How can I make other new email through application to work?

Update: I had to turn on "Access for less secure Apps" on google site: https://www.google.com/settings/security/lesssecureapps to make it work.

TechPro
  • 331
  • 1
  • 10
  • 29
  • 1
    your credential is incorrect. Just by looking at your code, I can see that you did not provide a proper email account – EagerToLearn Dec 11 '15 at 16:06
  • Did you check this answer? http://stackoverflow.com/a/298379/4995166 Try this. I think it'll work for you – CoderWho Dec 11 '15 at 16:06
  • The error message to me seems quite self-explanatory... Your credentials are wrong. – Rob Dec 11 '15 at 16:07
  • 1
    @NguyenHoang you don't seriously expect him to provide his gmail login credentials. It's obviously been anonymized. – user1666620 Dec 11 '15 at 16:14
  • I didn't provide the actual email id for privacy, that's correct!!! – TechPro Dec 11 '15 at 16:54
  • 1
    Possible duplicate of [How can I make SMTP authenticated in C#](http://stackoverflow.com/questions/298363/how-can-i-make-smtp-authenticated-in-c-sharp) – Daniel Corzo Dec 11 '15 at 17:02

2 Answers2

2

Your code looks fine.

Assuming your login credentials are correct, check if you have Allow Less Secure Apps enabled on your gmail account, and check if 2 factor authentication is enabled.

If 2 factor authentication is enabled, you will need to create an app-specific password and use that for the login credentials.

user1666620
  • 4,800
  • 18
  • 27
1

It's probably because you need to enable less secure apps.

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

You can then send e-mail as per below:

var fromAddress = new MailAddress("myaccount@gmail.com", "My Name");
var toAddress = new MailAddress("test.address@email.com", "Mr Test");
const string fromPassword = "tbhagpfpcxwhkczd";
const string subject = "test";
const string body = "HEY, LISTEN!";

var smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
    Timeout = 20000
};

using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    smtp.Send(message);
}
Equalsk
  • 7,954
  • 2
  • 41
  • 67
  • 2
    Can you explain why is your code sample better than the one the OP is better, or why you think his is inadequate? – user1666620 Dec 11 '15 at 16:13
  • The core of my answer is around enabling less secure apps as this is why he's getting the error. My code isn't particularly better, it's just there for a sense check. – Equalsk Dec 11 '15 at 16:14