Returning a MimeKit message as a file to the browser

I am trying to return an eml file through the browser to the user. The point is that there is no static eml file - the page creates it. I can create an example post in MimeKit using the following method

public FileResult TestServe()
{
    var message = new MimeMessage();
    message.From.Add(new MailboxAddress("Joey", "joey@friends.com"));
    message.To.Add(new MailboxAddress("Alice", "alice@wonderland.com"));
    message.Subject = "How you doin?";

    message.Body = new TextPart("plain")
    {
        Text = @"Hey Alice,

        What are you up to this weekend? Monica is throwing one of her parties on
        Saturday and I was hoping you could make it.

        Will you be my +1?

        -- Joey
        "
    };

    MemoryStream stream = new MemoryStream();
    IFormatter formatter = new BinaryFormatter();
    formatter.Serialize(stream, message);

    return File(stream, "message/rfc822");
}

      

But when I run it I get this error

Type 'MimeKit.MimeMessage' in Assembly 'MimeKit, Version=0.27.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

      

Is there a way to get around this? I can write eml to a temp folder, but obviously this is due to a performance hit, so I would prefer. Any ideas?

+3


source to share


1 answer


As pointed out in the comments, you can use the method WriteTo

:



var stream = new MemoryStream();
message.WriteTo(stream);
stream.Position = 0;

return File(stream, "message/rfc822");

      

+6


source







All Articles