How to send generated PDF as email attachment with C #?

I have a code that generates a PDF file and I want to send it as an attachment in an email.

I have this code:

        FileContentResult fileContentResult = File(fileName, "application/pdf", "file.pdf");

and I have this code to send an attachment

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

How do I convert a FileContentResult to an Attachment ?

+5
source share
2 answers

Have you tried to use this: Attachment Constructor (stream, ContentType) ?

Sort of

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);
+8
source

I am only 7 years old, but here is your answer:

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

All Articles