在内存中创建 eml 文件而不将其保存在磁盘上

Create eml file in memory without saving it on disk

我在 Windows 服务器上使用 C# 为存储在 IIS 服务器上的 Web 应用程序工作。

我想从 :

创建一个 eml 文件

主要问题是我不允许在主机服务器上存储文件(即使在临时目录中,或者如果我之后删除它们)。

我看到很多线程解释 how to create an eml file with the help of SmtpClient。但是我们总是需要用一个目录来保存文件。

有人知道这样做的方法吗?或者在内存中创建一个目录() ?

感谢所有阅读我的人

[编辑] 使用下面 jstedfast 的回答和 Mime documentation,我可以想出办法。这是一个 POC,以备日后有人需要。

        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?";

        var builder = new BodyBuilder();

        // Set the plain-text version of the message text
        builder.TextBody = @"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
";

        // We may also want to attach a calendar event for Monica's party...
        builder.Attachments.Add("test.pdf", attachmentByteArray);

        // Now we just need to set the message body and we're done
        message.Body = builder.ToMessageBody();

        using (var memory = new MemoryStream())
        {
            message.WriteTo(memory);
        }

考虑使用 MimeKit

您可以将 MimeMessage 对象写入您想要的任何类型的流,包括 MemoryStream。