我有生成pdf文件的代码,我想将其作为电子邮件的附件发送。
我有这段代码:
FileContentResult fileContentResult = File(fileName, "application/pdf", "file.pdf");
我将此代码通过电子邮件发送附件
Attachment a = new Attachment();
sender.SendMail("a@a.com", "a@a.com", "subject", "Body", a);
如何将 FileContentResult 转换为附件对象?
答案 0 :(得分:10)
您是否尝试过使用此功能:Attachment Constructor (Stream, ContentType)?
像
这样的东西MemoryStream ms = new MemoryStream(fileContentResult.FileContents);
// Create an in-memory System.IO.Stream
ContentType ct = new ContentType(fileContentResult.ContentType);
Attachment a = new Attachment(ms, ct);
答案 1 :(得分:1)
我只是迟到了7年,但这是你的回答:
Attachment a = new Attachment(fileName, "application/pdf");
sender.SendMail("a@a.com", "a@a.com", "subject", "Body", a);