c# - 将 MimeKit 消息作为文件返回给浏览器

标签 c# eml mimekit

我正在尝试通过浏览器向用户返回一个 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 方法:

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

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

关于c# - 将 MimeKit 消息作为文件返回给浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27274538/

相关文章:

python - 如何使用 Python 从电子邮件附加的存档(rar 或 zip)中获取文件列表?

email - 发送电子邮件的工具

vb.net - MailKit/MimeKit SMTP 连接错误

c# - 在 .NET 线程中花费了多少时间?

c# - 如何使用/复杂数据交互测试函数

c# - 如何使用 Microsoft Office Interop word 选择段落的第一行或(前 20 个字符)。?

python - 如何在 python 中读取 eml 文件?

c# - 使用 string.Substring() 作为链的一部分

c# - 使用 mimekit/mailkit 库获取电子邮件的传递状态

c# - MimeKit:如何嵌入图像?