2014-12-03 55 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

相關問題