[英]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 文件:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.