2012-01-10 4 views
1

OutlookでLate Bindingを使用してMailItem.AddressEntryのMAPIOBJECTを取得しようとしています。指定されたキャストが無効です。Late Bindingを使用してMailItem.AddressEntryからMAPIOBJECTを取得する

「呼び出しの対象によって例外がスローされました」と「指定されたキャストが無効です」の内部例外が発生し続けている理由がわかりません。 Google検索などで何も起きていません。

まず、MAPIOBJECTは非推奨であり、intellisenseでは表示されませんが、動作します。

遅延バインディングなしで問題なくオブジェクトを取得できます。

/// <summary> 
/// Gets the MAPI Object from the AddressEntry of the new recipient. 
/// </summary> 
/// <param name="senderName">The name of the sender to add to the recipients.</param> 
/// <param name="outlookApplication">The Outlook Application instance.</param> 
private static object GetMapiObject(string senderName, object outlookApplication) 
{ 
    var mailItem = InvokeMember("CreateItem", outlookApplication, new object[] { 0 }); 
    var recipients = GetProperty("Recipients", mailItem); 
    var recipient = InvokeMember("Add", recipients, new object[] { senderName }); 

    InvokeMember("Resolve", recipient, new object[] {}); 
    var addressEntry = GetProperty("AddressEntry", recipient); 

    var mapiObject = GetProperty("MAPIOBJECT", addressEntry); // Error occurs here. 
    return mapiObject; 
} 

/// <summary> 
/// Gets a property of an instance by its name 
/// </summary> 
/// <param name="propertyName">The property name to get.</param> 
/// <param name="instance">The object to get the property from.</param> 
/// <returns>The resulting object.</returns> 
private static object GetProperty(string propertyName, object instance) 
{ 
    Type type = instance.GetType(); 
    return type.InvokeMember(propertyName, BindingFlags.GetProperty, null, instance, new object[] { }); 
} 

/// <summary> 
/// Invoke an object by its method and type - takes parameters. 
/// </summary> 
/// <param name="method">The method to invoke.</param> 
/// <param name="instance">The object that contains the method to invoke.</param> 
/// <param name="parameters">The parameters to pass to the method.</param> 
/// <returns>The resulting object.</returns> 
private static object InvokeMember(string method, object instance, object[] parameters) 
{ 
    try 
    { 
     Type type = instance.GetType(); 
     return type.InvokeMember(method, BindingFlags.InvokeMethod, null, instance, parameters); 
    } 
    catch (Exception ex) 
    { 
     switch (method) 
     { 
      case "SaveAsFile": 
       throw new System.IO.IOException("Error occurred during \"SaveAsFile\" for attachments. Attachment filename may be too long. ", ex);             

      default: 
       throw new TargetInvocationException("Handled error at Invoke Member. Method Name: " + method, ex); 
     } 
    } 
} 

答えて

2

MAPIインターフェイスをそのまま使用する場合を除き、CodeProjectでMAPIExプロジェクトを使用することを強くお勧めします。

これにより、MAPIの統合が非常にスムーズに行われました。

そして、最悪の場合、ソースコードは、このような特定の質問に光を当てる可能性があります。

+0

ありがとう、私はそれを使い終わった!それは非常に有益で、私が後にしたことをすることができました。 – doiley

+3

管理されたMAPI **の[最新の高品質**記事](http://www.codeproject.com/Articles/455823/Managed-MAPI-Part-1-Logon-MAPI-Session-and)があります。 -Retriev)をコードプロジェクトに追加しました。 –

1

まず、MAPIOBJECTは廃止されていない、ただ目に見えない:ここで

はコードです。 第2に、コードはどこで実行されますか? outlook.exe以外のexe(つまり、コードがCOMアドインにない)の場合は、MAPIInitialize()を呼び出す必要があります。

+0

MSDNによれば、それは償却されています。私のコードはOutlookの外で実行されます。私はこの行の前にそれを追加しようとしました:var mapiObject = GetProperty( "MAPIOBJECT"、addressEntry);しかし役に立たない。これは正しい呼び出しです:MAPIInitialize(IntPtr.Zero); – doiley

+0

はい、うまくいくはずです。何が起こるのですか?なぜあなたはIMailUser MAPIオブジェクトが必要ですか? –

+0

Alos、適切なDLLからエクスポートされたMAPIInitializeを呼び出してもよろしいですか? –