PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream) //capture the object Just change the top of the code to this: var doc = new Document() I tried the code posted by brianng and it worked. If my memory serves me correctly, this solved a similar problem in a previous project. Have you tried: PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream) Mm.Attachments.Add( new Attachment(memoryStream, "filename.pdf")) Ĭredentials = new NetworkCredential( "password") PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream) 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() Smtp.Send(mm) //the "Cannot Access a Closed Stream" error is thrown here Mm.Attachments.Add( new Attachment(memoryStream, "test.pdf")) Ĭredentials = new NetworkCredential( "my_password") MailMessage mm = new MailMessage( = "subject", PdfWriter.GetInstance(doc, memoryStream) ĭoc.Add( new Paragraph( "First Paragraph")) ĭoc.Add( new Paragraph( "Second Paragraph")) ĭoc.Close() //if I remove this line the email attachment is sent but with 0 bytes MemoryStream memoryStream = new MemoryStream() I run the code above from a ASP.Net page and get "Cannot Access a Closed Stream". I'd appreciate if you could tell me what I am doing wrong in my code. I've asked a couple of questions here but am still having issues.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |