【问题标题】:How to get Sender email address using Microsoft.Office.Interop.Outlook如何使用 Microsoft.Office.Interop.Outlook 获取发件人电子邮件地址
【发布时间】:2019-01-28 05:38:43
【问题描述】:

我在我的 C# 代码中使用 Microsoft.Office.Interop.Outlook 从 PST 文件中读取邮件,但在尝试获取发件人的电子邮件地址时遇到了问题。

我尝试了以下代码,我收到了组织中用户的电子邮件,但无法获得离开组织或在 AD 中不活跃的用户的电子邮件。

 string SenderEmailAddress = "";
        try
        {
            AddressEntry sender = mail.Sender;

            if (sender.AddressEntryUserType == OlAddressEntryUserType.olExchangeUserAddressEntry || sender.AddressEntryUserType == OlAddressEntryUserType.olExchangeRemoteUserAddressEntry)
            {
                ExchangeUser exchUser = sender.GetExchangeUser();
                if (exchUser != null)
                {
                    SenderEmailAddress = exchUser.PrimarySmtpAddress;
                }
            }
            else
            {
                SenderEmailAddress = mail.SenderEmailAddress;
            }
        }
        catch (System.Exception ex)
        {
            log.Log("Error Occured at getSenderEmailAddress() :: for " + mail.Sender + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace);
        }
        return SenderEmailAddress;

【问题讨论】:

    标签: c# outlook interop office-interop


    【解决方案1】:

    您应该先检查 SenderEmailType 属性。

    _MailItem.SenderEmailType Property

    返回一个字符串(C# 中的字符串),它表示 Outlook 项目发件人的电子邮件地址的条目类型,例如 Internet 地址的“SMTP”、Microsoft Exchange 服务器地址的“EX”、等只读。

    另请参阅此处。
    Get the SMTP address of the sender of a mail item

    要确定接收邮件项目的 SMTP 地址,请使用 MailItem 对象的 SenderEmailAddress 属性。但是,如果发件人在您的组织内部,SenderEmailAddress 不会返回 SMTP 地址,您必须使用 PropertyAccessor 对象返回发件人的 SMTP 地址。

    在以下代码示例中,GetSenderSMTPAddress 使用 PropertyAccessor 对象来获取未直接在 Outlook 对象模型中公开的值。 GetSenderSMTPAddress 接受一个 MailItem。如果收到的 MailItem 的 SenderEmailType 属性的值为“EX”,则邮件的发件人驻留在组织中的 Exchange 服务器上。 GetSenderSMTPAddress 使用 MailItem 对象的 Sender 属性来获取由 AddressEntry 对象表示的发件人。

    private string GetSenderSMTPAddress(Outlook.MailItem mail)
    {
        string PR_SMTP_ADDRESS =
            @"https://schemas.microsoft.com/mapi/proptag/0x39FE001E";
        if (mail == null)
        {
            throw new ArgumentNullException();
        }
        if (mail.SenderEmailType == "EX")
        {
            Outlook.AddressEntry sender =
                mail.Sender;
            if (sender != null)
            {
                //Now we have an AddressEntry representing the Sender
                if (sender.AddressEntryUserType ==
                    Outlook.OlAddressEntryUserType.
                    olExchangeUserAddressEntry
                    || sender.AddressEntryUserType ==
                    Outlook.OlAddressEntryUserType.
                    olExchangeRemoteUserAddressEntry)
                {
                    //Use the ExchangeUser object PrimarySMTPAddress
                    Outlook.ExchangeUser exchUser =
                        sender.GetExchangeUser();
                    if (exchUser != null)
                    {
                        return exchUser.PrimarySmtpAddress;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return sender.PropertyAccessor.GetProperty(
                        PR_SMTP_ADDRESS) as string;
                }
            }
            else
            {
                return null;
            }
        }
        else
        {
            return mail.SenderEmailAddress;
        }
    }
    

    另外:
    我误解了这个问题。

    我没有你的问题的答案。
    但是,客户端程序似乎无法解决这个问题。
    管理员需要做些什么吗?

    下面的文章可能是一些提示。
    Overview of inactive mailboxes in Office 365

    Office 365 User Email Settings
    Give mailbox permissions to another user in Office 365 - Admin Help
    Convert a user mailbox to a shared mailbox
    Open and use a shared mailbox in Outlook

    【讨论】:

    • 嗨@Kunif 谢谢你的回复,但我已经尝试过你提到的解决方案,它仍然给我空白值。任何其他解决方案都是最受欢迎的。
    • @praveenkandari 我也有这个问题。这似乎是不可能的,因为尽管寻找了几周,但我还没有找到一个可行的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 2020-09-25
    • 2014-08-13
    • 2018-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多