2014-12-03 53 views
3

我试图通过浏览器将eml文件返回给用户。事情是,没有静态eml文件 - 页面构建它。我可以用下面的方法将MimeKit消息作为文件返回给浏览器

public FileResult TestServe() 
{ 
    var message = new MimeMessage(); 
    message.From.Add(new MailboxAddress("Joey", "[email protected]")); 
    message.To.Add(new MailboxAddress("Alice", "[email protected]")); 
    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"); 
} 

建立MimeKit样本消息,但是当我运行它,我得到这个错误

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

有没有办法解决?我可以将eml写入临时文件夹,但很明显,它带有性能问题,所以我宁愿不要。有任何想法吗?

+0

您是否尝试过使用[的writeTo(http://jstedfast.github.io/MimeKit/docs/MimeKit/MimeMessage.html #M:MimeKit.MimeMessage.WriteTo(System.IO.Stream,System.Threading.CancellationToken))方法并传递一个新的内存流? – Jan 2014-12-16 16:53:39

回答

6

正如意见建议你可以使用一个WriteTo方法:

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

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

谢谢,我会试试看 – roryok 2014-12-22 14:16:42

相关问题