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

标签 c# .net email 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);
        }
    }

但是保存的 msg 文件打不开,错误如下: “无法打开文件 xyz.msg。该文件可能不存在,您可能没有打开它的权限,或者它可能已被其他程序打开......”

我的问题是:如何将 System.Net.mail.MailMessage 保存为 msg 文件?

最佳答案

Here Ryan 提出了一种无需任何努力即可轻松完成的好方法。

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);

您也可以像这样在您的应用程序配置文件中进行设置:

 <configuration>
     <system.net>
         <mailSettings>
             <smtp deliveryMethod="SpecifiedPickupDirectory">
                 <specifiedPickupDirectory pickupDirectoryLocation="C:\somedirectory" />
             </smtp>
         </mailSettings>
     </system.net>
 </configuration>

关于c# - 将 System.Net.mail.MailMessage 保存为 .msg 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2423617/

相关文章:

c#为虚拟机分配内存

asp.net - 没有数据绑定(bind)时使 GridView 页脚可见

c# - 进程无法访问文件,因为它被另一个进程使用

email - Paypal 收款 - 它有 API 吗?

email - Arduino 自动电子邮件通知

c# - 如何在 silverlight 应用程序中接收电子邮件?

c# - CosmosClient : Custom json converter works out of the box with NewtonSoft, 不包含 System.Text.Json

c# - jQuery 自动完成文本不通过使用 MVC3 创建 View 保存到数据库

c# - 带有 C# 内部访问修饰符的 Doxygen

.net 运行时 - Silverlight 运行时 =?