locked
Move outlook email and save as .msg file or eml file or mhtml format RRS feed

  • Question

  • User-148788041 posted

    Move outlook emails and save email as .msg file and .eml file or mhtml file.<br>
    I am using Microsoft.ineteropp.outlook.dll
    Tuesday, January 8, 2019 5:37 AM

All replies

  • User-148788041 posted
    For msg i used
    Mailmessage m=new mailmessage();
    m.SaveAs(pathname,outlook.OlSaveAsType.olMSG)

    For Mhtml i used
    m.SaveAs(pathname,outlook.OlSaveAsType.olHtml)

    For eml format .need code
    Tuesday, January 8, 2019 8:50 AM
  • User36583972 posted


    Hi Guhananth,

    For msg i used
    Mailmessage m=new mailmessage();
    m.SaveAs(pathname,outlook.OlSaveAsType.olMSG)

    For Mhtml i used
    m.SaveAs(pathname,outlook.OlSaveAsType.olHtml)

    For eml format .need code

    You can use the code for extension method to save mailMessage ( from codeProject : Adding Save() functionality to System.Net.Mail.MailMessage )

    MailMessageExt:

    public static class MailMessageExt
        {
            public static void Save(this 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  -- before net 4.5 
                    //_sendMethod.Invoke(
                    //    Message,
                    //    BindingFlags.Instance | BindingFlags.NonPublic,
                    //    null,
                    //    new object[] { _mailWriter, true },
                    //    null);
    
                    // Call method passing in MailWriter  --net 4.5 later
                    _sendMethod.Invoke(
                          Message,
                           BindingFlags.Instance | BindingFlags.NonPublic,
                          null,
                        new object[] { _mailWriter, true, 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);
                }
            }
        }

    Call :

        MailMessage _testMail = new MailMessage();
                _testMail.Body = "This is a test email";
                _testMail.To.Add(new MailAddress("email@domain.com"));
                _testMail.From = new MailAddress("sender@domain.com");
                _testMail.Subject = "Test email";
                _testMail.Save(@"D:\testemail.eml");

    Best Regards,

    Yong Lu

    Wednesday, January 9, 2019 7:42 AM
  • User-148788041 posted
    Hi
    I am not using system.net.mail.using microsoft.interopp.office.outlook dll.
    How to save outlook email as msg or eml file.
    Thursday, January 10, 2019 11:21 AM
  • User-943250815 posted

    Do you have Outlook installed on Server?
    As I understand to use such DLL you need to have Outlook installed and configured.

    Thursday, January 10, 2019 10:39 PM