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

Send Exported PDF as Attachment in Email

我正在尝试将 PDF 作为邮件附件发送,但我正在努力寻找路径。我学会了以 PDF 格式导出 Crystal 报告,但我不知道如何在附件中给出路径:

这就是我导出 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 的路径?

您需要将 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);