我试图通过浏览器将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写入临时文件夹但显然会出现性能损失,所以我不愿意。有任何想法吗?
答案 0 :(得分:6)
根据评论中的建议,您可以使用WriteTo
方法:
var stream = new MemoryStream();
message.WriteTo(stream);
stream.Position = 0;
return File(stream, "message/rfc822");