System.Web.Mail FAQ

Complete FAQ Listing

4.4.8 System.Web.Mail is corrupting my PDF attachments, what gives? Printer Friendly   Email This FAQ   Discuss

Ok, you are going to *LOVE* this one. ;-)

There is a bug in System.Web.Mail (actually the underlying CDOSYS).. First a little background.

The SMTP RFC implements a technique called "dot stuffing". Basically, any line that starts with a period must be escaped (or 'dot stuffed'), by being replaced with 2 periods.

Then, when the SMTP server recognizes these dot stuffed line, it unescapes the dots, and replaces 2 dots with 1 dot (which is what the original line looked like). However, System.Web.Mail forgets to dot stuff lines. Thus all lines that start with a dot, are NOT dot stuffed, and the single dot (which there is supposed to be 2 dots) is removed (now there isn't any dot starting the line) by the SMTP server. Whew... how's that for an explanation?

Now, since PDFs are simply a marked up text file, and PDF commands start with a dot, that single dot is being removed by the SMTP server, thus being corrupted. If System.Web.Mail replaced all lines starting a dot with 2 dots, then everything would be fine, because the receiving SMTP server would replace the 2 dots with 1 dot.

So, now, how do you fix this? Well, the only solution (that I know of ) is to purchase a reliable 3rd party component such as aspNetEmail. If anyone else knows of a different solution, feel free to post it here.

Still don't believe me, and want to test and verify this is a problem? Run the following code snippet:
[ C# ]
MailMessage m = new MailMessage();
   m.From = "";
   m.To = "";
   m.Subject = "period test";
   m.Body= "this \r\n. is a test";
   MailAttachment a = new MailAttachment( "c:\\test.txt" );
   m.Attachments.Add( a );
   SmtpMail.Send( m );

[ VB.NET ]
Dim m As New MailMessage()

m.From = ""
m.To = ""
m.Subject = "period test"
m.Body = "this " + ControlChars.Cr + ControlChars.Lf + ". is a test"

Dim a As New MailAttachment("c:\test.txt")


where test.txt consists of

and you will see the periods are removed.

Discuss This FAQ Item
Got a question? Another Suggestion? Want to post your solution? Discuss it below.
New Post
  • Use MailEncoding.UUEncode    [ Reply ]
    • RE: Use MailEncoding.UUEncode    [ Reply ]
    • RE: Use MailEncoding.UUEncode    [ Reply ]
    • RE: Use MailEncoding.UUEncode    [ Reply ]
    • RE: Use MailEncoding.UUEncode    [ Reply ]
    • RE: Use MailEncoding.UUEncode    [ Reply ]
  • PDF attached in W XP    [ Reply ]
  • mail.body    [ Reply ]
    • RE: mail.body    [ Reply ]
    • RE: mail.body    [ Reply ]
  • Semi Solution    [ Reply ]
  • thies worked for us    [ Reply ]
  • Microsoft's "fix"    [ Reply ]
    • RE: Microsoft's "fix"    [ Reply ]
  • Solution: empty email attachments    [ Reply ]
  • similar problem    [ Reply ]
  • Use FreeSMTP.Net    [ Reply ]
  • RE: Use MailEncoding.UUEncode    [ Reply ]
  • Is it on a particular OS??    [ Reply ]