One of my clients is having issues with his site not sending out emails. He's chosen to host his site with someone else which is making this problem even harder to track down. Here is the code I'm using now (and have used successfully across a hundred other sites):
Here are the settings in my Web.config:
Here is the error being returned:
Despite what the error suggests, I've checked at least 10 times and those credentials are indeed correct. As proof, if I programmatically pull the values directly from my Web.config and explicitly set them, everything works perfectly... as shown here:
Can ANYONE help me? I've already got a nice neat library built for this and I really hate having to hack something together because one site is being stubborn. Much appreciated.
Code:
MailMessage message = new MailMessage();
message.To.Add(new MailAddress("address@yourdomain.com"));
message.Subject = "This is the subject.";
message.IsBodyHtml = true;
message.Body = "This is the body.";
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Send(message);
Here are the settings in my Web.config:
Code:
<system.net>
<mailSettings>
<smtp from="admin@mydomain.com">
<network host="localhost" port="25" userName="admin@mydomain.com" password="mypassword" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
Here is the error being returned:
Exception Details: System.Net.Mail.SmtpException: Bad sequence of commands. The server response was: This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.
Despite what the error suggests, I've checked at least 10 times and those credentials are indeed correct. As proof, if I programmatically pull the values directly from my Web.config and explicitly set them, everything works perfectly... as shown here:
Code:
MailMessage message = new MailMessage();
message.To.Add(new MailAddress("address@yourdomain.com"));
message.Subject = "This is the subject.";
message.IsBodyHtml = true;
message.Body = "This is the body.";
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.config");
MailSettingsSectionGroup mailSettings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
client.Host = mailSettings.Smtp.Network.Host;
client.Port = mailSettings.Smtp.Network.Port;
client.Credentials = new NetworkCredential(mailSettings.Smtp.Network.UserName, mailSettings.Smtp.Network.Password);
client.Send(message);
Can ANYONE help me? I've already got a nice neat library built for this and I really hate having to hack something together because one site is being stubborn. Much appreciated.