Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iTextSharp - Sending in-memory pdf in an email attachment

I've asked a couple of questions here but am still having issues. I'd appreciate if you could tell me what I am doing wrong in my code. I run the code above from a ASP.Net page and get "Cannot Access a Closed 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("[email protected]", "[email protected]") {     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("[email protected]", "my_password") };  smtp.Send(mm); //the "Cannot Access a Closed Stream" error is thrown here 

Thanks!!!

EDIT:

Just to help somebody looking for the answer to this question, the code to send a pdf file attached to an email without having to physically create the file is below (thanks to Ichiban and 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("[email protected]", "[email protected]") {     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("[email protected]", "password")  };  smtp.Send(mm); 
like image 565
Gus Cavalcanti Avatar asked Jul 28 '09 18:07

Gus Cavalcanti


People also ask

What is the use of iTextSharp DLL?

What is ITextSharp? iTextSharp is a free and open source assembly that helps to convert page output or HTML content in a PDF file. Now add that DLL in the application. Getting Started: Start Visual Studio and create a new website in ASP.Net and add these 2 DLLs to the solution.

What is chunk in iTextSharp?

A Chunk is the smallest significant piece of text that you can work with.


2 Answers

Have you tried:

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")); 

If my memory serves me correctly, this solved a similar problem in a previous project.

See http://forums.asp.net/t/1093198.aspx

like image 67
brianng Avatar answered Oct 03 '22 09:10

brianng


I tried the code posted by brianng and it worked. Just change the top of the code to this:

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*/ 
like image 43
ichiban Avatar answered Oct 03 '22 09:10

ichiban