2

I am trying to send out an email from my windows form application. I have seen a lot of similar posts, but none seem to work. But when I change my settings in gmail to allow less secure apps, the code works. But I don't want to make my account vulnerable for this application. Here's the code and error I get if I don't allow less secure apps.

MailMessage mail = new MailMessage("abc@gmail.com", "xyz@gmail.com", "Test Automation", "Did you receive this?");
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.Credentials = new NetworkCredential("abc@gmail.com", "password");
client.EnableSsl = true;
client.Send(mail);
MessageBox.Show("Mail Sent", "success");

An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll

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

Please help!

P.S. : I created an outlook account using my gmail and when i put in the oulook server and credentials, the code works. So is it the issue with new gmail security changes? Other similar questions have their problem solved, but I keep having the same errors after trying pretty much every solution.

Nil
  • 401
  • 1
  • 6
  • 18
  • Possible duplicate of [Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required](http://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not) – hrust Oct 31 '15 at 23:31
  • Possible duplicate of [Sending email through Gmail SMTP server with C#](http://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp) – MethodMan Oct 31 '15 at 23:37
  • @Synaps no, its not the same. and i had tried both the steps, didnt work out – Nil Oct 31 '15 at 23:47
  • @MethodMan The problem is same, but the solution didnt solve my problem. tried them out. – Nil Oct 31 '15 at 23:51

1 Answers1

2

If you don't have 2 factor

Enable "Less Secure Apps"

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

If you have 2 factor authentication

You can make an "App Password". Go to the ling below and add a custom app (just write any name you want, name not important just used for your own "bookkeeping") then use that password as the password.

client.Credentials = new NetworkCredential("From@gmail.com", "Generated Password");

https://security.google.com/settings/u/1/security/apppasswords

NOTE: If you get "The setting you are looking for is not available for your account" then use "less secure app"

Thomas Andreè Wang
  • 3,379
  • 6
  • 37
  • 53
  • Did it.. Does not work.. Also tried adding ServerCertificateValidationCallback. That too didnt help. – Nil Oct 31 '15 at 23:37
  • added something more that might do it, im just gueassing here, have you tried sender info in the web.config? (just an idea) – Thomas Andreè Wang Oct 31 '15 at 23:41
  • Its a winform application. I dont have a web.config file. – Nil Oct 31 '15 at 23:43
  • deliveryMethod didnt help either – Nil Oct 31 '15 at 23:45
  • Rewrote the answer to something that made it work for me, got he same arror as you going straight forward. – Thomas Andreè Wang Oct 31 '15 at 23:53
  • I mentioned that in my question. My code works when I enable less secure apps [two factor is disabled all along]. But I dont want to make my account vulnerable. So, want another way out. I that possible, or the only way there is to enanle less secure apps? – Nil Oct 31 '15 at 23:56
  • ohh sorry about, that (did not see that part) il continue to try make it work – Thomas Andreè Wang Oct 31 '15 at 23:57
  • found it https://security.google.com/settings/u/1/security/apppasswords go it to work on my example atleast with my own 2 factor acc – Thomas Andreè Wang Nov 01 '15 at 00:00
  • "The setting you are looking for is not available for your account" – Nil Nov 01 '15 at 00:05
  • Suddenly i realized. The problem is that you don't have 2 factor enabled. then your only bet is on less secure or enable 2 factor and then use app password. – Thomas Andreè Wang Nov 01 '15 at 00:08
  • Yeah..Figured that out.. Now working, both your version, and mine one.. :) Thanks a lot. Amidst a couple of people shouting about duplicate, someone solved my problem. – Nil Nov 01 '15 at 00:13
  • Ye, sometimes the problem is not directly a technical one (then I think it deserves a solution amids the duplicate shouts). – Thomas Andreè Wang Nov 01 '15 at 00:16
  • 1
    Yeah, the security of google has changed over the years. What worked a few years back does not work now. – Nil Nov 01 '15 at 00:18