been struggling with this and have gotten so close, but not quite there. I am trying to use iTextSharp V 4.1.2 to send a PDF as an email attachment. The code I provided below is using both filestream and memorystream, filestream is there to prove to myself that the PDF template is being stamped with data (it is). Also, I am receiving the email, but without an attachment. Can anyone see what I'm missing please?
public class BasePDFController : BaseController
{ protected ActionResult eMailPdf(object model) { string pdf = Path.Combine(Server.MapPath("~/Infrastructure/PDFTemplates/fw9.pdf")); string outputFilePath = @"C:\Projects\Temp\test_template_filled.pdf";
MemoryStream memoryStream = new MemoryStream();
PdfReader pdfFileReader = null;
PdfReader pdfMemoryReader = null;
try
{
pdfFileReader = new PdfReader(pdf);
pdfMemoryReader = new PdfReader(pdf);
using (FileStream pdfOutputFile = new FileStream(outputFilePath, FileMode.Create))
{
PdfStamper pdfFileStamper = null;
PdfStamper pdfMemoryStamper = null;
try
{
pdfFileStamper = new PdfStamper(pdfFileReader, pdfOutputFile);
pdfMemoryStamper = new PdfStamper(pdfMemoryReader, memoryStream);
AcroFields acroFileFields = pdfFileStamper.AcroFields;
AcroFields acroMemoryFields = pdfMemoryStamper.AcroFields;
acroFileFields.SetField("topmostSubform[0].Page1[0].f1_01_0_[0]", "Batman");
pdfFileStamper.FormFlattening = true;
pdfMemoryStamper.FormFlattening = true;
pdfMemoryStamper.Writer.CloseStream = false;
if (pdfMemoryStamper != null)
{
pdfMemoryStamper.Close();
}
memoryStream.Position = 0;
EmailProvider.Email email = new EmailProvider.Email();
email = new EmailProvider.Email
{
To = "[email protected]",
Subject = "Scholars Attached PDF",
Body = "A PDF!",
Attachment = new Attachment(memoryStream, new System.Net.Mime.ContentType("application/pdf"))
};
EmailProvider.SendEmail(email);
}
finally
{
if (pdfFileStamper != null)
{
pdfFileStamper.Close();
}
}
}
}
finally
{
pdfFileReader.Close();
pdfMemoryReader.Close();
}
return File(outputFilePath, "application/pdf", "Returned.pdf");
}
}
EDIT: - yes, there is a problem with the mail provider. I don't know how to fix it though, or if this is even the best way to go about sending? The person who wrote this obviously gave up. EDIT Fixed the attachment.
public static class EmailProvider
{
public class Email
{
public String To { get; set; }
public String Subject { get; set; }
public String Body { get; set; }
**public Attachment Attachment { get; set; }**
}
public static void SendEmail(Email email)
{
MailMessage mail = new MailMessage();
mail.To.Add(email.To);
mail.Subject = email.Subject;
mail.Body = email.Body;
mail.IsBodyHtml = true;
mail.Attachments.Add(email.Attachment);
SmtpClient smtp = new SmtpClient();
smtp.Send(mail);
}
}
With the email fixed, I am receiving the PDF! However, when I try to open the PDF from email I get an error: There was an error opening this document. The file is damaged and could not be repaired. Ideas?
EmailProvider.Email
orEmailProvider.SendEmail
. Post that code if you can. – Jeremy Cook