3

我正在尝试通过浏览器将 eml 文件返回给用户。问题是,没有静态 eml 文件 - 页面构建它。我可以使用以下方法在 MimeKit 中构建示例消息

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");
}

但是当我运行它时,我得到了这个错误

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

有没有解决的办法?我可以将 eml 写入临时文件夹,但显然这会影响性能,所以我宁愿不这样做。有任何想法吗?

4

1 回答 1

7

正如评论中所建议的,您可以使用一种WriteTo方法:

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

return File(stream, "message/rfc822");
于 2014-12-18T22:46:06.717 回答