Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save System.Net.mail.MailMessage as .msg file

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?

like image 990
Gaby Avatar asked Mar 11 '10 09:03

Gaby


People also ask

How do I create a .MSG file?

To create an . msg file from an Outlook item, such as an email message, you can drag-and-drop the item from an Outlook folder to a folder in Windows Explorer, as Figure 1 shows. You can also select the Save As menu option from an Outlook item, then choose a location to save the item.

How do I create a .MSG file in Outlook Web?

Select the email message that you want to save in MSG format. Click the File tab and then click Save As. If you have opened the message and it appears in its own window, click the Microsoft Office Button Button image, and then click Save As. Browse to the file location where you want to save the file.


2 Answers

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>
like image 50
Arsen Mkrtchyan Avatar answered Sep 25 '22 18:09

Arsen Mkrtchyan


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.

like image 28
Martin Vobr Avatar answered Sep 24 '22 18:09

Martin Vobr