ABCpdf将Doc附加到电子邮件
问题描述:
我已经使用ABDpdf来渲染PDF并将其传输到浏览器,但是我想知道是否可以将渲染的PDF附加到电子邮件中。有没有人曾经这样做过?ABCpdf将Doc附加到电子邮件
我希望有一种方法,不需要我将PDF保存到临时目录,然后附加文件,然后将其删除。
答
根据ABCpdf PDF支持网站上的文档,Doc()对象支持保存到流的重载。使用此功能,您可以将结果保存为生成的PDF,而无需使用MemoryStream类显式写入磁盘。
ABCpdf PDF Component for .NET : Doc.Save()
MemoryStream (System.IO) @ MSDN
随着创建一个MemoryStream,你便可以传递到支持来自流产生任何附件的电子邮件提供商流。 System.Net.Mail中的MailMessage支持这一点。
MailMessage Class (System.Net.Mail) @ MSDN
MailMessage.Attachments Property @ MSDN
Attachments Class @ MSDN
Attachments Constructor @ MSDN
最后,如果你以前从未使用过的类MAILMESSAGE,使用SmtpClient类发送邮件的道路上。
答
Meklarian是正确的,但有一点要指出的是,您保存的PDF到您的流之后,你会想回来重置流位置为0。否则附件是发送将是所有foo禁止。
(我花了大约两个小时来弄明白。哎哟,希望能帮助别人节省一些时间。)
//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);
+1 ......我忘了复位在其它场景流位置,以及。 – meklarian 2010-05-21 19:24:05
+1 ..我只花了一个小时试图弄清为什么我的文件总是0字节。 – philwilks 2011-01-03 17:05:05
感谢您发表您的答案。 – 2015-11-21 16:08:06