我有一个接受字符串的函数,并根据该字符串生成电子邮件附件。它适用于html页面,文本文档等,但我不能让它生成PDF文件。
代码:
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
如果我将文件附件保存为Whatever.PDF,则会收到错误消息,表明它未正确编码。不知道我需要做些什么来使用PDF,尝试搜索谷歌但没有找到任何帮助我。
EDITED :
我正在使用datadynamics活动报告PDF导出器来生成PDF
dim pdf as new datadynamics.activereports.export.pdf.pdfexport
sendmail("from@", "to@", "test", "test", pdf.tostring, "pdf.pdf")
我认为问题在于我将PDF转换为字符串,然后尝试将其转换回PDF并将其附加到电子邮件中,但我并非百分百确定。
感谢。
答案 0 :(得分:1)
我不熟悉这种报告技术,但根据documentation你应该使用这样的东西:
DataDynamics.ActiveReports.Export.Pdf.PdfExport p = new DataDynamics.ActiveReports.Export.Pdf.PdfExport();
string tmp = Path.GetTempFileName();
p.Export(rpt.Document, tmp, "1-10000");
byte[] pdf = File.ReadAllBytes(tmp);
File.Delete(tmp);
并从MemoryStream
数组构建pdf
或从tmp
文件添加附件。