将 System.Net.mail.MailMessage 保存为 .msg 文件

发布于 2024-08-25 05:51:47 字数 2201 浏览 4 评论 0原文

我正在构建一个应用程序,我有义务创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

天气好吗我好吗 2024-09-01 05:51:47

此处 Ryan 建议无需任何努力即可轻松完成此任务的好方法。

您实际上可以配置 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>

Here Ryan suggests an easy and great way to do it whithout any effort.

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>
通知家属抬走 2024-09-01 05:51:47

如果您引用的是 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.

月亮是我掰弯的 2024-09-01 05:51:47

如果您引用的是 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

杀お生予夺 2024-09-01 05:51:47

检查这个使用 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.

思念绕指尖 2024-09-01 05:51:47

Microsoft 支持知识库文章:
信息:将消息保存到 MSG 复合文件

Microsoft Support KB article:
INFO: Save Message to MSG Compound File

缺⑴份安定 2024-09-01 05:51:47

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文