我使用 ABDpdf 呈现 pdf 并将其流式传输到浏览器,但我想知道是否可以将呈现的 pdf 附加到电子邮件中。有没有人这样做过?
我希望有一种方法不需要我将pdf保存到临时目录然后附加文件,然后将其删除。
我使用 ABDpdf 呈现 pdf 并将其流式传输到浏览器,但我想知道是否可以将呈现的 pdf 附加到电子邮件中。有没有人这样做过?
我希望有一种方法不需要我将pdf保存到临时目录然后附加文件,然后将其删除。
Meklarian 是对的,但需要指出的一点是,在将 pdf 保存到流中后,您需要将流位置重置回 0。否则,发送的附件将全部被禁止。
(我花了大约两个小时才弄清楚。哎呀。希望能帮助别人节省一些时间。)
//Create the pdf doc
Doc theDoc = new Doc();
theDoc.FontSize = 12;
theDoc.AddText("Hello, World!");
//Save it to the Stream
Stream pdf = new MemoryStream();
theDoc.Save(pdf);
theDoc.Clear();
//Important to reset back to the begining of the stream!!!
pdf.Position = 0;
//Send the message
MailMessage msg = new MailMessage();
msg.To.Add("you@you.com");
msg.From = new MailAddress("me@me.com");
msg.Subject = "Hello";
msg.Body = "World";
msg.Attachments.Add(new Attachment(pdf, "MyPDF.pdf", "application/pdf"));
SmtpClient smtp = new SmtpClient("smtp.yourserver.com");
smtp.Send(msg);
根据 ABCpdf PDF 支持站点上的文档,支持保存到流的 Doc() 对象有一个重载。使用此功能,您可以将结果保存为生成的 PDF,而无需使用 MemoryStream 类显式写入磁盘。
.NET 的 ABCpdf PDF 组件:Doc.Save()
MemoryStream (System.IO) @ MSDN
创建 MemoryStream 后,您可以将流传递给任何支持从流创建附件的电子邮件提供商。System.Net.Mail 中的 MailMessage 对此有支持。
MailMessage 类(System.Net.Mail)
@MSDN MailMessage.Attachments 属性@MSDN
附件类@MSDN
附件构造函数@MSDN
最后,如果您以前从未使用过 MailMessage 类,请使用 SmtpClient 类在途中发送您的消息。