I cannot figure out for the life of my why this isn\'t working
SmtpClient smtp = new SmtpClient
{
Host = \"smtp.gmail.com\",
Port = 587,
UseDefau
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
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...
<mailSettings>
<smtp from="youremail@gmail.com">
<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("myemail@gmail.com", "myGmailPasswordHere"),
This worked just fine for me
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
UseDefaultCredentials = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("myid@gmail.com", "mypassword"),
EnableSsl = true,
Timeout = 10000
};
MailMessage message = new MailMessage();
message.Body = "hello there";
message.Subject = "hi!!";
message.To.Add("myid@gmail.com");
message.From = new MailAddress("myid@gmail.com");
smtp.Send(message);
I had this problem before and fixed it by following these steps:
I just turned this option off and my code ran successfully.
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);