Sending PDF as EmailAttachment


I’m attempting to send an email with a PDF as an attachement from a CGI WE app, but I’m running into some problems when it shows up in my inbox. I’m using the MBS DynaPDF plugin to generate the PDF files on the fly. Basically after the PDF is done rendering, I’m trying to attach it as below:

    dim pdf As New MyDynaPDFMBS
    CreatePDF pdf 'renders the PDF
    CurrentFile = new WebFile
    CurrentFile.Filename = "Proposal_" + ProposalActionContainer1.lblPropNum.Text + ".pdf"
    CurrentFile.MIMEType = "application/pdf"
    CurrentFile.Data = pdf.GetBuffer
    CurrentFile.ForceDownload = true
    ShowURL(CurrentFile.URL) ' downloads the file - this looks fine
    Dim mail As EmailMessage
    Dim file As New EmailAttachment
    file.Name = "Proposal_" + ProposalActionContainer1.lblPropNum.Text + ".pdf"
    file.MIMEType = "application/pdf"
    file.Data = pdf.GetBuffer

    dim mailServer1 as SMTPSecureSocket = Session.ProposalList.mailServer1
    Session.ProposalList.currPropView = self
    pwEmail.Visible = True
    pwEmail.Enabled = True
    mailServer1.Address = "xxxxxxxxxx"
    mailServer1.Port = xxxxx
    mailServer1.Username = "xxxxxxx"
    mailServer1.Password = "xxxxxxxxxxx"
    mail = New EmailMessage
    mail.FromAddress = ""
    mail.Subject = "Test subject"
    mail.BodyPlainText = "testing plain text"
    mail.Headers.AppendHeader "X-Mailer","ProposalSYS SMTP"
    mail.Attachments.Append file
    mail.AddRecipient ""
    mailServer1.Messages.Append mail

Note that the variable pdf is the MBS pdf object - when I download it as a Webfile it looks just fine. When sent as an attachment all the images are distorted, fonts aren’t correct, most of the page isn’t rendered. I noticed that pdf.GetBuffer returns a MemoryBlock where as file.Data is expecting a String. Looking around for solutions on this, it seems like doing the assignment should work okay. I’m just wondering if I have to specify an encoding or do anything else to the attachment before appending it to the mail message.

Like I said, the Webfile works great so I know the PDF is being rendered just fine.

Any help is greatly appreciated. Maybe I’m doing this totally the wrong way - it would be great to somehow just use the Webfile as the attachment directly.


  • Dustin

I think you need an encoding here for the attachment.
EncodeBase64 for the data from pdf.GetBuffer and than
file.ContentEncoding = “base64”

We have joy! Attachment looks perfect now.

Thank you Christian!

Excellent. Thanks (Y)