tags:

views:

90

answers:

1

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/pdf", "file.pdf");

and i have this code to email an attachment

        Attachment a = new Attachment();
        sender.SendMail("[email protected]", "[email protected]", "subject", "Body", a);

how do i convert a FileContentResult into a Attachment object?

+1  A: 

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

Something like

MemoryStream ms = new MemoryStream(stream.FileContents); 
// Create an in-memory System.IO.Stream

ContentType ct = new ContentType(stream.ContentType);

Attachement a = new Attachment(ms, ct);

btw, the name stream for the FileContentResult is confusing. I suggest you change it.

Moron
@Moron - this code doesn't compile. i have renamed that variable above. this line is not recognized: ContentType ct = stream.ContentType;
ooo
@ooo: You need `using System.IO`, do you have that? Also edited code as stream.ContentType was apparently string.
Moron
@Moron - this line is not recognized: ContentType ct = stream.ContentType;
ooo
@ooo: See my edit.
Moron