Reputation: 47733
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.
Upvotes: 20
Views: 39712
Reputation: 41
For me the solution required 2 "steps":
UseDefaultCredentials = false
first, then set the credentials I want to use Credentials = new NetworkCredential("[email protected]", "myGmailPasswordHere")
. Setting the credentials first, when I set UseDefaultCredentials = false
will make the Credentials property null
.Upvotes: 1
Reputation: 352
I had this problem before and fixed it by following these steps:
I just turned this option off and my code ran successfully.
Upvotes: 3
Reputation: 562
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
Upvotes: 23
Reputation: 880
Very simple just follow this for C# WPF Application:
private void SendByGmail(string subject, string body, string recepientEmail, string MailMsgFrom, string MailMsgPass)
{
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress(MailMsgFrom);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(recepientEmail));
mailMessage.Priority = System.Net.Mail.MailPriority.High;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.Timeout = 200000;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = MailMsgFrom;
NetworkCred.Password = MailMsgPass;
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mailMessage);
}
}
After that you should get like this Error
smtpException {"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"} System.Net.Mail.SmtpException
To Solve this problem, at first login your email account to your google account in web browser. Then just follow this link Google Account Activity. Then you'll get recent Devices & activity by your account. If show block your current activity from your current device. Just Unblock this. Then try again to send email.
Thanks
Upvotes: 0
Reputation: 2486
My problem was that the domain-owner for our gmail-account had disabled both "Access for less secure apps" and two step authentication. There was no way to enable it, I couldn't even see the setting. So I tested with my personal account instead, and it worked fine.
Upvotes: 0
Reputation: 573
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...
Upvotes: 41
Reputation: 271
Had the same issue accessing smtp.gmail.com from an ASP.NET application running on Amazon AWS hosting. It turned out that my configuration was right - it was also working fine from another computer - but gmail would deny my login attempt, because I try logging in from an unusual location. I logged on to gmail over the web interface (www.gmail.com), and had to answer a captcha. After that it worked.
Upvotes: 0
Reputation: 741
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.
Upvotes: 11
Reputation: 1351
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);
Upvotes: 3
Reputation: 13250
<mailSettings>
<smtp from="[email protected]">
<network host="smtp.gmail.com" password="yourpassword" port="587" userName="username"/>
</smtp>
</mailSettings>
Edit: try adding this line smtp.Credentials = Credentials
after this
Credentials = new NetworkCredential("[email protected]", "myGmailPasswordHere"),
Upvotes: 0
Reputation: 2104
Have a callback as follows. Tell System.Net
to ignore the error!
Add this before call to Send()
ServicePointManager.ServerCertificateValidationCallback =
delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
smtp.Send(mail);
Upvotes: 0