【问题标题】:Returning a MimeKit message as a file to the browser将 MimeKit 消息作为文件返回给浏览器
【发布时间】:2014-12-03 14:45:14
【问题描述】:

我正在尝试通过浏览器将 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 写入临时文件夹,但显然这会影响性能,所以我宁愿不这样做。有任何想法吗?

【问题讨论】:

  • 您是否尝试过使用WriteTo 方法并传递新的内存流?

标签: c# eml mimekit


【解决方案1】:

根据 cmets 的建议,您可以使用 WriteTo 方法:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多