I have a function that accepts a string and generates an email attachment based on that string. It works fine for html pages, text documents, and so forth but I can not get it to generate a PDF file.
Code:
Public Sub SendMail _
( _
ByVal strFrom As String _
, ByVal strTo As String _
, ByVal strSubject As String, ByVal strBody As String _
, Optional ByVal attachment As String = "" _
, Optional ByVal filename As String = "Attachment.HTML" _
)
Dim sendMail As New SmtpClient
Dim mail As New MailMessage(strFrom, strTo)
Dim userToken As New Object
mail.Subject = strSubject
mail.Body = strBody
mail.IsBodyHtml = True
Using MemoryStream = New MemoryStream
If attachment.Length <> 0 Then
Dim data As Byte() = Encoding.ASCII.GetBytes(attachment)
MemoryStream.Write(data, 0, data.Length)
MemoryStream.Seek(0, SeekOrigin.Begin)
MemoryStream.Position = 0
Dim content As New ContentType()
content.MediaType = MediaTypeNames.Application.Octet
content.Name = filename
Dim newAttach As New Attachment(MemoryStream, content)
mail.Attachments.Add(newAttach)
End If
sendMail.DeliveryMethod = SmtpDeliveryMethod.Network
sendMail.Host = "SERVER"
sendMail.UseDefaultCredentials = False
sendMail.Credentials = New System.Net.NetworkCredential("UN", "PW")
sendMail.Send(mail)
End Using
If I save the file attachment as Whatever.PDF i get the error that it was not encoded properly. Not sure what I need to do to have this work with PDF, have tried searching google but not finding anything that helps me.
EDITED:
I am using datadynamics active reports PDF exporter to generate the PDF
dim pdf as new datadynamics.activereports.export.pdf.pdfexport
sendmail("from@", "to@", "test", "test", pdf.tostring, "pdf.pdf")
I think the problem is I am converting the PDF to a string, and then trying to convert it back to a PDF and attach it to the email but I am not 100% sure.
Thanks.
I’m not familiar with this reporting technology, but according to documentation you should use something like this:
and build
MemoryStreamfrompdfarray or add attachment fromtmpfile.