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.