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);
精彩评论