none
Outlook.Application objOutlook = new Outlook.Application() RRS feed

  • Question

  • Hi there,

    My scenario is when I run the Outlook 2016 then I will save the unread mail as a MSG file. so I write some code to do it like the following sample:

    // Creates a new Outlook Application instance
    Outlook.Application objOutlook = new Outlook.Application();
    
    // Creating a new Outlook message from the Outlook Application instance
    Outlook.MailItem msgInterop = (Outlook.MailItem)(objOutlook.CreateItem(Outlook.OlItemType.olMailItem));
    
    // Set recipient information
    msgInterop.To = "to@domain.com";
    msgInterop.CC = "cc@domain.com";
    
    // Set the message subject
    msgInterop.Subject = "Subject";
    
    // Set some HTML text in the HTML body
    msgInterop.HTMLBody = "<h3>HTML Heading 3</h3> <u>This is underlined text</u>";
    
    // Save the MSG file in local disk
    string strMsg = @"c:\\Temp\TestInterop.msg";
    msgInterop.SaveAs(strMsg, Outlook.OlSaveAsType.olMSG);
    I use the above sample code and got a error message 


    Hi there, if you found my comment very helpful then please | Propose as answer | . Thanks and Regards.

    Tuesday, June 18, 2019 5:15 PM

Answers

All replies

  • Hello Will,

    There are a lot of reasons why it can happen...

    First of all, please remember that 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 consider using a low-level API on which Outlook is built - Extended MAPI or any other third-party wrapper around that API (such as Redemption).

    Second, when automating Office applications from another applications. They must be executed in the same privilege level (security context). That is to say, if there is already an Outlook running on the system in normal mode, the application we used to automate it should be in normal mode. If the Outlook is in Administrative mode, the application we used to automate it should also be in administrative mode. The issue can be caused by your Outlook running as normal state and the Visual Studio is running as Administrative state.

    Third, some anti-virus software may prevent Outlook automation.


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

    Tuesday, June 18, 2019 6:09 PM
  • hi Eugene,

    thanks for your information. my propose is when user open the Outlook application 2010 2013 2016, we could extract the unread mail item (this part work properly) then we need to save the mail item as a msg file. so I mean if following up the scenario, now how should I sort this issue out. let the issue become simple and easy. because you provide three directions for me. but which one direction is better for this issue.

    Thanks for your help 

    WILL


    Hi there, if you found my comment very helpful then please | Propose as answer | . Thanks and Regards.

    Wednesday, June 19, 2019 4:49 AM
  • Do you develop a COM add-in for Outlook?

    Note, there is no need to create a new Application instance in this scenario. You should use a corresponding property available in add-ins.


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

    Wednesday, June 19, 2019 5:27 AM
  • Sure, I create the new Outlook Add-in by using VS .NET 2019 for outlook 2016

    Hi there, if you found my comment very helpful then please | Propose as answer | . Thanks and Regards.

    Wednesday, June 19, 2019 5:33 AM
  • Hi Eugene,

    would you mind providing sample code with me. ? Let me refer to it..

    Thanks.


    Hi there, if you found my comment very helpful then please | Propose as answer | . Thanks and Regards.

    Wednesday, June 19, 2019 6:34 AM
  • You just need to use the Application property of the ThisAddin class:



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

    • Marked as answer by Will .H Wednesday, June 19, 2019 3:43 PM
    Wednesday, June 19, 2019 2:53 PM