Given the following section in Web.Config
:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="SomeWebsite Admin <[email protected]>">
<network host="smtp.gmail.com" port="587" defaultCredentials="true" userName="[email protected]" password="somepassword" />
</smtp>
</mailSettings>
</system.net>
And the following code snippet:
smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential( "[email protected]", "somepassword" );
smtp.Send( mailMessage );
The above works fine, however commenting out the programmatic overrides, like this:
smtp = new SmtpClient();
//smtp.Host = "smtp.gmail.com";
//smtp.Port = 587;
smtp.EnableSsl = true;
//smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
//smtp.UseDefaultCredentials = false;
//smtp.Credentials = new NetworkCredential( "[email protected]", "somepassword" );
smtp.Send( mailMessage );
fails with:
System.Net.Mail.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 "}
Yes, the blank space after the "learn more at" is really there, and it makes things extra fun. Thing is, I can't be hardcoding these values, as they change from development to internal staging to live. So I'm wondering why it appears to fail to work with the Web.Config
defaults. I'm figuring I'm missing one critical something-or-other?
* EDIT *
Here's some more information, looking at the values of my smtp
object (without programmatic overrides except EnableSSL = true):
Host = smtp.gmail.com (makes sense -- proves I'm actually editing the right Web.Config...
DeliveryMethod = Network
Port = 587
Credentials.UserName = <blank> (Problem???)
Credentials.Password = <blank> (Problem???)
Credentials.Domain = <blank>
* EDIT of the EDIT *
The missing Username
and Password
values, and the accepted response, below, clued me in to the problem: defaultCredentials
in Web.Config must be false
.
On your computer, go to Gmail. Enter your Google Account email or phone number and password. If information is already filled in and you have to sign in to a different account, click Use another account. If you get a page that describes Gmail instead of the sign-in page, at the top right of the page, click Sign in.
A Gmail account is a free Google account with an email address that ends in @gmail.com. Gmail accounts arrived on the scene back in 2004, and they were such a hot commodity that people needed to receive an invitation in order to acquire an account.
Click the user's name to open their account page. At the top left, a message indicates why the user's Gmail account is suspended. (Optional) To find out how the user can avoid exceeding bandwidth limits in the future, go to Gmail bandwidth limits. At the top right, click Reactivate.
Gmail - Free Storage and Email from Google.
For googlers ending up here, the correct way to do this is:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="SomeWebsite Admin <[email protected]>">
<network host="smtp.gmail.com" port="587" enableSsl="true" defaultCredentials="false" userName="[email protected]" password="somepassword" />
</smtp>
</mailSettings>
</system.net>
And your code is then:
smtp = new SmtpClient();
smtp.Send( mailMessage );
I am not familiar with working this way but in the web.config defaultCredentials is true, you set it to false when doing it programatically is that the difference?
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