I am using following code to send email. The Code works correctly in my local Machine. But on Production server i am getting the error message
var fromAddress = new MailAddress("mymailid@gmail.com");
var fromPassword = "xxxxxx";
var toAddress = new MailAddress("yourmailid@yourdoamain.com");
string subject = "subject";
string body = "body";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
smtp.Send(message);
And on my Gmail A/c I have received the following email after i ran the code from production server
Hi ,
Someone recently used your password to try to sign in to your Google Account mymailid@gmail.com. This person was using an application such as an email, client or mobile device.
We prevented the sign-in attempt in case this was a hijacker trying to access your account. Please review the details of the sign-in attempt:
Friday, 3 January 2014 13:56:08 o'clock UTC IP Address: xxx.xx.xx.xxx (abcd.net.) Location: Philadelphia PA, Philadelphia, PA, USA
If you do not recognise this sign-in attempt, someone else might be trying to access your account. You should sign in to your account and reset your password immediately.
Reset password
If this was you and you are having trouble accessing your account, complete the troubleshooting steps listed at http://support.google.com/mail?p=client_login
Yours sincerely, The Google Accounts team
If your are using gmail.
1-logon to your account
2- browse this link
3- Allow less secure apps: ON
Enjoy....
dont put break-point before await smtp.SendMailAsync(mail);
:)
when it waits with a break point giving this error when i remove break point it has worked
Just follow the step in the google email and enable less secure apps.
You should disallow less secure apps to access your google account.
to do:
https://myaccount.google.com/lesssecureapps
NOVEMBER 2018, have tried everything above with no success.
Below is the solution that worked finally. Unfortunately it's not using SSL, but it works!!!
I had the same problem for an application deployed to Microsoft Azure.
First I approved all unknown devices (some ip-addresses originating from Ireland) on the following page (signed in as the gmail user): https://security.google.com/settings/u/1/security/secureaccount
I used the following settings for the client:
It started working only after I set the following property on the smtp-client: