iTextSharp – 通过电子邮件附件发送内存pdf

我在这里问了几个问题,但仍然有问题。 如果你能告诉我在我的代码中我做错了什么,我将不胜感激。 我从ASP.Net页面运行上面的代码,并得到“无法访问封闭的stream”。

var doc = new Document(); MemoryStream memoryStream = new MemoryStream(); PdfWriter.GetInstance(doc, memoryStream); doc.Open(); doc.Add(new Paragraph("First Paragraph")); doc.Add(new Paragraph("Second Paragraph")); doc.Close(); //if I remove this line the email attachment is sent but with 0 bytes MailMessage mm = new MailMessage("username@gmail.com", "username@gmail.com") { Subject = "subject", IsBodyHtml = true, Body = "body" }; mm.Attachments.Add(new Attachment(memoryStream, "test.pdf")); SmtpClient smtp = new SmtpClient { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, Credentials = new NetworkCredential("username@gmail.com", "my_password") }; smtp.Send(mm); //the "Cannot Access a Closed Stream" error is thrown here 

谢谢!!!

编辑:

只是为了帮助有人寻找这个问题的答案,发送附加到电子邮件的PDF文件的代码,而不必物理创build文件是(感谢Ichiban和Brianng):

 var doc = new Document(); MemoryStream memoryStream = new MemoryStream(); PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream); doc.Open(); doc.Add(new Paragraph("First Paragraph")); doc.Add(new Paragraph("Second Paragraph")); writer.CloseStream = false; doc.Close(); memoryStream.Position = 0; MailMessage mm = new MailMessage("username@gmail.com", "username@gmail.com") { Subject = "subject", IsBodyHtml = true, Body = "body" }; mm.Attachments.Add(new Attachment(memoryStream, "filename.pdf")); SmtpClient smtp = new SmtpClient { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, Credentials = new NetworkCredential("username@gmail.com", "password") }; smtp.Send(mm); 

你有没有尝试过:

 PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream); // Build pdf code... writer.CloseStream = false; doc.Close(); // Build email memoryStream.Position = 0; mm.Attachments.Add(new Attachment(memoryStream, "test.pdf")); 

如果我的记忆正确地为我服务,这解决了以前项目中类似的问题。

请参阅http://forums.asp.net/t/1093198.aspx

我尝试了由brianng发布的代码,它工作。 只要将代码的最上面改为:

 var doc = new Document(); MemoryStream memoryStream = new MemoryStream(); PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream); //capture the object doc.Open(); doc.Add(new Paragraph("First Paragraph")); doc.Add(new Paragraph("Second Paragraph")); writer.CloseStream = false; //set the closestream property doc.close(); //close the document without closing the underlying stream memoryStream.Position = 0; /* remainder of your code stays the same*/ 

你可以刷新文档或内存stream,然后closures它,你附加后?

大概调用doc.Close()处理底层的stream。 尝试删除doc.Close(),而不是该行设置memoryStream.Position = 0;

或者,您可以使用临时文件:

 var tempFilePath = Path.GetTempFileName(); try { var doc = new Document(); PdfWriter.GetInstance(doc, File.OpenWrite(tempFilePath)); doc.Open(); doc.Add(new Paragraph("First Paragraph")); doc.Add(new Paragraph("Second Paragraph")); doc.Close(); MailMessage mm = new MailMessage("username@gmail.com", "username@gmail.com") { Subject = "subject", IsBodyHtml = true, Body = "body" }; mm.Attachments.Add(new Attachment(tempFilePath, "test.pdf")); SmtpClient smtp = new SmtpClient { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, Credentials = new NetworkCredential("username@gmail.com", "my_password") }; smtp.Send(mm); } finally { File.Delete(tempFilePath); } 

我有同样的问题,我用这个post来解决它在brianng写的代码

 PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream); // Build pdf code... writer.CloseStream = false; doc.Close(); // Build email memoryStream.Position = 0; mm.Attachments.Add(new Attachment(memoryStream, "test.pdf")); 

我认为,而不是写作

 writer.CloseStream = false and memoryStream.Position = 0; 

只需创build一个新的stream

 MemoryStream m = new MemoryStream(memoryStream); 

然后打电话

 mm.Attachments.Add(new Attachment(memoryStream, "test.pdf")); 

这两个工作,但我认为创build新的stream更好