18
Given the following section in Web.Config:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="SomeWebsite Admin <someuser@gmail.com>">
<network host="smtp.gmail.com" port="587" defaultCredentials="true" userName="someuser@gmail.com" 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( "someuser@gmail.com", "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( "someuser@gmail.com", "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