Указанный актерский состав недействителен получение MapiObject от mailitem.addressentry с использованием позднего привязки

StackOverflow https://stackoverflow.com/questions/8811812

  •  26-10-2019
  •  | 
  •  

Вопрос

Я пытаюсь получить MapiObject of the mailitem.addressentry с использованием позднего привязки с Outlook.

Я продолжаю получать «Исключение, было брошено в цель для вызова», и внутреннее исключение «Указанный актерский состав недействителен», и я понятия не имею, почему. Ничто не появилось в поиске 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);
        }
    }
}
Это было полезно?

Решение

Если вам не нужно использовать интерфейс MAPI, как вы, я настоятельно рекомендую использовать Mapiex Проект в CodeProject.

Это заставило нашу интеграцию MAPI идти очень, очень гладко.

И, в худшем случае, исходный код может пролить свет на конкретные вопросы, такие как этот.

Другие советы

Во -первых, MapiObject не устарел, просто невидим. Во -вторых, где работает ваш код? Если это exe, отличный от Outlook.exe (то есть ваш код не находится в дополнении Com), вы должны вызвать Mapiinitialize ().

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top