views:

64

answers:

2

I am currently working a piece of code which needs to send an attachment in the email it generates. The attachment is a PDF document. Due to requirements I am unable to save the PDF and send it, so I have had to create a memory stream attachment.

The issue I have with this is that the file size is _500KB. However save the file on my machine and it is around 370KB.

This increase in file size is unacceptable. has anyone come across this issue before? If so how have they got round the problem.

Below is the section of code. '

Dim memStream As System.IO.MemoryStream = Nothing
'assign number to the PDF name
Dim filename As String = req.GetAccountNumber()
'add file extension
filename &= ".pdf"

'initialise the memory stream
memStream = New System.IO.MemoryStream()

'generate the pdf and put it in a byte array
Dim arrBytData() As Byte = CType(PDFRequest(),Byte()))

'flush the stream
memStream.Flush()

'Create new instances of the message and attachments
'and intialise them
Dim msg As New System.Net.Mail.MailMessage(req.EmailFrom, req.EmailTo)
Dim att As New System.Net.Mail.Attachment(memStream, filename)

With msg
     .Attachments.Add(att)
     .Body = req.EmailBody
     .Subject = req.EmailSubject
End With

'connect to the server and send the message
Dim client As New System.Net.Mail.SmtpClient()
client.Host = PDFService(Of T).mSMTPServer
client.Send(msg)

'Close our stream
memStream.Close()
msg = Nothing
+4  A: 

The problem is that the attachment has to be encoded with Base64- raw binary data won't "survive" in e-mail, so it has to be encoded to a form that only uses certain "safe" characters. This increases the size- raw binary has 256 "characters", while the Base64 encoding only has 64. Not really any way around it.

nitzmahone
Wow, I used a "lot" of quotes in that answer. Sorry. :)
nitzmahone
That's ok, my browser has plenty of quotes left in its bag-of-glyphs ;)
Niklas
Obviously not the answer I was hoping for, but that does explain the increase in the size.
Miker169
+2  A: 

The increase is probably due to the base64 encoding of the binary data when it is attached to the email. AFAIK there is nothing you can do about this.

Thomas Lötzer