ABCpdf 将文档附加到电子邮件

发布于 2024-08-07 02:47:33 字数 121 浏览 9 评论 0原文

我已经使用 ABDpdf 渲染 pdf 并将其流式传输到浏览器,但我想知道是否可以将渲染的 pdf 附加到电子邮件中。有人这样做过吗?

我希望有一种方法不需要我将 pdf 保存到临时目录然后附加文件,然后删除它。

I've used ABDpdf to render a pdf and stream it to the browser, but I'm wondering if I can attach the rendered pdf to an email. Has anyone ever done that?

I'm hoping there is a way that doesn't require me to save the pdf to a temp directory then attach the file, then delete it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终止放荡 2024-08-14 02:47:33

Meklarian 是对的,但需要指出的一点是,将 pdf 保存到流中后,您需要将流位置重置回 0。否则发送的附件将全部被 foo-barred。

(我花了大约两个小时才弄清楚。哎呀。希望能帮助其他人节省一些时间。)

    //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("[email protected]");
    msg.From = new MailAddress("[email protected]");
    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);

Meklarian is right, but one little thing to point out, is that after you've saved the pdf into your stream, you will want to reset your stream position back to 0. Otherwise the attachment that is sent will be all foo-barred.

(It took me about two hours to figure it out. Ouch. Hope to help someone else save some time.)

    //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("[email protected]");
    msg.From = new MailAddress("[email protected]");
    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);
落在眉间の轻吻 2024-08-14 02:47:33

根据 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 类来发送消息。

SmtpClient 类 (System.Net .邮件)

According to the documentation on the ABCpdf PDF support site, there is an overload of the Doc() object that supports saving to a stream. With this feature, you can save the results a generated PDF without explicitly writing to disk by using the MemoryStream class.

ABCpdf PDF Component for .NET : Doc.Save()
MemoryStream (System.IO) @ MSDN

With a MemoryStream created, you may then pass the stream on to any email provider that supports creating attachments from a stream. MailMessage in System.Net.Mail has support for this.

MailMessage Class (System.Net.Mail) @ MSDN
MailMessage.Attachments Property @ MSDN
Attachments Class @ MSDN
Attachments Constructor @ MSDN

Finally, if you've never used the MailMessage class before, use the SmtpClient class to send your message on its way.

SmtpClient Class (System.Net.Mail)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文