将System.Net.mail.MailMessage保存为.msg文件 [英] save System.Net.mail.MailMessage as .msg file
问题描述
我正在构建一个应用程序,我有义务创建一个MailMessage(System.Net.mail.MailMessage)并将其保存在磁盘上.msg扩展不是.eml
I am building an application where i am obligated to create a MailMessage (System.Net.mail.MailMessage) and save it on the disk as .msg extention not .eml
下面是我用来将MailMessage保存为.msg文件的方法:
Below is the method i'm using to save a MailMessage as .msg file:
public static void Save(MailMessage Message, string FileName)
{
Assembly assembly = typeof(SmtpClient).Assembly;
Type _mailWriterType =
assembly.GetType("System.Net.Mail.MailWriter");
using (FileStream _fileStream =
new FileStream(FileName, FileMode.Create))
{
// Get reflection info for MailWriter contructor
ConstructorInfo _mailWriterContructor =
_mailWriterType.GetConstructor(
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new Type[] { typeof(Stream) },
null);
// Construct MailWriter object with our FileStream
object _mailWriter =
_mailWriterContructor.Invoke(new object[] { _fileStream });
// Get reflection info for Send() method on MailMessage
MethodInfo _sendMethod =
typeof(MailMessage).GetMethod(
"Send",
BindingFlags.Instance | BindingFlags.NonPublic);
// Call method passing in MailWriter
_sendMethod.Invoke(
Message,
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new object[] { _mailWriter, true },
null);
// Finally get reflection info for Close() method on our MailWriter
MethodInfo _closeMethod =
_mailWriter.GetType().GetMethod(
"Close",
BindingFlags.Instance | BindingFlags.NonPublic);
// Call close method
_closeMethod.Invoke(
_mailWriter,
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new object[] { },
null);
}
}
但保存的msg文件不打开和下面是错误:
无法打开文件xyz.msg。文件文件可能不存在,您可能没有权限打开它或可能由另一个程序打开....
But the saved msg file doesn't open and below is the error: "Cannot open file xyz.msg.The file file may not exist, you may not have permission to open it or it may be open by another program...."
我的问题是:如何将System.Net.mail.MailMessage保存为msg文件?
推荐答案
在这里瑞恩建议一个简单而好的方法,无需任何努力。
Here Ryan suggests an easy and great way to do it whithout any effort.
你实际上可以配置SmtpClient来发送电子邮件到文件
系统而不是网络。您可以使用
以下代码以编程方式执行此操作:
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:
<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\somedirectory" />
</smtp>
</mailSettings>
</system.net>
</configuration>
这篇关于将System.Net.mail.MailMessage保存为.msg文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!