Tips for avoiding Spam Filters with System.Net.Mail

I recently noticed that automatic mails sent by one of my online shops were flagged as spam when using GMX, GMail or generally mail services which are using SpamAssassin to filter out spam.

Sending out plain text-mails is old and rusty, if you want to deliver pretty mails to your customers you have to use HTML, unfortunately there is no way around it, but how to make sure that those pretty HTML mails actually reach the customer? Spam filters like SpamAssassin treat HTML Mails as potentially dangerous, so you have to use HTML very carefully.

If you do use HTML Mails you have to create two kinds of Bodys, one HTML Mail Body and one PlainText Mail Body. I show you how to do this in C# / ASP.NET 2.0 by using the built-in System.Net.Mail Class after some general advices.

Here are some recommendations for a automatic HTML mails which are being sent out a hundred times per day:

  • Don’t forget to add <html><body>Your HTML Text/Code</body></html> to your HTML-Text-String
  • Try to stick with 1-3 Images in total. For example your Logo, a header background and a photo of the particular product.
  • Use HTML Tags with care. Set a good looking font like Arial, create listings, link tags, bold and italic texts and restrict yourself to create a well formed/layouted text, which is most important. Don’t even think about implenting an embed or object tag for including flash videos or anything like that.

SpamAssassin checks a lot of factors and is using a point-system for filtering out spam, here is a little snippet:

5.000 BAYES_99

The heaviest impact is BAYES_99, if your text sounds spammy, you are spam flagged. If you e.g. sell Viagra you shouldn’t list the product name in your confirmation mail ;-). Another example is MIME_HTML_ONLY, this hits your mail if you only supply HTML Text, without a seperated PlainText. I won’t explain every function in detail here, if you follow this HowTo you likely won’t run into any problems.

Let’s have a look on how to properly send a HTML Mail with the System.Net.Mail Class, in this case it is sending the Mail via Google Apps / GMail:

public void ExecuteHtmlSendMail(string FromAddress, string ToAddress, string BodyText, string Subject)
MailMessage mailMsg = new MailMessage();

mailMsg.From = new MailAddress(FromAddress);
mailMsg.To.Add(new MailAddress(ToAddress));
mailMsg.Subject = Subject;
mailMsg.BodyEncoding = System.Text.Encoding.GetEncoding(“utf-8″);

System.Net.Mail.AlternateView plainView = System.Net.Mail.AlternateView.CreateAlternateViewFromString
(System.Text.RegularExpressions.Regex.Replace(BodyText, @”<(.|\n)*?>”, string.Empty), null, “text/plain”);
System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(BodyText, null, “text/html”);


// Smtp configuration
SmtpClient smtp = new SmtpClient();
smtp.Host = “”;

smtp.Credentials = new System.Net.NetworkCredential(“username”, “password”);
smtp.EnableSsl = true;


What’s important here: Setting the correct Encoding, in my case utf-8. Create a plainView and htmlView of the mail message. For the plainView I used a simple Regex statement for stripping out the HTML Tags. Add the plainView and htmlView to the AlternateViews Class of the Mail Message and finally send the mail, in my case via SSL, if you don’t know whether you have an Mailserver with SSL capabilities just ask your provider.

Using this construct you send out your HTML Mails as HTML Mail and Plaintext Mail and SpamAssassin is going to like you for that.

I use for testing my mails as they are using a pretty well configured SpamAssassin and you can easily see the spam-flags by checking out the Header of the mail (also possible with GMail and hopefully every other mail application out there). An example of this:

X-GMX-UID: UNbaLmk4ZDI4zQ9QSmY2gY1xemhmY8Gs

A Spam-Score of 5 (=treated as Spam) and it’s telling me that I didn’t supply a PlainText Version of the Mail, after including that one all errors were gone.

Hope this saves some headaches 😉

kick it on


#1 Eduardo Costa on 09.19.07 at 3:32 pm

Those advices and sample code will really save some headaches. Thanks!

Regards from a brazilian friend.

#2 Dave Stitz on 09.22.07 at 5:11 am

Nice work on this post. This is a very helpful concept to keep in mind!

#3 Benny on 10.19.07 at 11:12 am

Thanks I had a problem when I used the body to put in the email text (sometimes it worked somtimes not) but using your code it allways works 🙂

#4 Anuj Jain HOVR on 01.22.08 at 12:28 pm

Thanks, your suggestions helped me get past the HOVR filters. Still being blocked by Yahoo unfortunately!

#5 Utku Ozturk on 01.27.08 at 3:27 pm

Great post! Very helpful, solved all headache. Thanks Andreas 🙂

#6 Mike on 02.04.08 at 10:27 pm

Thanks for the info it was helpful, now I don’t have any trouble sending emails

#7 Anup on 03.01.08 at 3:55 pm

Really nice suggestion

#8 Ray Akkanson on 03.24.08 at 7:28 pm

How would I add shortcut(link) attachment?

Ray akkanson

#9 Vijay Karla on 04.24.08 at 7:27 am

I am getting following reasons (from the mail header) for the mail getting marked spam:
X-Spam-Status: Yes, score=6.4 required=5.0 tests=
Any idea to avoid this?

#10 andreas.kraus on 04.24.08 at 7:37 am

Vijay: Did you follow my suggestions? Looks like your mail is only sending out HTML, no text.

#11 Mike Irving on 05.02.08 at 5:19 pm


I’ve just been migrating from System.Web.Mail to System.Net.Mail (as per Visual Studio’s recommendation).

Your article proved very helpful,



#12 Chris on 06.03.08 at 11:35 pm

You rule!

#13 Anders Kjeldsen on 06.05.08 at 1:56 am

Just what I was looking for. Thanks.

#14 Onur acun on 06.17.08 at 7:05 pm

Thank you, it is great

#15 Pratheesh on 10.21.08 at 6:26 am

I made the e mail content as email content . Email server is not ssl enabled. So I did not set ssl enabling in email. Some of my emails are going into spam. I used gmail account to test the code.

#16 Caleb on 11.01.08 at 12:48 am

Thank you so much for this article! It really helped me out a lot!

#17 Su on 01.28.09 at 5:55 am

This is a very good guideline to avoide spam filter.
One thing I would like to ask you – After senning the charecter encoding utf-8, the single quote(‘) change to question mark(?).
Please provide me any suggestion for this.

Thank you.

#18 KAGRAN22 on 02.04.09 at 11:00 am

You use for mailMsg Encoding UTF-8: (mailMsg.BodyEncoding)

But in AlternateView you use null as Second Param which will equal to ascii as Encoding not UTF-8 from mailMsg.bodyEncoding (tested in NET 2.0)

You should also set in CreateAlternateViewFromString the UTF-8 Encoding!

This explains probally why Su (MSG #17) is getting wrong Chars

#19 Nikhil on 02.17.09 at 7:15 am

hey in case i created a filter in my gmail cant i receive those mails in the gMX mail????? if so how??? please help out.

#20 David on 03.04.09 at 3:32 pm

Unfortunately, is only for person living in Germany and 1 or 2 other countries.

I’m living in Canada. Could you please suggest me a way to test my emails ?

#21 Gustav on 03.09.09 at 9:42 am

Try at

#22 Haris on 07.03.09 at 10:46 am

Very much helpful,

#23 malappuram on 07.03.09 at 10:48 am

Helpful Tip for HTML Mailing,


#24 Manjur on 01.18.10 at 1:44 pm

Really helpful.

#25 roman on 02.07.11 at 11:22 pm

works like a charm!!

#26 Steven on 06.13.12 at 9:54 pm

This post is great.
Thanks a lot.


#27 Serkan on 11.15.12 at 10:17 pm

This post is great.
Thanks a lot.


Leave a Comment