简体   繁体   中英

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?

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);

只需采用在“ PDF生成操作方法”中使用的相同逻辑,然后将Stream (或iTextSharp返回的任何内容)添加为邮件的附件:

message.Attachments.Add(new Attachment(stream, "name", "mimeType"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM