We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Corrupted attachments via MemoryStream

Hello,

I am using the latest versions of Aspose.Network (4.1.0.0) and Aspose.Pdf (3.7.0.0). I am attempting to attach a Pdf document to a mail message using a MemoryStream object. The email sends and arrives just fine, but the attachment will not open. Adobe Reader tells me that the Pdf is corrupted. Here's what I am doing.

Dim myPdf as New Aspose.Pdf.Pdf

generatePdf(myPdf) 'Passed ByRef

Dim ms As New System.IO.MemoryStream

myPdf.Save(ms)

Dim message as New Aspose.Network.Mail.MailMessage
message.From.Add("from email")
message.To.Add("to email")
message.Subject = "MemoryStream Attachment"
message.HtmlBody = "

Here is the file.

"
message.AddAttachment(New Aspose.Mail.Attachment(ms))

Dim smtp As New Aspose.Mail.SmtpClient("my host")
smtp.AuthenticationMethod = Aspose.Mail.SmtpAuthentication.None
smtp.Send(message)

If I save the Pdf to file and then attach the file, the Pdf is not corrupted. Please fix this ASAP, I have a client waiting on this.

Thank you!

Dan

Hi Dan,

Before adding the attachment, please set the MemoryStream.Position to 0, so that the attachment could be added to the email message.

' add the following line before adding the attachment

ms.Position = 0

message.AddAttachment(New Aspose.Mail.Attachment(ms))

Thanks Saqib, that worked. Since the Aspose.Pdf Save() method gets passed the MemoryStream ByRef, couldn't your Save method also reset the position to 0 to avoid this error?

Thanks again,

Dan