C# application crashes after sending xml through outlook mail
<p>C# application crashes after sending xml through outlook mail. Also it requires outlook to be closed while sending mail.Using OUTLOOK 2010. It works fine with lower versions.</p>

<pre><code>using System;
using System.Reflection;

namespace Amdocs.Infra.Controls
{
   public class OutlookWrapper
   {
     public OutlookWrapper ()
     {
       outlookType = Type.GetTypeFromProgID ("Outlook.Application", true);
       outlookObject = Activator.CreateInstance (outlookType);
       namespaceMAPI = outlookType.InvokeMember ("GetNamespace", BindingFlags.InvokeMethod, null, outlookObject, new object [] {"MAPI"});
       namespaceMAPIType = namespaceMAPI.GetType ();
       namespaceMAPIType.InvokeMember ("Logon", BindingFlags.InvokeMethod, null, namespaceMAPI, new object [] {null, null, true, false});
     }
#endregion

#region ~OutlookWrapper ()
~OutlookWrapper ()
{
  namespaceMAPIType.InvokeMember ("Logoff", BindingFlags.InvokeMethod, null, namespaceMAPI, new object [] {});
}
#endregion

#region SendMail

#region public void SendMail (bool modal, string toValue)
public void SendMail (bool modal, string toValue)
{
  SendMail (modal, toValue, String.Empty);
}
#endregion
#region public void SendMail (bool modal, string toValue, string subjectValue)
public void SendMail (bool modal, string toValue, string subjectValue)
{
  SendMail (modal, toValue, subjectValue, String.Empty);
}
#endregion
#region public void SendMail (bool modal, string toValue, string subjectValue, string bodyValue)
public void SendMail (bool modal, string toValue, string subjectValue, string bodyValue)
{
  SendMail (modal, toValue, subjectValue, bodyValue, null);
}
#endregion
#region public void SendMail (bool modal, string toValue, string subjectValue, string bodyValue, string[] attachments)
public void SendMail (bool modal, string toValue, string subjectValue, string bodyValue, string[] attachments)
{
  CreateNewMailItem (toValue, subjectValue, bodyValue, attachments);
  outlookMailItemType.InvokeMember ("Send", BindingFlags.InvokeMethod, null, outlookMailItem, new object [] {modal});
}
#endregion

#endregion

#region NewMail

#region public void NewMail ()
public void NewMail ()
{
  NewMail (true);
}
#endregion
#region public void NewMail (bool modal)
public void NewMail (bool modal)
{
  NewMail (modal, String.Empty);
}
#endregion
#region public void NewMail (bool modal, string toValue)
public void NewMail (bool modal, string toValue)
{
  NewMail (modal, toValue, String.Empty);
}
#endregion
#region public void NewMail (bool modal, string toValue, string subjectValue)
public void NewMail (bool modal, string toValue, string subjectValue)
{
  NewMail (modal, toValue, subjectValue, String.Empty);
}
#endregion
#region public void NewMail (bool modal, string toValue, string subjectValue, string bodyValue)
public void NewMail (bool modal, string toValue, string subjectValue, string bodyValue)
{
  NewMail (modal, toValue, subjectValue, bodyValue, null);
}
#endregion
#region public void NewMail (bool modal, string toValue, string subjectValue, string bodyValue, string[] attachments)
public void NewMail (bool modal, string toValue, string subjectValue, string bodyValue, string[] attachments)
{
  CreateNewMailItem (toValue, subjectValue, bodyValue, attachments);
  outlookMailItemType.InvokeMember ("Display", BindingFlags.InvokeMethod, null, outlookMailItem, new object [] {modal});
}
#endregion

#endregion

#region private void CreateNewMailItem (string toValue, string subjectValue, string bodyValue, string[] attachments)
private void CreateNewMailItem (string toValue, string subjectValue, string bodyValue, string[] attachments)
{
  outlookMailItem = outlookType.InvokeMember ("CreateItem", BindingFlags.InvokeMethod, null, outlookObject, new object [] {null});
  if (outlookMailItem == null) throw new ApplicationException (StringsManager.GetString("Cannot create outlook mail item."));

  outlookMailItemType = outlookMailItem.GetType ();
  if (outlookMailItemType == null) throw new ApplicationException (StringsManager.GetString("Cannot get outlook mail item type."));

  outlookMailItemType.InvokeMember ("To",      BindingFlags.SetProperty, null, outlookMailItem, new object [] {toValue});
  outlookMailItemType.InvokeMember ("Subject", BindingFlags.SetProperty, null, outlookMailItem, new object [] {subjectValue});
  outlookMailItemType.InvokeMember ("Body",    BindingFlags.SetProperty, null, outlookMailItem, new object [] {bodyValue});

  if (attachments != null)
  {
    object attachmentsObject = outlookMailItemType.InvokeMember ("Attachments", BindingFlags.GetProperty, null, outlookMailItem, new object [] {});
    if (attachmentsObject == null) throw new ApplicationException (StringsManager.GetString("Cannot get outlook attachments property."));

    System.Type attachmentsType = attachmentsObject.GetType ();
    if (attachmentsType == null) throw new ApplicationException (StringsManager.GetString("Cannot get outlook attachments property type."));

    int bodyLength = bodyValue.Length;

    /*
     * We do not need this since we have the original text message
     * 
    object bodyObject = outlookMailItemType.InvokeMember ("Body", BindingFlags.GetProperty, null, outlookMailItem, new object [] {});
    if (bodyObject == null) throw new ApplicationException (StringsManager.GetString("Cannot get outlook body property."));

    System.Type bodyType = bodyObject.GetType ();
    if (bodyType == null) throw new ApplicationException (StringsManager.GetString("Cannot get outlook body property type."));
    */

    foreach (string s in attachments)
      attachmentsType.InvokeMember ("Add", BindingFlags.InvokeMethod, null, attachmentsObject, new object [] {s, olByValue, ++bodyLength, s});
  }
}
#endregion

#region private members

private System.Type outlookType         = null;
private object      outlookObject       = null;
private object      namespaceMAPI       = null;
private System.Type namespaceMAPIType   = null;
object              outlookMailItem     = null;
System.Type         outlookMailItemType = null;

private const int olFolderDeleted   = 3;
private const int olFolderOutbox    = 4;
private const int olFolderSentItems = 5; 
private const int olFolderInbox     = 6; 
private const int olFolderCalendar  = 9; 
private const int olFolderContacts  = 10; 
private const int olFolderJournal   = 11; 
private const int olFolderNotes     = 12; 
private const int olFolderTasks     = 13; 
private const int olFolderDrafts    = 16; 

private const int olByValue      = 1;
private const int olByReference  = 4;
private const int olEmbeddeditem = 5;

#endregion
}
}
</code></pre>
