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。你知道怎么做吗?
3 个解决方案
#1
104
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以将电子邮件发送到文件系统而不是网络。您可以使用以下代码以编程方式完成此操作:
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:
您还可以在应用程序配置文件中这样设置:
<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.
您应该能够使用空的构造函数而不是列出的构造函数,因为无论如何它都不会发送它。
#2
22
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.
这里有一个扩展方法,可以将邮件消息转换为包含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.
然后,您可以获取返回的流并对其进行您想要的操作,包括保存到磁盘上的另一个位置或存储到数据库字段中,甚至以附件的形式发送电子邮件。
#3
0
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.
出于某种原因,客户。发送失败(在使用该方法的实际发送之后),所以我插入了良好的ole CDO和ADODB流。我还得下载CDO。消息使用模板。在设置. message值之前执行eml。但它的工作原理。
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)
#1
104
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以将电子邮件发送到文件系统而不是网络。您可以使用以下代码以编程方式完成此操作:
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:
您还可以在应用程序配置文件中这样设置:
<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.
您应该能够使用空的构造函数而不是列出的构造函数,因为无论如何它都不会发送它。
#2
22
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.
这里有一个扩展方法,可以将邮件消息转换为包含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.
然后,您可以获取返回的流并对其进行您想要的操作,包括保存到磁盘上的另一个位置或存储到数据库字段中,甚至以附件的形式发送电子邮件。
#3
0
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.
出于某种原因,客户。发送失败(在使用该方法的实际发送之后),所以我插入了良好的ole CDO和ADODB流。我还得下载CDO。消息使用模板。在设置. message值之前执行eml。但它的工作原理。
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)