将 System.Net.mail.MailMessage 保存为 .msg 文件
我正在构建一个应用程序,我有义务创建一个 MailMessage (System.Net.mail.MailMessage) 并将其保存在磁盘上作为 .msg 扩展名而不是 .eml
下面是我用来将 MailMessage 保存为 .msg 的方法文件:
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);
}
}
但保存的消息文件无法打开,错误如下: “无法打开文件 xyz.msg。该文件可能不存在,您可能没有打开它的权限,或者它可能被其他程序打开......”
我的问题是:如何将System.Net.mail.MailMessage另存为msg文件?
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
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);
}
}
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...."
My question is: How to save System.Net.mail.MailMessage as msg file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
此处 Ryan 建议无需任何努力即可轻松完成此任务的好方法。
您还可以在应用程序配置文件中进行设置,如下所示:
Here Ryan suggests an easy and great way to do it whithout any effort.
You can also set this up in your application configuration file like this:
如果您引用的是 Outlook MSG 文件格式,请检查 Microsoft 发布的 MSG 格式规范< /a>.以下对类似问题的回答也可能有所帮助。
If you are referring to Outlook MSG file format check the MSG format specification published by Microsoft. Following answer to similar question might also help.
如果您引用的是 Outlook .msg 文件,则无法在 .NET 中本机完成此操作。
Outlook .msg 文件是一个复合文档,因此,您需要将其保存为该格式,并确保将所有适当的属性放在其确切位置。
您需要自己编写、使用第三方或可能的 Outlook 互操作。
对不起,
戴夫
If you are referring to the Outlook .msg file, this cannot be done natively in .NET.
The Outlook .msg file is a compound document, so, you would need to save it in that format, and be sure to put all of the appropriate properties in their exact locations.
You need to either write your own, use a 3rd party, or possible Outlook interop.
Sorry,
Dave
检查这个使用 Outlook Interop 库的简短示例 使用 C# 创建 Outlook 邮件文件。这并不完全是您所要求的,但有一种方法可以将值从一个手动复制到另一个。
Check this short example that uses Outlook Interop library Creating an Outlook Message File with C# . It's not exactly what have you been asking for but there is a way to copy values from one to another - manually.
Microsoft 支持知识库文章:
信息:将消息保存到 MSG 复合文件
Microsoft Support KB article:
INFO: Save Message to MSG Compound File
Outlook 给出的错误消息是由于 .msg 扩展名引起的。我使用相同的方法将 MailMessage 对象保存到磁盘,并发现该文件只能在 Outlook 中打开,扩展名为 .eml。
The error message given by Outlook is because of the .msg extension. I am using the same method to save MailMessage objects to disk and have found that the file will only open in outlook with an extension of .eml.