Problem developing an outlook 2007 addin with c# - retrieving the mailitem.Body property

May 30, 2012
Reaction score
I am working on an Outlook addin using c#. I am more or less just trying different things to see how it all works. I first wanted to get some fields from an email written and sent in outlook. So I can get the To, CC, and subject with the following code, but when I try to get mailItem.Body or mailItem.HTMLBody I get an exception:
System.Runtime.InteropServices.COMException was unhandled by user code
Message=The operation failed.
Source=Microsoft Office Outlook
at Microsoft.Office.Interop.Outlook._MailItem.set_Body(String Body)
at FirstOutlookPlugin.ThisAddIn.Inspectors_NewInspector(Inspector Inspector) in L:\Pub. Files\Beginning Visual C# 2008\Chapter09\List try\FirstOutlookPlugin\FirstOutlookPlugin\ThisAddIn.cs:line 25

Does anyone know what's up? I have read it may be something to do with security patches and SMIME (which I am trying to read up on). Seems like if it works getting the "to" field and stuff, it should be no problem getting the body also.
Thanks for any help!!

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml.Linq; using Office = Microsoft.Office.Core; using Outlook = Microsoft.Office.Interop.Outlook; namespace FirstOutlookPlugin { public partial class ThisAddIn { //Outlook.Inspectors inspectors; Outlook.MailItem mailItem; private void ThisAddIn_Startup(object sender, System.EventArgs e) { this.Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend); } private void Application_ItemSend(object Item, ref bool Cancel) { Outlook.MailItem mail1 = Item as Outlook.MailItem; mail1.Save(); string body = mail1.HTMLBody; MessageBox.Show("Hello, the email body you sent it " + body); } private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { } #region VSTO generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InternalStartup() { this.Startup += new System.EventHandler(ThisAddIn_Startup); this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); } #endregion VSTO generated code }

Messing around, without trying to "get" the body using
string body = mail1.Body
I look at the mail1 in the locals, and it's showing this in the body and htmlbody property
Body {System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException: The operation failed.
--- End of inner exception stack trace ---
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at System.Dynamic.IDispatchComObject.GetMembers(IEnumerable`1 names)} System.Reflection.TargetInvocationException


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question