Open specific local Outlook window from .msg file

Author - Harleen Kaur

C# : Triggering the Local Outlook for forward, reply and reply all actions from existing .msg file

For the desktop based applications when user is dealing with the store outlook files in msg format we can open local outlook when required.

In outlook when we open a email message, we get the Forward, Reply & Reply All Buttons, where on click of these buttons respective Outlook window opens. From the stored .msg files in my application I had to fire the trigger to open the locally installed Outlook with Forward, Reply & Reply all windows in Outlook. To work with active instance of Microsoft Outlook, we are required  to install the Microsoft.Office.Interop.Outlook.Application dll.

Below is the sample code to trigger Forward, Reply & Reply all windows in Outlook:

Trigger Forward in Outlook:

                Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();

                    Microsoft.Office.Interop.Outlook.MailItem oMailItem = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

Outlook.Accounts accounts = oApp.Session.Accounts;

private Outlook.Account oAccount = null;

                foreach (Outlook.Account account in accounts)

                {

// Use the specific outlook account..

                              oAccount = account;

                }

                oMailItem = (Outlook.MailItem)oApp.CreateItemFromTemplate(_sourceFile); 

// Specify the file location of saved .msg file. 

                oMailItem.RecipientReassignmentProhibited = false;            

                if (this.from != null && testuser == true)

                {

                    Outlook.Actions actions = oMailItem.Actions;

                    Outlook.Action action = actions["Forward"];  // Add the action as forward

                    Marshal.ReleaseComObject(actions);

                    while( oMailItem.Recipients.Count!=0) {

                          oMailItem.Recipients.Remove(1);   // Remove the recipient’s Information                    

                    }

                    oMailItem.SendUsingAccount = oAccount;

                    oMailItem.SentOnBehalfOfName = oAccount.UserName;

                    Outlook.MailItem response = action.Execute() as Outlook.MailItem;

                    Marshal.ReleaseComObject(action);

                     response.Display(true);

                    Marshal.ReleaseComObject(response);

       }

In case of forward, attachments are also attached. This performs the same action. We may need to remove the recipient Information in this case manually as this creates the new mail from the existing file, which acts as template file and picks all the Information available in the file.

Trigger Reply, ReplyAll in Outlook:

string actionName=’Reply’; // In case of Reply All we will use ReplyAll here.

 Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();

                Microsoft.Office.Interop.Outlook._MailItem oMailItem = (Microsoft.Office.Interop.Outlook._MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);       

    oMailItem = (Outlook.MailItem)oApp.CreateItemFromTemplate(_sourceFile);

    Outlook.Actions actions = oMailItem .Actions;

    Outlook.Action action = actions[actionName];   // set action here..  Reply or reply all

    Marshal.ReleaseComObject(actions);

    action.ReplyStyle = Outlook.OlActionReplyStyle.olUserPreference;  // To set as Reply format

    Outlook._MailItem response = action.Execute() as Outlook.MailItem;

    Marshal.ReleaseComObject(action);

    response.Display(true);

    Marshal.ReleaseComObject(response);

As in case of Reply & Reply all, attachments are not attached, it behaves in the same way.
If the action is Reply then in this case it sets the ‘To’ field and in case of ‘Reply All’, it sets to, cc .. fields to set reply fields for all.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *