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

leo*_*ora 6 c# email

我有生成pdf文件的代码,我想将其作为电子邮件的附件发送.

我有这个代码:

        FileContentResult fileContentResult = File(fileName, "application/pdf", "file.pdf");
Run Code Online (Sandbox Code Playgroud)

我有这个代码通过电子邮件发送附件

        Attachment a = new Attachment();
        sender.SendMail("a@a.com", "a@a.com", "subject", "Body", a);
Run Code Online (Sandbox Code Playgroud)

如何将FileContentResult转换为Attachment对象?

小智 10

您是否尝试过使用此选项:附件构造函数(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);
Run Code Online (Sandbox Code Playgroud)