IIS, Asp.net, C# sending SMTP Mail so it won’t get flagged as spam by filters
August 30, 2010
I had a hell of a time getting my smtp mailer to send to aol and a few other places on our IIS server. It seems that with one little setting change I was able to get things working correctly. After taking the time to actually spend a few hours scouring the net and reading up on the delivery method I figured out how to send smtp mail correctly.
So let’s look at the scenario. At one point in time our iis web server wasn’t sending out any mails entirely from our asp.net site. A change in mail servers messed our mail sending ability. That’s when I went in and added in
smtp deliveryMethod=”PickupDirectoryFromIis”
to our webconfig file. This of course got everything working fine and dandy. However we were getting complaints from customers about them not getting emails.
Looking into this I finally found that using pickupdirectoryfromiis uses the webserver to directly send the mail. This will bypass any setting you might have to use your email server to send out emails. Apparently AOL looks at your MX record to check to see if the machine it getting the email from is a email server. If it’s not a mail server then it will flag the email as spam. Bad, very bad.
After some trial and error I finally found the right settings to use for your webconfig and code behind.
In your webconfig you should use the following.
<system.net>
<mailSettings>
<smtp deliveryMethod=”Network” from=”username@yourserver.com”>
<network host=”mail.yourserver.com” userName=”username” password=”password” port=”25″ defaultCredentials=”false”/>
</smtp>
</mailSettings>
</system.net>
The important thing here is to use the Network delivery method. This will actually use the settings specified in the webconfig to send out your mail. Once you have this setup in your webconfig sending out the email is fairly easy. You can use the following code in your code behind to send out an email.
MailMessage msg2 = new MailMessage();
msg2.From = new MailAddress(“username@yoursite.com”, “User Name”);
msg2.To.Add(“touser@hissite.com”);
msg2.Subject = “Test Email”;
msg2.Body = “Test hello world”;
msg2.IsBodyHtml = false;
//this is all you need to make a smtp mail all the settings are in the webconfig
SmtpClient myClient2 = new SmtpClient();
//send the email
myClient2.Send(msg2);
Now when the code runs everything gets sent from the asp.net site on our IIS server to the mail server. Then it gets sent out from the mail server to the your final destination.
I hope you guys find this post useful and helpful.
Got something to say?
You must be logged in to post a comment.


