0
Reply

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

Pride Grimm

Pride Grimm

Apr 25 2012 8:21 PM
2.8k
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
  ErrorCode=-763099643
  StackTrace:
  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
  InnerException: 

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 {

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 address = mail1.Recipients[1].Address;
 string subject = mail1.Subject;
 string
body = mail1.Body;
 MessageBox
.Show("Hello, you send an email to " + address );  
 MessageBox
.Show("Hello, you email said " + body);
 
}


This is the whole code.  I am typing some text in the body, just as I type some into the "To", and "subject".    I am going to try your two suggestions, see what I can come up with.   I can't figure why the body would be any different than the "to" or "subject".  The error comes up on the  string body = mail1.Body;
 
Messing around, without trying to "get" the body using  
string body = mail1.BodyI 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