【问题标题】:Create eml file in memory without saving it on disk在内存中创建 eml 文件而不将其保存在磁盘上
【发布时间】:2021-05-03 12:36:18
【问题描述】:

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

我想从以下位置创建一个 eml 文件:

  • html 内容(字符串)
  • 内存中加载的一些附件
  • 字符串主题
  • 字符串收件人
  • 字符串发件人

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

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

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

感谢所有愿意阅读我的人

[编辑] 使用下面 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);
        }

【问题讨论】:

    标签: c# web-applications eml


    【解决方案1】:

    考虑使用MimeKit

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

    【讨论】:

    • 谢谢,我会看看这个
    猜你喜欢
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 2015-12-14
    • 2014-04-18
    • 2015-06-27
    相关资源
    最近更新 更多