PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Jul 02, 2024 9:21 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Email PDFDocument
PostPosted: Thu Aug 13, 2009 1:40 am 
Offline

Joined: Thu Feb 07, 2008 4:58 pm
Posts: 5
Location: Phoenix, AZ
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!!!


Last edited by brendalisa on Fri Aug 21, 2009 2:20 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Email PDFDocument
PostPosted: Fri Aug 14, 2009 3:43 pm 
Offline

Joined: Thu Feb 07, 2008 4:58 pm
Posts: 5
Location: Phoenix, AZ
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!!!


Top
 Profile  
Reply with quote  
 Post subject: Re: Email PDFDocument
PostPosted: Fri Aug 21, 2009 9:23 am 
Offline

Joined: Fri Aug 21, 2009 9:15 am
Posts: 1
That's not your email credentials in your example is it?


Top
 Profile  
Reply with quote  
 Post subject: Re: Email PDFDocument
PostPosted: Fri Aug 21, 2009 2:21 pm 
Offline

Joined: Thu Feb 07, 2008 4:58 pm
Posts: 5
Location: Phoenix, AZ
Of course not.


Top
 Profile  
Reply with quote  
 Post subject: Re: Email PDFDocument
PostPosted: Mon Aug 24, 2009 2:10 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: Email PDFDocument
PostPosted: Wed Aug 26, 2009 7:52 pm 
Offline

Joined: Wed Aug 26, 2009 7:50 pm
Posts: 1
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);


Top
 Profile  
Reply with quote  
 Post subject: Re: Email PDFDocument
PostPosted: Mon Feb 28, 2011 2:07 am 
Offline

Joined: Mon Feb 28, 2011 2:03 am
Posts: 1
Thanks a million mmeliska!

Using:
Stream.Position = 0;

before constructing the attachment object as proposed solved this problem for me :).

Grtz,
Bart


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 59 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group