PDFsharp & MigraDoc Foundation http://forum.pdfsharp.de/ |
|
Email PDFDocument http://forum.pdfsharp.de/viewtopic.php?f=2&t=833 |
Page 1 of 1 |
Author: | brendalisa [ Thu Aug 13, 2009 1:40 am ] |
Post subject: | Email PDFDocument |
I am trying to email a PDF file I create. Here is my current code. It sends the file, but the file has nothing in it. When I open it, the error says: "Adobe Reader could not open because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)." private static void SendEmail(PdfDocument pDoc) { try { MailAddress SendFrom = new MailAddress("brendalisalowe@gmail.com", "Brenda Hatch"); MailAddress SendTo = new MailAddress("brendalisalowe@hotmail.com", "Recepient name"); MailMessage MyMessage = new MailMessage(SendFrom, SendTo); MyMessage.Subject = "HUD Instructions"; MyMessage.Body = "Testing..."; //http://stackoverflow.com/questions/1073277/pdfsharp-save-to-memorystream Document doc = new Document(); DocumentRenderer renderer = new DocumentRenderer(doc); PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(); pdfRenderer.PdfDocument = pDoc; pdfRenderer.DocumentRenderer = renderer; using (MemoryStream ms = new MemoryStream()) { pdfRenderer.Save(ms, false); byte[] buffer = new byte[ms.Length]; ms.Seek(0, SeekOrigin.Begin); ms.Flush(); ms.Read(buffer, 0, (int)ms.Length); //ContentType ct = new ContentType(MediaTypeNames.Application.Pdf); //MyMessage.Attachments.Add(new Attachment(ms, ct)); MyMessage.Attachments.Add(new Attachment(ms, "HUDInstructions.pdf", "application/pdf")); SmtpClient client = new SmtpClient("smtp.gmail.com", 587); client.EnableSsl = true; NetworkCredential myCreds = new NetworkCredential("asdfasdf@gmail.com", "asdfasdfadsf", ""); client.Credentials = myCreds; client.Send(MyMessage); } //litStatus.Text = "Message Sent"; } catch (Exception ex) { //litStatus.Text = ex.ToString(); } When I open the file like this instead of trying to email it, it works great!!! PdfDocument document = (PdfDocument)Session["Document"]; // Send PDF to browser MemoryStream stream = new MemoryStream(); document.Save(stream, false); Response.Clear(); Response.ContentType = "application/pdf"; Response.AddHeader("content-length", stream.Length.ToString()); Response.BinaryWrite(stream.ToArray()); Response.Flush(); stream.Close(); Response.End(); Any ideas? Thanks!!! |
Author: | brendalisa [ Fri Aug 14, 2009 3:43 pm ] |
Post subject: | Re: Email PDFDocument |
Should I try to keep continuing down this road? Or do I need to try another way of doing this? Thanks SO much for your time!!! |
Author: | BigAl [ Fri Aug 21, 2009 9:23 am ] |
Post subject: | Re: Email PDFDocument |
That's not your email credentials in your example is it? |
Author: | brendalisa [ Fri Aug 21, 2009 2:21 pm ] |
Post subject: | Re: Email PDFDocument |
Of course not. |
Author: | Thomas Hoevel [ Mon Aug 24, 2009 2:10 pm ] |
Post subject: | Re: Email PDFDocument |
brendalisa wrote: Should I try to keep continuing down this road? Or do I need to try another way of doing this? In the working sample you only use 2 lines of code to save the PDF to a MemoryStream: // Send PDF to browser MemoryStream stream = new MemoryStream(); document.Save(stream, false); In the case that does not work, you use 20 lines of code from stackoverflow.com to achieve the same task. I suggest using the 2-line-version to get a memory stream. I don't understand which problem shall be avoided by the 20-line-version. The post on stackoverflow.com says there is an issue with document.Save - I never heard of that, but I'll check it eventually. |
Author: | mmeliska [ Wed Aug 26, 2009 7:52 pm ] |
Post subject: | Re: Email PDFDocument |
Use the two line version, but set the stream position to 0 before adding it to the attatchment document.Save(stream, false); stream.Position = 0; Attachment att = new Attachment(stream, filename, System.Net.Mime.MediaTypeNames.Application.Pdf); |
Author: | Bart [ Mon Feb 28, 2011 2:07 am ] |
Post subject: | Re: Email PDFDocument |
Thanks a million mmeliska! Using: Stream.Position = 0; before constructing the attachment object as proposed solved this problem for me ![]() Grtz, Bart |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |