3

I have a PDF being generated by PDFTK (PDFToolkit). I am trying to add the ReturnedPDF which gets saved to the downloads folder to a MailMessage in C#.

The PDF downloads with no problem; however, attaching isn't going so well.

var pdfContents = PDFHelper.GeneratePDF(pdfPath, formFieldMap);

var newpdffer = PDFHelper.ReturnPDF(pdfContents, "100-" + genUUID.Text + ".pdf");

Returns my PDF filled in just fine (as a downloaded file)... however, I can't seem to figure out how to attach it in MemoryStream. I am simply trying to attach it and email it.

I receive the email - but I get an attachment with nothing in it. SEE MY CODE BELOW:

    var doc = new Document();
    MemoryStream memoryStream = new MemoryStream();
    PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);

    doc.Open();

    //doc.Add(new Attachment(thispdfnew));
    //doc.Add(new Paragraph("Second Paragraph"));

    writer.CloseStream = false;

    doc.Close();
    memoryStream.Position = 0;

    string toemailaddress = email.Text;
    string youremailadd = youremail.Text;

    MailMessage mm = new MailMessage();
    mm.To.Add(new MailAddress(youremail.Text));

    MailAddress addressBCC = new MailAddress(email.Text);
    mm.Bcc.Add(addressBCC);

    mm.Subject = "New form Completed "+Preparedby.Text;
    mm.Body = "To view the message, please use an HTML compatible email viewer!";

    mm.Attachments.Add(new Attachment(memoryStream, "100-" + genUUID.Text + ".pdf"));
    SmtpClient client = new SmtpClient();
    client.Port = 587;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential("postmaster@this.com", "343543");
    client.Host = "smtp.mailchimp.org";


    client.Send(mm);

I've spent the last day googling and trying to figure out how to get the generated PDF into the Mailstream message but can't seem to figure it out. I am using PDFTK which uses iTextSharp to parse the PDF.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
MizAkita
  • 1,115
  • 5
  • 21
  • 52
  • Have you intentionally left out the part of code where you load a pdf document or write to `doc`? Have you checked to see if the memory stream has anything in it `memoryStream.GetBuffer()`? – Mike Hixson May 11 '15 at 22:26
  • I wasnt exactly sure on how to use the add.doc function... do i need it to generate from MemoryStream? I commented it out because it was giving me a blank page. – MizAkita May 11 '15 at 22:44
  • Do you already have the pdf saved on disk as a file? – Mike Hixson May 11 '15 at 22:52
  • Yes it saves to my servers c:// drive. – MizAkita May 11 '15 at 23:18
  • 1
    If you are saving the output of iTextSharp (or PDFTK) to disk then at that exact moment you have PDF as a file and you can ignore iTextSharp. Just [add the file to your `MailMessage` as you would](http://stackoverflow.com/a/5034554/231316). Ideally you'd bypass the disk completely using a `Stream` for the entire process but I'd just start with this – Chris Haas May 12 '15 at 02:08
  • thats what im saying... i dont know how to get the generatedPDF (as mentioned above) as an attachment to mail. - i need: PDFHelper.ReturnPDF(pdfContents, "100-" + genUUID.Text + ".pdf"); attached to mail. – MizAkita May 12 '15 at 03:30
  • 2
    You say you are saving a file to disk, right? I assume you know the file's name then, too, right? The link I posted shows you how to load an attachment from disk using an absolute file path. If you are already writing to disk in the first place, trying to use a `MemoryStream` or any other `Stream` for that matter will just add complexity, so don't bother with it. So just take the absolute file name and use the on-liner for adding attachments from the link above. – Chris Haas May 12 '15 at 14:14
  • @ChrisHaas: You should add a proper answer, based on your comments... – Kurt Pfeifle May 23 '15 at 23:13

0 Answers0