2012-01-10 3 views
1

바인딩 사용 MailItem.AddressEntry에서 MAPIOBJECT 얻기 잘못된 것입니다.지정한 캐스트가 늦게가 나는 Outlook과 늦은 바인딩 사용 MailItem.AddressEntry의 MAPIOBJECT을 얻으려고

내가와의 내부 예외 "예외가 호출 대상이 throw되었습니다"가 계속 "지정한 캐스트 잘못되었습니다"내가 왜 아무 생각이 없습니다. 아무것도

첫째 나는 MAPIOBJECT는 사용되지 않으며 인텔리하지만 작품을 통해 보이지 않는 것을 알고 Google 검색 등을 마련하지 않았다.

늦은 바인딩없이 문제없이 개체를 가져올 수 있습니다.

/// <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이되지되지 않으며, 단지 눈에 보이지 않는 : 여기

는 코드입니다. 둘째, 코드가 실행되는 위치는 무엇입니까? outlook.exe 이외의 exe 인 경우 (즉, 코드가 COM 추가 기능에 없음) MAPIInitialize()를 호출해야합니다.

+0

MSDN에서는 가치가 하락한다고 말합니다. 내 코드가 Outlook 외부에서 실행됩니다. 이 줄 앞에 추가하려고 : var mapiObject = GetProperty ("MAPIOBJECT", addressEntry); 그러나 아무 소용이 없습니다. 이것은 올바른 호출입니다 : MAPIInitialize (IntPtr.Zero); – doiley

+0

예, 작동합니다. 무슨 일이야? 왜 당신은 IMailUser MAPI 객체가 필요합니까? –

+0

Alos, 올바른 dll에서 내 보낸 MAPIInitialize를 호출 하시겠습니까? –

관련 문제