MailItem.HtmlBody throws a Not Implemented exception

只谈情不闲聊 提交于 2020-05-15 08:03:09

问题


I have a ribbon button that inserts text into an outlook Inspector by modifying the MailItem object based on the recipients in it. The method that gets called on click looks like this:

public async void OnTemplateClick(Office.IRibbonControl control)
        {
            var templateId = control.Tag;
            var template = templates.GetTemplateById(templateId);
            await templateUi.SetTemplate(control.Context, template);
        }

the SetTemplate method looks like this:

public async Task SetTemplate(object window, Template template, 
            SynchronizationContext uiThread = null)
{
Outlook.MailItem currentMailItem = null;
            Outlook.Recipients olRecps = null;
            Outlook.Recipient recp = null;
            Outlook.AddressEntry addEntry = null;
            try
            {
                currentMailItem = GetMailItem(window);
                olRecps = currentMailItem.Recipients;
                var recipType = Outlook.OlMailRecipientType.olTo;
                var recps = from r in olRecps.Cast<Outlook.Recipient>()
                            where r.Type == (int)recipType
                            select r;
                var numRecps = recps.Count();

                var oldBodyHtml = currentMailItem.HTMLBody;
               ...

Now, sometimes, that last line that fetches the HTMLBody throws the following error:

System.Runtime.InteropServices.COMException (0x8E604001): Not implemented.
   at Microsoft.Office.Interop.Outlook._MailItem.get_HTMLBody()

This error doesnt happen all the time and its very difficult to reproduce so we mostly see it in the application logs. I was wondering what could possibly be causing this error? I assumed this has something to do with the timing of this asynchronous call which means that the MailItem message isnt fully formed?

Thanks!


回答1:


Outlook Object Model cannot be used on a secondary thread. Outlook 2016 will raise an error immediately as it detects such a call. In the older versions of Outlook the calls can fail unpredictably.

If secondary thread is a must, your only options are Extended MAPI (C++ or Delphi) or Redemption (its RDO family of objects can be used on secondary threads).



来源:https://stackoverflow.com/questions/35853432/mailitem-htmlbody-throws-a-not-implemented-exception

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