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.
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.”
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.
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.
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:
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.
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.
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?
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
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...
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With