将导出的PDF作为附件发送到电子邮件中

时间:2015-07-31 10:51:29

标签: vb.net email crystal-reports export-to-pdf

我正在尝试将PDF作为附件发送到邮件中,但我正在努力找出应该是什么路径。我学会了以PDF格式导出Crystal Report,但我不知道如何在附件中提供路径:

这就是我导出PDF的方式

Dim rptDocument As ReportDocument = New ReportDocument()
rptDocument.Load(mReportPath)
Dim exportOpts As ExportOptions = New ExportOptions()
Dim pdfOpts As PdfRtfWordFormatOptions = ExportOptions.CreatePdfRtfWordFormatOptions()
exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat
exportOpts.ExportFormatOptions = pdfOpts
rptDocument.ExportToHttpResponse(exportOpts, Response, True, "")

这是通过电子邮件发送pdf的代码:

Dim msg As New MailMessage()
msg.From = New MailAddress("proccoinvoice@gmail.com")
msg.[To].Add(recipient)
msg.Subject = "Procco Invoice"
msg.Body = "Invoice attached"
msg.Attachments.Add(New Attachment(filepath)) //Path should be given here
Dim client As New SmtpClient("smtp.gmail.com")
client.Port = 25
client.Credentials = New NetworkCredential("proccoinvoice@gmail.com", "<Procco>;1947")
client.EnableSsl = True
client.Send(msg)

我的问题是如何提供附件中运行时生成的PDF的路径?

1 个答案:

答案 0 :(得分:0)

您需要将PDF文件转换为byte[]以作为邮件中的附件发送。

请检查以下代码。

byte[] pdfarry = null;

using (MemoryStream ms = new MemoryStream())
{
  document.Save(ms, false);
  document.Close();

  pdfarry = ms.ToArray();
}


mailMessage.Attachments.Add(new Attachment(pdfarry, "testPDF.pdf", "application/pdf"));

smtpClient = new SmtpClient("xxxx");
smtpUserInfo = new System.Net.NetworkCredential("xxxx", "xxx", xxx");
smtpClient.Credentials = smtpUserInfo;
smtpClient.UseDefaultCredentials = false;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

smtpClient.Send(mailMessage);