• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

ASP.NET (C#) - issues with System.Net.Mail

JPWilson

Weaksauce
Joined
Jun 26, 2007
Messages
70
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):
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.
 
A lot of SMTP servers have anonymous access. Have you tried that?
 
I've seen similar issues when the message is flag as being spam. I don't remember the specific error (possible the same?) but I know it wasn't descriptive of the actual problem (spam filter).
 
I think your issue lies with the "UseDefaultCredentials" property. You might want to put up a test page that outputs the various objects/properties for the current in-process user account, what ASP.Net thinks the current logged in user is, etc. ... perhaps also the values behind "mailSettings.Smtp.Network.Username" for good measure. I think those values are going to be different across the board, and will help you zero-in on what changes would be appropriate to fit your custom Mail class.

I'm basing this off of what MSDN lists in their "Remarks" section on UseDefaultCredentials, and that it also lists the default value for this property as "false".
 
Back
Top