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 fromAddres
Its a security issue, Gmail by default prevents access for your e-mail account from custom applications. You can set it up to accept the login from your application.
After Logging in to your e-mail, CLICK HERE
This will take you to the following page
A comment from Tomasz Madeyski is what fixed my problem... he tells that exists a bug on SetDefaultCredential, him says:
"The issue is that in UseDefaultCredentials setter there is this code: this.transport.Credentials = value ? (ICredentialsByHost) CredentialCache.DefaultNetworkCredentials : (ICredentialsByHost) null; which overrides credentials set by Credentials setter. For me it looks like SmtpClient's bug"
if you put smtpClient.UseDefaultCredentials = false
after set credentials... this line set to null those credentials...
First, I purchased the domain in GoDaddy, and when I clicked this link https://www.google.com/settings/security/lesssecureapps I didn't have the option, it show this message "The domain administrator manages these settings", so I go to the Admin Console https://admin.google.com/
There is this option
Then appears the option that I needed in
I used all of the above mentioned solutions but it finally worked only after i enabled IMAP Access from Gmail settings Link to Enable IMAP Access in gmail settings
Of course, the points in the other solutions were required too.
After spending a couple of hours today trying every solution here, I was still unable to get past this exact error. I have used gmail many times in this way so I knew it was something dumb, but nothing I did fixed the problem. I finally stumbled across the solution in my case so thought I would share.
First, most of the answers above are also required, but in my case, it was a simple matter of ordering of the code while creating the SmtpClient
class.
In this first code snippet below, notice where the Credentials = creds
line is located. This implementation will generate the error referenced in this question even if you have everything else set up properly.
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient
{
Host = Emailer.Host,
Port = Emailer.Port,
Credentials = creds,
EnableSsl = Emailer.RequireSSL,
UseDefaultCredentials = false,
DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network
}
However, if you move the Credentials setter call to the bottom, the email will be sent without error. I made no changes to the surrounding code...ie...the username/password, etc. Clearly, either the EnableSSL, UseDefaultCredentials, or the DeliveryMethod
is dependent on the Credentials
being set first... I didn't test all to figure out which one it was though.
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient
{
Host = Emailer.Host,
Port = Emailer.Port,
EnableSsl = Emailer.RequireSSL,
UseDefaultCredentials = false,
DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
Credentials = creds
}
Hope this helps save someone else some headaches in the future.
I had the same problem for an application deployed to Microsoft Azure.
SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
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:
var client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("my_user_name@gmail.com", "my_password");
It started working only after I set the following property on the smtp-client:
client.TargetName = "STARTTLS/smtp.gmail.com";