C# 如何将 MailMessage 对象作为 *.eml 或 *.msg 文件保存到磁盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1264672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save MailMessage object to disk as *.eml or *.msg file
提问by ashwnacharya
How do I save MailMessage object to the disk? The MailMessage object does not expose any Save() methods.
如何将 MailMessage 对象保存到磁盘?MailMessage 对象不公开任何 Save() 方法。
I dont have a problem if it saves in any format, *.eml or *.msg. Any idea how to do this?
如果它以任何格式保存,*.eml 或 *.msg,我没有问题。知道如何做到这一点吗?
采纳答案by Ryan Versaw
For simplicity, I'll just quote an explanation from a Connect item:
为简单起见,我将引用Connect 项的解释:
You can actually configure the SmtpClient to send emails to the file system instead of the network. You can do this programmatically using the following code:
SmtpClient client = new SmtpClient("mysmtphost"); client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory; client.PickupDirectoryLocation = @"C:\somedirectory"; client.Send(message);
You can also set this up in your application configuration file like this:
您实际上可以配置 SmtpClient 将电子邮件发送到文件系统而不是网络。您可以使用以下代码以编程方式执行此操作:
SmtpClient client = new SmtpClient("mysmtphost"); client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory; client.PickupDirectoryLocation = @"C:\somedirectory"; client.Send(message);
您还可以在您的应用程序配置文件中进行设置,如下所示:
<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\somedirectory" />
</smtp>
</mailSettings>
</system.net>
</configuration>
After sending the email, you should see email files get added to the directory you specified. You can then have a separate process send out the email messages in batch mode.
发送电子邮件后,您应该会看到电子邮件文件被添加到您指定的目录中。然后,您可以让单独的进程以批处理模式发送电子邮件。
You should be able to use the empty constructor instead of the one listed, as it won't be sending it anyway.
您应该能够使用空构造函数而不是列出的构造函数,因为无论如何它都不会发送它。
回答by saille
Here's an extension method to convert a MailMessage to a stream containing the EML data. Its obviously a bit of a hack as it uses the file system, but it works.
这是将 MailMessage 转换为包含 EML 数据的流的扩展方法。由于它使用文件系统,这显然有点黑客,但它可以工作。
public static void SaveMailMessage(this MailMessage msg, string filePath)
{
using (var fs = new FileStream(filePath, FileMode.Create))
{
msg.ToEMLStream(fs);
}
}
/// <summary>
/// Converts a MailMessage to an EML file stream.
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public static void ToEMLStream(this MailMessage msg, Stream str)
{
using (var client = new SmtpClient())
{
var id = Guid.NewGuid();
var tempFolder = Path.Combine(Path.GetTempPath(), Assembly.GetExecutingAssembly().GetName().Name);
tempFolder = Path.Combine(tempFolder, "MailMessageToEMLTemp");
// create a temp folder to hold just this .eml file so that we can find it easily.
tempFolder = Path.Combine(tempFolder, id.ToString());
if (!Directory.Exists(tempFolder))
{
Directory.CreateDirectory(tempFolder);
}
client.UseDefaultCredentials = true;
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = tempFolder;
client.Send(msg);
// tempFolder should contain 1 eml file
var filePath = Directory.GetFiles(tempFolder).Single();
// stream out the contents
using (var fs = new FileStream(filePath, FileMode.Open))
{
fs.CopyTo(str);
}
if (Directory.Exists(tempFolder))
{
Directory.Delete(tempFolder, true);
}
}
}
You can then take the stream thats returned and do as you want with it, including saving to another location on disk or storing in a database field, or even emailing as an attachment.
然后,您可以获取返回的流并随心所欲地使用它,包括保存到磁盘上的另一个位置或存储在数据库字段中,甚至作为附件通过电子邮件发送。
回答by Gregory Way
For one reason or another the client.send failed (right after an actual send using that method) so I plugged in good 'ole CDO and ADODB stream. I also had to load CDO.message with a template.eml before setting the .Message values. But it works.
出于某种原因,client.send 失败(在使用该方法实际发送之后),所以我插入了好的 'ole CDO 和 ADODB 流。在设置 .Message 值之前,我还必须使用 template.eml 加载 CDO.message。但它有效。
Since the above one is C here is one for VB
由于上面的一个是C,这里是VB的一个
MyMessage.From = New Net.Mail.MailAddress(mEmailAddress)
MyMessage.To.Add(mToAddress)
MyMessage.Subject = mSubject
MyMessage.Body = mBody
Smtp.Host = "------"
Smtp.Port = "2525"
Smtp.Credentials = New NetworkCredential(------)
Smtp.Send(MyMessage) ' Actual Send
Dim oldCDO As CDO.Message
oldCDO = MyLoadEmlFromFile("template.eml") ' just put from, to, subject blank. leave first line blank
oldCDO.To = mToAddress
oldCDO.From = mEmailAddress
oldCDO.Subject = mSubject
oldCDO.TextBody = mBody
oldCDO.HTMLBody = mBody
oldCDO.GetStream.Flush()
oldCDO.GetStream.SaveToFile(yourPath)
回答by Naveen
If you are using Mailkit. Just write below code
如果您使用的是Mailkit。直接写下面的代码
string fileName = "your filename full path";
MimeKit.MimeMessage message = CreateMyMessage ();
message.WriteTo(fileName);