SMTP.WS Microsite - Microsoft SMTP Service

Welcome to SMTP.WS

“Providing information about Microsoft SMTP Service”

System.Web.Mail and PDF getting corrupted workaround

I was troubleshooting an issue where I was sending an email using .NET 1.1 and attaching PDF files. The PDF files where getting an error when trying to open the attachment.  Here is what was displayed in Adobe reader.  "There was an error processing a page. A file I/O error has occurred".  Thanks a bunch to Dave Wanta, creator of http://ASPNetEmail.com for providing a great resource about System.Web.Mail ( http://www.systemwebmail.com ).  A similar example worked in ASP.NET 2.0, comparing email headers provided the clues it was how the attachment was getting encoded.  I found the answer in the article listed below.

This article explains it.
http://systemwebmail.com/faq/4.4.8.aspx 

The bolded code is what you have to alter

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Mail" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

protected void Page_Load(object sender, EventArgs e)
    {

    string FilePath = Request.PhysicalApplicationPath + "/c.pdf";
    MailMessage msgMail = new MailMessage();

    msgMail.To = "steve@changethisBefore.com";
    msgMail.From = "steve@changethisBefore.com";
    msgMail.Subject = "Your Favorite Subject";
    msgMail.Body = @"Some Text goes here";
    msgMail.Attachments.Add(new MailAttachment(FilePath, MailEncoding.UUEncode));
    SmtpMail.Send(msgMail);
    Response.Write("Thank you."); 
}


</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>

Hope this helps

Steve Schofield
Microsoft MVP - IIS