Can't auth to Gmail smtp via MailMessage & smtpClient

36,680

Solution 1

I know this is an old topic, BUT... Google has changed something on their security settings.

Was struggling with all the answers until I checked my email with a mail from Google stating that "we've recently blocked a sign-in attempt on your Google account".

That led me to this page: Google Account Security

Under the "Access for less secure apps" section, you can enable access to your account from other devices/applications... like your C# application.

Note, there is no longer an "application specific" section.

Hope this helps someone... I just lost 30 minutes of my life...

Solution 2

If login info is 100% right, you need to set UseDefaultCredentials = false first and then set the credentials you want to use Credentials = new NetworkCredential("[email protected]", "myGmailPasswordHere").

If you set the credentials first, when you set UseDefaultCredentials = false this will make the Credentials property to null.

This is wired, but it happened to me.

Debug your code and check if the Credentials property is null before you call smtp.Send(message);. If so, then try inverting the order. It seems you have it in the right order, but if it's null, don't use the inline initialization.

Hope it helps.

EDIT: If you are using two-step verification, be sure you are using an App Specific password

Solution 3

It looks like Gmail requires Application-specific password(not your main password).

Please, look into this: http://support.google.com/mail/bin/answer.py?hl=en&answer=1173270

I had the same problem recently.

Solution 4

I had this problem before and fixed it by following these steps:

  1. Go to "My Account" settings.
  2. Click "Sign-in & Security" category.
  3. Scroll down to "Connected apps & sites" section.
  4. turn off the "Allow less secure apps" option.

I just turned this option off and my code ran successfully.

Solution 5

This worked just fine for me

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

    MailMessage message = new MailMessage();
    message.Body = "hello there";
    message.Subject = "hi!!";
    message.To.Add("[email protected]");
    message.From = new MailAddress("[email protected]");
    smtp.Send(message);
Share:
36,680
PositiveGuy
Author by

PositiveGuy

Updated on July 09, 2022

Comments

  • PositiveGuy
    PositiveGuy almost 2 years

    I cannot figure out for the life of my why this isn't working

    SmtpClient smtp = new SmtpClient
    {
        Host = "smtp.gmail.com",
        Port = 587,
        UseDefaultCredentials = false,
        DeliveryMethod = SmtpDeliveryMethod.Network,
        Credentials = new NetworkCredential("[email protected]", "myGmailPasswordHere"),
        EnableSsl = true,
        Timeout = 10000
    };
    
    
    smtp.Send(mail);
    

    I get:

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

    I just specified EnableSsl to true so that shouldn't be the issue in terms of secure connection.

    I'm running this from localhost. And yes, my username and password I'm entering to auth (my gmail account credentials) is 100% right.

  • PositiveGuy
    PositiveGuy over 12 years
    yea I know you can do this but this doesn't explain why the hard coded magic strings of mine above (only for testing) isn't working.
  • PositiveGuy
    PositiveGuy over 12 years
    why? I already set that in the object initializer for smtpClient Credentials = new NetworkCredential("[email protected]", "myGmailPasswordHere")
  • ukhardy
    ukhardy over 12 years
    Well don't eat then... handle the enum. Do tell us what you got.
  • Al Dass
    Al Dass about 11 years
    Much thx, that did the trick for me. Using the code base from here, following this advice along with my application generated 2-step verification Google password (not my regular Gmail password), I was able to send email from a C# test client. I was doing development/testing on my local desktop using my personal Gmail account and forgot I had enabled Gmail's 2-step verification...so that is 2 hours of my life I'll never get back! :)
  • radu florescu
    radu florescu over 10 years
    Indeed this really works. Had same problem and this fixed mine out.
  • MrZander
    MrZander over 9 years
    I spent 3 hours on this today... can't believe this fixed it.
  • okieh
    okieh about 9 years
    OMG... spent half of the day trying nearly everything. THANKS for the answer!
  • Silver
    Silver about 9 years
    this answer should get more votes.. sometimes we should try less voted answers too.. I tried all articles and really gave up after I tried solution marked as answer in stackoverflow.com/questions/32260/… I mean I had no idea what am doing wrong.. Thanks Tiny.. your solution worked for me..
  • Silver
    Silver about 9 years
    this did not work for me but i did consider it.. plz try stackoverflow.com/a/25414099/2649883 if still facing problem
  • Silver
    Silver about 9 years
    stackoverflow.com/a/25414099/2649883 works fine. there is no application specific section in gmail anymore
  • Optimus1
    Optimus1 about 9 years
    This really helped me. I don't understand why this information isn't easier to find.
  • Admin
    Admin over 8 years
    So what do I need to change in my code if I don't want to allow "less secure apps" in my gmail account?
  • itsho
    itsho over 8 years
    if you use your own domain, you need to use admin.google.com to set 2-step-verification or allowing less secure apps
  • inkubpl
    inkubpl about 5 years
    incredible but in this order is work correctly SmtpClient smtpClient = new SmtpClient(); smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new NetworkCredential(senderAddress,senderPassword); smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.Host = host; smtpClient.Port = port; smtpClient.EnableSsl = useSsl; return smtpClient;
  • emirhosseini
    emirhosseini almost 5 years
    I turned on allowing less secure app access and still having intermittent issues. I don't follow what this reply is suggesting to do exactly.