none
Open, modify and save Outlook .msg file RRS feed

  • Question

  • When we assign a task, a .msg flie will be produced.

    How can i open outlook .msg file in c++ and modify it and save it as .msg file? I wanna modify the message body(task body,email body),attachment(task attachment, email attachment) From, To, cc and the subject line using c++. How can i do that? Thanks.

    Wednesday, November 23, 2016 8:13 AM

Answers

  • Hello Leo,

    The .MSG file format is well documented. So, you can parse messages on your own. See [MS-OXMSG]: Outlook Item (.msg) File Format for more information. 

    But the simplest way is to automate Outlook. You may find the following links helpful:

    How to automate Outlook 2010 by using C++ in Visual Studio 2010 together with MFC

    C++ app automates Outlook (CppAutomateOutlook)

    Outlook is a singleton, that means only one instance of the application can be run in the system. So, you can use Process.Start method for opening a .msg file. 

    Also the CreateItemFromTemplate method of the Application class can be used for opening .msg files. Read more about that in the  How To: Create a new Outlook message based on a template article.

    After opening you can use the Outlook object model for doing the required modifications. The SaveAs method of the MailItem class saves the Microsoft Outlook item to the specified path and in the format of the specified file type. If the file type is not specified, the MSG format (.msg) is used.

    Be aware, Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article. 

    As a workaround, you may also consider using third-party components designed for the server-side execution. 


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    • Proposed as answer by Chenchen LiModerator Friday, November 25, 2016 8:52 AM
    • Marked as answer by LeoGe Monday, November 28, 2016 3:27 AM
    Wednesday, November 23, 2016 5:18 PM

All replies

  • .msg is used to represent individual email messages, appointments, contacts, tasks, and so on in the filesystem.

    .msg file format versions update regularly:

    Date

    Protocol Revision

    Revision Class

    6/13/2016

    12.0

    Major

    9/14/2015

    11.0

    Major

    5/26/2015

    10.0

    None

    3/16/2015

    10.0

    Major

    10/30/2014

    9.2

    Minor

    7/31/2014

    9.1

    None

    4/30/2014

    9.1

    None

    2/10/2014

    9.1

    None

    My questions:

    1)can we parse .msg, change the subject, body, attachments and so on?

    2)lots of .msg file format  versions, how can i know earch outlook version uses which .msg format version?

    Wednesday, November 23, 2016 2:44 AM
  • Hello Leo,

    The .MSG file format is well documented. So, you can parse messages on your own. See [MS-OXMSG]: Outlook Item (.msg) File Format for more information. 

    But the simplest way is to automate Outlook. You may find the following links helpful:

    How to automate Outlook 2010 by using C++ in Visual Studio 2010 together with MFC

    C++ app automates Outlook (CppAutomateOutlook)

    Outlook is a singleton, that means only one instance of the application can be run in the system. So, you can use Process.Start method for opening a .msg file. 

    Also the CreateItemFromTemplate method of the Application class can be used for opening .msg files. Read more about that in the  How To: Create a new Outlook message based on a template article.

    After opening you can use the Outlook object model for doing the required modifications. The SaveAs method of the MailItem class saves the Microsoft Outlook item to the specified path and in the format of the specified file type. If the file type is not specified, the MSG format (.msg) is used.

    Be aware, Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article. 

    As a workaround, you may also consider using third-party components designed for the server-side execution. 


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    • Proposed as answer by Chenchen LiModerator Friday, November 25, 2016 8:52 AM
    • Marked as answer by LeoGe Monday, November 28, 2016 3:27 AM
    Wednesday, November 23, 2016 5:18 PM
  • Hello,

    I have just answered to your post at https://social.msdn.microsoft.com/Forums/en-US/55e08d19-e1ae-43fb-861c-6d0ed12c7029/open-modify-and-save-outlook-msg-file?forum=appsforoffice . There you can find all possible ways for reading and customizing .msg files programmatically. 

    All formats listed on the sheet are Exchange Server Protocol Documents, not Outlook. So, you can assume all Outlook versions support them (of course, if they can connect to the corresponding Exchange server). 


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Wednesday, November 23, 2016 5:22 PM
  • appreciated!

    Thank you very much!

    Thursday, November 24, 2016 2:33 AM
  • I have done as you to told me. My problem was rolved.

    Now I consider performance impact.

    As you kown, my plug-in run the code as soon as send a mail, and in a flow, we may be need to pare many .msg(e.g.:.msg contains anther .msg file).

    Do you have any experience?

    Thanks

    Leo

    Friday, November 25, 2016 2:51 AM