Retrieve Current Email Body In Outlook

孤者浪人 提交于 2019-12-05 04:25:33

You shouldn’t initialize a new Outlook.Application() instance each time. Most add-in frameworks provide you with an Outlook.Application instance, corresponding to the current Outlook session, typically through a field or property named Application. You are expected to use this for the lifetime of your add-in.

To get the currently-selected item, use:

Outlook.Explorer explorer = this.Application.ActiveExplorer();
Outlook.Selection selection = explorer.Selection;

if (selection.Count > 0)   // Check that selection is not empty.
{
    object selectedItem = selection[1];   // Index is one-based.
    Outlook.MailItem mailItem = selectedItem as Outlook.MailItem;

    if (mailItem != null)    // Check that selected item is a message.
    {
        // Process mail item here.
    }
}

Note that the above will let you process the first selected item. If you have multiple items selected, you might want to process them in a loop.

On Top add reference to

using Outlook = Microsoft.Office.Interop.Outlook;

Then inside a method;

Outlook._Application oApp = new Outlook.Application();
if (oApp.ActiveExplorer().Selection.Count > 0)
            {
                Object selObject = oApp.ActiveExplorer().Selection[1];

                if (selObject is Outlook.MailItem)
                {
                    Outlook.MailItem mailItem = (selObject as Outlook.MailItem);
                    String htmlBody = mailItem.HTMLBody;
                    String Body = mailItem.Body;
                 }
             }

Also you can change the body which will show in outlook before viewing the mail.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!