0

I am using Rotativa. MVC to generate a PDF from a view, so I can send an email with the PDF as an attachment. The following code does so:

var p = ControllerContext;
var h = new ViewAsPdf("Index") { FileName = "TestViewAsPdfHm.pdf" }; 
var hmm = h.BuildPdf(p); 
var memStream2 = new SysIO.MemoryStream(hmm);
MailMessage mm = new MailMessage("From@gmail.com", "To@gmail.com");
mm.IsBodyHtml = true;
mm.Attachments.Add(new Attachment(memStream2, "BuildPdfOption.pdf"));

My question is "How do I obtain a PDF file saved i.e. in my MVC application's Content/Pdf folder, from within in c# code, so I can attach it to an email in the same manner?" I have tried many times with file,stream readers and with HttpContext.Server.MapPath, but without success. Using the MapPath option just had a textual document in the email that I couldn't open.

Abdullah Ilgaz
  • 719
  • 1
  • 17
  • 39
chri3g91
  • 1,196
  • 14
  • 16
  • `Server.MapPath("~/Content/Pdf/yourFileName.pdf")` ? http://stackoverflow.com/questions/17680941/using-smtpclient-to-send-a-file-attachment – Shyju Feb 13 '16 at 16:22
  • Yea the "Content Type" helped, thanks. Just add the code: ContentType myType = new ContentType { MediaType = MediaTypeNames.Application.Pdf, Name = "MyFileName" }; mm.Attachments.Add(new Attachment ("fileName", myType)); – chri3g91 Feb 16 '16 at 16:04

0 Answers0