Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't auth to Gmail smtp via MailMessage & smtpClient

Tags:

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.

like image 644
PositiveGuy Avatar asked Feb 01 '12 22:02

PositiveGuy


People also ask

Why is my SMTP for Gmail not working?

In Google Mail, you must allow "less secure" apps access in order for your SMTP settings to work. There are two places this setting must be enabled: The first is here: https://myaccount.google.com/ under “Connected apps & sites.”

Does Gmail support SMTP AUTH?

Use the Gmail SMTP server If you connect using SSL or TLS, you can send mail to anyone inside or outside of your organization using smtp.gmail.com as your server. This option requires you to authenticate with your Gmail or Google Workspace account and passwords.

What is the SMTP port for Gmail?

The outgoing SMTP server, smtp.gmail.com , requires TLS. Use port 465 , or port 587 if your client begins with plain text before issuing the STARTTLS command.

How to receive incoming mail using SMTP on Gmail?

Since SMTP is only used to send outgoing mail, you need a way to fetch incoming mail. To receive incoming mail on your mail app, you need to set up an incoming mail message server using Gmail POP or IMAP protocols. Here’s how you can set up POP or IMAP settings for Gmail on your mail application:

How to send emails via Gmail API/SMTP in WordPress?

This is what lets you configure your WordPress site to send emails via the Gmail API/SMTP server. After activating the plugin, go to the Post SMTP tab in your WordPress dashboard and click the Show All Settings link underneath the big Start the Wizard button. Then, go to the Message tab and set your “from” email address and name.

What is the SMTP port for Gmail?

Gmail SMTP Password: your Gmail password Gmail SMTP port: 465 (SMTP SSL) or 587 (SMTP TLS) How to set up POP and IMAP Gmail settings Since SMTP is only used to send outgoing email, you need a way to fetch incoming mail.

Can I use Gmail to send emails to another email client?

If you want to also receive emails to your Gmail account in another email client, you’ll need to use POP3 or IMAP. You can find these settings by opening your Gmail settings and going to the Forwarding and POP/IMAP tab. Can I Use the Gmail SMTP Server to Send WordPress Transactional Emails?


4 Answers

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

like image 38
edteke Avatar answered Sep 22 '22 08:09

edteke


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

like image 87
Tiny Avatar answered Sep 24 '22 08:09

Tiny


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.

like image 11
Aleh Zasypkin Avatar answered Sep 25 '22 08:09

Aleh Zasypkin


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);
like image 3
krishna Avatar answered Sep 23 '22 08:09

krishna