Fusioni specificato non è valido Ottenere MAPIOBJECT da MailItem.AddressEntry utilizzando l'associazione tardiva

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

  •  26-10-2019
  •  | 
  •  

Domanda

Sto cercando di ottenere il MAPIOBJECT del MailItem.AddressEntry utilizzando l'associazione tardiva con Outlook.

Continuo a ricevere "Eccezione generata dalla destinazione di chiamata" e un'eccezione interna di "cast specificato non è valido" e non ho idea del perché. Nulla ha messo su ricerche di Google, ecc.

In primo luogo io so che il MAPIOBJECT è deprecato e non visibile attraverso intellisense ma opere.

posso ottenere l'oggetto senza problemi, senza tardiva.

:

Ecco il codice:

/// <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);
        }
    }
}
È stato utile?

Soluzione

A meno che non si deve utilizzare l'interfaccia MAPI come sei, mi sento di raccomandare vivamente di utilizzare il MAPIEx progetto in CodeProject.

Questo ha reso la nostra integrazione MAPI va molto, molto bene.

E, nel peggiore dei casi, il codice sorgente potrebbe far luce su questioni specifiche, come ad esempio questo.

Altri suggerimenti

In primo luogo, MAPIOBJECT non è deprecato, proprio invisibile. In secondo luogo, in cui viene eseguito il codice? Se si tratta di un exe diverso outlook.exe (vale a dire il codice non è in un componente aggiuntivo COM), è necessario chiamare MAPIInitialize ().

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top