Der angegebene Cast ist ungültig, wenn Sie MapiObject von MailItem.Addresentry mit einer späten Bindung erhalten

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

  •  26-10-2019
  •  | 
  •  

Frage

Ich versuche, das MapiObject of the MailItem.adDressEntry mit einer späten Bindung mit Outlook zu erhalten.

Ich bekomme immer wieder "Ausnahme wurde durch das Ziel der Berufung" und eine innere Ausnahme von "Spezifizierter Besetzung ist ungültig" und ich habe keine Ahnung, warum. Bei Google -Suche usw. ist nichts aufgetaucht.

Erstens weiß ich, dass das MapiObject veraltet und nicht durch Intellisense sichtbar ist, sondern funktioniert.

Ich kann das Objekt ohne Probleme ohne verspätete Bindung bekommen.

Hier ist der Code:

/// <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);
        }
    }
}
War es hilfreich?

Lösung

Wenn Sie die Mapi -Schnittstelle nicht so verwenden müssen, wie Sie sind, würde ich dringend empfehlen, die zu verwenden Mapiex Projekt in CodeProject.

Dies machte unsere Mapi -Integration sehr, sehr reibungslos.

Und im schlimmsten Fall könnte der Quellcode auf bestimmte Fragen wie diese beleuchten.

Andere Tipps

Erstens ist MapiObject nicht veraltet, nur unsichtbar. Zweitens, wo wird Ihr Code ausgeführt? Wenn es sich um eine andere Exe als Outlook.exe handelt (dh Ihr Code ist nicht in einem COM-Add-In), müssen Sie Mapiinitialize () aufrufen.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top