0

I am writing VSTO Outlook add-in that needs to save some items as msg files. The problem is, each time I call MailItem.SaveAs it causes Outlook to lag slightly and show processing cursor (blue circle). I have tried moving it int a separate thread, but that does not help. Saving item is quite fast (less than 100 ms most of the time), but still causes this annoying behavior. I need to save an item to read it as MSG format, so if I can do this directly this would be even better, but as I found here: Outlook MailItem as Stream the only solution seems to use EWS for this. Are there other alternatives?

Maybe using RDO can help in this case? Another option, as I understand, create msg manually from mail item properties. But maybe there is an easier way?

Sergey Belikov
  • 420
  • 3
  • 14
  • Have you tried to use RDO? It can be used on a secondary thread. – Dmitry Streblechenko Jul 12 '18 at 19:54
  • Yes, I was thinking about it. Will give it a try. – Sergey Belikov Jul 13 '18 at 07:15
  • Creating Redemption SafeMailItem from Outlook MailItem and saving it seems to help. But I have some questions. Is it safe to call Marshal.ReleaseComObject for an item after SafeMailItem is created with Item set? Does it hold any references or just copy data? And saved msg differs a little from the one I get from Outlook. For example, for messages with empty subject it shows [UNREGISTERED] in subject. – Sergey Belikov Jul 13 '18 at 09:55
  • Yes, Marshal.ReleaseComObject is safe - it just ensures that the COIM object is released immediately rather than later on the GC thread.[UNREGISTERED] is added by the dev version - it is not present in the distributable version. – Dmitry Streblechenko Jul 13 '18 at 21:01

1 Answers1

3

Unlike OOM, a low level API on which Outlook is based on (Extended MAPI) supports multithreading. So, you can run the code on a secondary thread without any visible impact to the Outlook UI. See INFO: Save Message to MSG Compound File for more information.

Also you may consider using third-party wrappers around Extended MAPI such as Redemption.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thanks! So if I create dll with such function I can pass MAPIOBJECT from OOM there it may work? Or try to reproduce it in C#. I was thinking about Redemption also, but we have strict security rules, so try to use minimum external closed-source libraries. – Sergey Belikov Jul 13 '18 at 07:15