El elenco especificado no es válido que se obtenga MapioBject de MailItem. AddressEntry utilizando la unión tardía

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

  •  26-10-2019
  •  | 
  •  

Pregunta

Estoy tratando de obtener el MapioBject de MailItem.AddressEntry utilizando la vinculación tardía con Outlook.

Sigo obteniendo "la excepción ha sido lanzada por el objetivo de la invocación" y una excepción interna del "Cast especificado no es válido" y no tengo idea de por qué. No ha surgido nada en las búsquedas de Google, etc.

En primer lugar, sé que el Mapiobject está en desuso y no es visible a través de IntelliSense, pero funciona.

No puedo obtener el objeto sin problemas sin la vinculación tardía.

Aquí está el código:

/// <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);
        }
    }
}
¿Fue útil?

Solución

A menos que tenga que usar la interfaz MAPI como es, recomendaría encarecidamente usar el Mapiex Proyecto en CodeProject.

Esto hizo que nuestra integración MAPI fuera muy, muy bien.

Y, en el peor de los casos, el código fuente podría arrojar luz sobre preguntas específicas, como esta.

Otros consejos

En primer lugar, Mapiobject no está en desuso, simplemente invisible. En segundo lugar, ¿dónde se ejecuta su código? Si es un exe que no sea Outlook.exe (es decir, su código no está en un complemento COM), debe llamar a Mapiinitialize ().

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top