指定的转换无效使用后期绑定从 MailItem.AddressEntry 获取 MAPIOBJECT

发布于 2024-12-25 22:29:22 字数 2723 浏览 3 评论 0原文

我正在尝试使用 Outlook 的后期绑定来获取 MailItem.AddressEntry 的 MAPIOBJECT。

我不断收到“调用目标已引发异常”和“指定的演员无效”的内部异常,我不知道为什么。 Google 搜索等上没有任何结果。

首先,我知道 MAPIOBJECT 已被弃用,并且通过智能感知不可见,但可以使用。

我可以毫无问题地获取对象,而无需后期绑定。

这是代码:

/// <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);
        }
    }
}

I'm trying to get the MAPIOBJECT of the MailItem.AddressEntry using Late Binding with Outlook.

I keep getting "Exception has been thrown by target of invocation" and an inner exception of "Specified Cast is Invalid" and I have no idea why. Nothing has come up on Google searches etc.

Firstly I know that the MAPIOBJECT is deprecated and not visible through intellisense but works.

I can get the object no problems without late binding.

Here is the 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);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸠书 2025-01-01 22:29:22

除非您必须按原样使用 MAPI 界面,否则我强烈建议使用 MAPIEx 项目。

这使得我们的 MAPI 集成进行得非常非常顺利。

而且,最坏的情况是,源代码可以阐明特定问题,例如这个。

Unless you have to use the MAPI interface as you are, I would strongly recommend using the MAPIEx project in CodeProject.

This made our MAPI integration go very, very smoothly.

And, worst case, the source code could shed light on specific questions, such as this.

还在原地等你 2025-01-01 22:29:22

首先,MAPIOBJECT 并未被弃用,只是不可见。
其次,你的代码在哪里运行?如果它是 Outlook.exe 以外的 exe(即您的代码不在 COM 加载项中),则必须调用 MAPIInitialize()。

Firstly, MAPIOBJECT is not deprecated, just invisible.
Secondly, where does your code run? If it is an exe other than outlook.exe (i.e. your code is not in a COM add-in), you must call MAPIInitialize().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文