save System.Net.mail.MailMessage as .msg file
Asked Answered
F

6

11

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?

Fiddlefaddle answered 11/3, 2010 at 9:0 Comment(0)
S
4

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>
Sigismond answered 11/3, 2010 at 9:7 Comment(3)
Yes, it is true, but the MailMessage is saved as .eml file i want it to be saved as .msg fileFiddlefaddle
Gaby,do you have an answer to it?Schram
Jason, I don't think this link-only answer can become invalid, because the link is to another SO page, However I have added quote ;)Sigismond
C
3

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.

Camarilla answered 21/9, 2010 at 16:5 Comment(0)
K
2

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

Kerakerala answered 11/3, 2010 at 13:25 Comment(0)
R
2

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.

Ruffianism answered 14/11, 2011 at 8:17 Comment(1)
Outlook Interop is useful for client-side settings where Office is installed, but almost un-useable in server-side scenarios.Inaptitude
S
1

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

Savoyard answered 14/11, 2011 at 7:59 Comment(1)
This example is in C++, not .NETInaptitude
C
0

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.

Crinoid answered 21/9, 2011 at 6:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.