我正在使用C#在Windows服务器上为存储在IIS服务器上的Web应用程序工作。
我想创建一个eml文件,其中包括:
- html内容(字符串)
- 一些在内存中加载的附件
- 字符串主题
- 字符串收件人
- 字符串发件人
主要问题是不允许在主机服务器上存储文件(即使在临时目录或在删除它们后也不行)。
我看到了很多解释如何通过SmtpClient帮助创建eml文件的线程。但是我们总是需要使用目录来保存文件。
有没有人知道如何做到这一点?或者在内存中创建一个目录(似乎做不到)?
感谢每个阅读我的人
[编辑] 使用下面jstedfast的答案和Mime文档,我可以找到一种方法。以下是一个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);
}