开发者

How to send generated pdf file as attachment in email from C#?

i have code that generates a pdf file and i want to send it as an attachment to an email .

I have this code:

        FileContentResult fileContentResult = File(fileName, "application/pd开发者_StackOverflow社区f", "file.pdf");

and i have this code to email an attachment

        Attachment a = new Attachment();
        sender.SendMail("a@a.com", "a@a.com", "subject", "Body", a);

how do i convert a FileContentResult into a Attachment object?


Have you tried using this: Attachment Constructor (Stream, ContentType)?

Something like

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


I am just 7 years late, but here is your answer:

Attachment a = new Attachment(fileName, "application/pdf");
sender.SendMail("a@a.com", "a@a.com", "subject", "Body", a);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜