如何将生成的pdf文件作为附件发送到C#的电子邮件中?

时间:2010-07-20 21:43:30

标签: c# email

我有生成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 转换为附件对象?

2 个答案:

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