0

I'm not sure if this is possible, but I need to return an email message from a web api controller. Essentially, it would then allow the user the open the file (an eml or msg), make some changes and then send it to the relevant person.

Code wise I have a service that returns a MailMessage.

I have a controller that returns a pdf, using the file's byte array as it's content, but a mail message doesn't seem the easiest thing to convert.

Is this possible? I would rather not write the message to disk first if I can help it, but I could if this is the only solution.

Tom
  • 603
  • 1
  • 6
  • 17

1 Answers1

1

I have just faced the same problem, and I resolved it like this:

var stream = new MemoryStream(); // this has to contain your file content

var response = new HttpResponseMessage(HttpStatusCode.OK)
{
    Content = new ByteArrayContent(stream.GetBuffer())
};

response.Content.Headers.ContentType = new MediaTypeHeaderValue("message/rfc822");

response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
    FileName = "test.eml"
};

If you want to know how to generate emails and do not send the but save them to disk instead, you can check the solution proposed here: How to save MailMessage object to disk as *.eml or *.msg file

Community
  • 1
  • 1
JavierFromMadrid
  • 621
  • 9
  • 21