<<Back To Faq 4.4.8


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 = "dave@aspnetemail.com";
   m.To = "dave@aspnetemail.com";
   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 = "dave@aspnetemail.com"
m.To = "dave@aspnetemail.com"
m.Subject = "period test"
m.Body = "this " + ControlChars.Cr + ControlChars.Lf + ". is a test"

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

SmtpMail.Send(m)

where test.txt consists of

.1
.2
.3
.4
and you will see the periods are removed.
 



The formatted version of this faq can be found at http://www.SystemWebMail.com/faq/4.4.8.aspx