简体   繁体   English

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

[英]Create eml file in memory without saving it on disk

I'm working with C# on Windows servers for a web application stored on the IIS Server.我正在 Windows 服务器上使用 C#,用于存储在 IIS 服务器上的 web 应用程序。

I would like to create an eml file from:我想从以下位置创建一个 eml 文件:

  • an html content (string) html 内容(字符串)
  • some attachments that are loaded in memory memory中加载的一些附件
  • a string subject字符串主题
  • string recipients字符串收件人
  • string sender字符串发送者

The main problem is that I am not allowed to store files on the host server (not even in a temporary directory or if I delete them after).主要问题是我不允许在主机服务器上存储文件(即使在临时目录中,或者如果我之后删除它们)。

I saw many threads explaining how to create an eml file with the help of SmtpClient .我看到许多线程解释如何在 SmtpClient 的帮助下创建 eml 文件 But we always need to use a directory to save the file.但是我们总是需要使用一个目录来保存文件。

Do someone knows a way to do that?有人知道这样做的方法吗? Or to create a directory in memory ( which seems undoable )?或者在 memory 中创建一个目录( 这似乎是不可撤销的)?

Thanks for everyone who will read me感谢所有会读我的人

[EDIT] Using jstedfast's answer below and the Mime documentation , I could figure a way. [编辑] 使用下面 jstedfast 的回答和Mime 文档,我可以找到一种方法。 Here is a POC in case someone needs it later.这是一个 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);
        }

Look into using MimeKit .研究使用MimeKit

You can write the MimeMessage objects to any type of stream that you want, including a MemoryStream.您可以将 MimeMessage 对象写入您想要的任何类型的 stream,包括 MemoryStream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM