System.Net.mail.MailMessage保存为.msg文件 [英] save System.Net.mail.MailMessage as .msg file

查看:666
本文介绍了System.Net.mail.MailMessage保存为.msg文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在大楼里我有义务来创建一个MailMessage(System.Net.mail.MailMessage)并将其保存在磁盘上的。味精延伸不是的.eml应用程序

下面是我使用保存MailMessage的方法.msg文件:

 公共静态无效保存(MailMessage消息,字符串文件名)
    {
        装配装配= typeof运算(SmtpClient).Assembly;
        键入_mailWriterType =
          assembly.GetType(System.Net.Mail.MailWriter);

        使用(的FileStream _fileStream =
               新的FileStream(文件名,FileMode.Create))
        {
            //获取MailWriter构造器反射信息
            ConstructorInfo _mailWriterContructor =
                _mailWriterType.GetConstructor(
                    BindingFlags.Instance | BindingFlags.NonPublic可,
                    空值,
                    新类型[] {typeof运算(流)},
                    空值);

            //构造MailWriter对象与我们的FileStream
            对象_mailWriter =
              _mailWriterContructor.Invoke(新对象[] {_fileStream});

            //获取发送(反射信息)上MailMessage方法
            MethodInfo的_sendMethod =
                typeof运算(MailMessage).GetMethod(
                    发送,
                    BindingFlags.Instance | BindingFlags.NonPublic可);

            //调用方法传递MailWriter
            _sendMethod.Invoke(
                信息,
                BindingFlags.Instance | BindingFlags.NonPublic可,
                空值,
                新对象[] {_mailWriter,真实},
                空值);

            //终于搞定了关闭()在我们的MailWriter方法​​反映信息
            MethodInfo的_closeMethod =
                _mailWriter.GetType()。GetMethod的(
                    关闭,
                    BindingFlags.Instance | BindingFlags.NonPublic可);

            //调用close方法
            _closeMethod.Invoke(
                _mailWriter,
                BindingFlags.Instance | BindingFlags.NonPublic可,
                空值,
                新对象[] {},
                空值);
        }
    }
 

但保存的MSG文件没有打开及以下的错误: 无法打开文件xyz.msg.The文件文件可能不存在,您可能没有权限打开它,或者也可能是打开其他程序......

我的问题是:如何到System.Net.mail.MailMessage保存为MSG文件

解决方案

看<一href="http://stackoverflow.com/questions/1264672/how-to-save-mailmessage-object-to-disk-as-eml-or-msg-file">here瑞安提出一个简单而伟大的方式做任何内部消除努力

  

您可以实际配置为发送电子邮件到文件SmtpClient   系统,而不是网络。您可以以编程方式使用做到这一点   以下code:

  SmtpClient客户端=新SmtpClient(mysmtphost);
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = @C:\ somedirectory;
client.Send(消息);
您还可以设置在这样的应用程序配置文件:

 &lt;结构&GT;
     &LT; system.net&GT;
         &LT; mailSettings&GT;
             &LT; SMTP deliveryMethod =SpecifiedPickupDirectory&GT;
                 &LT; specifiedPickupDirectory pickupDirectoryLocation =C:\ somedirectory/&GT;
             &LT; / SMTP&GT;
         &LT; / mailSettings&GT;
     &LT; /system.net>
 &LT; /结构&gt;
 

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?

解决方案

look 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>

这篇关于System.Net.mail.MailMessage保存为.msg文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆