簡體   English   中英

將MimeKit消息作為文件返回給瀏覽器

[英]Returning a MimeKit message as a file to the browser

我正在嘗試通過瀏覽器將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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM