Q:

Sending e-mail with ASP.NET MVC3 with attachment

I searched everywhere, but without finding any solution.
The problem is: I'm trying to send e-mails with attachments from an ASP.NET MVC3 application. The problem is: the attachment is (or should be) a pdf file created with iTextSharp. I already have a method in a Controller that returns an ActionResult, and this method produces a pdf response. The problem is: how can I get teh file from this ActionResult?

A:

Move your code which generates the PDF fike stream to a method which you can Re Use in many places and use that when creating the attachment
public Byte[] GetGeneratedPDF(string someParameter)
{
   //Do your magic to create the PDF and return the byte array

}

Now you can call this method to create the Attachement
MailMessage msg = new MailMessage();
MemoryStream stream = new MemoryStream(GetGeneratedPDF("hi"));
Attachment att1= new Attachment(stream, "stack123.pdf");
msg.Attachments.Add(att1);

