简体   繁体   English

如何使用Microsoft.Office.Interop.Outlook获取发件人电子邮件地址

[英]How to get Sender email address using Microsoft.Office.Interop.Outlook

I am using Microsoft.Office.Interop.Outlook in my C# code to read mail from PST file and I am facing issue when trying to get email address of sender. 我在C#代码中使用Microsoft.Office.Interop.Outlook从PST文件读取邮件,并且在尝试获取发件人的电子邮件地址时遇到问题。

I have tried the below code and I am getting email of the user who are there in the organization but not able to get the email of the users who left the organization or not active in AD. 我尝试了下面的代码,但收到组织中用户的电子邮件,但无法获得离开组织或未在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;

You should check SenderEmailType Property first. 您应该首先检查SenderEmailType属性。

_MailItem.SenderEmailType Property _MailItem.SenderEmailType属性

Returns a String (string in C#) that represents the type of entry for the e-mail address of the sender of the Outlook item, such as 'SMTP' for Internet address, 'EX' for a Microsoft Exchange server address, etc. Read-only. 返回一个字符串(C#中的字符串),该字符串表示Outlook项目发件人的电子邮件地址的条目类型,例如Internet地址的“ SMTP”,Microsoft Exchange服务器地址的“ EX”等。 -只要。

Please see also here. 也请在这里查看。
Get the SMTP address of the sender of a mail item 获取邮件项目发件人的SMTP地址

To determine the SMTP address for a received mail item, use the SenderEmailAddress property of the MailItem object. 若要确定已接收邮件项目的SMTP地址,请使用MailItem对象的SenderEmailAddress属性。 However, if the sender is internal to your organization, SenderEmailAddress does not return an SMTP address, and you must use the PropertyAccessor object to return the sender's SMTP address. 但是,如果发件人在您的组织内部,则SenderEmailAddress不会返回SMTP地址,您必须使用PropertyAccessor对象返回发件人的SMTP地址。

In the following code example, GetSenderSMTPAddress uses the PropertyAccessor object to obtain values that are not exposed directly in the Outlook object model. 在下面的代码示例中,GetSenderSMTPAddress使用PropertyAccessor对象来获取未直接在Outlook对象模型中公开的值。 GetSenderSMTPAddress takes in a MailItem. GetSenderSMTPAddress接收一个MailItem。 If the value of the SenderEmailType property of the received MailItem is "EX", the sender of the message resides on an Exchange server in your organization. 如果接收到的MailItem的SenderEmailType属性的值为“ EX”,则邮件的发件人位于组织中的Exchange服务器上。 GetSenderSMTPAddress uses the Sender property of the MailItem object to get the sender, represented by the AddressEntry object. 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;
    }
}

In Addition: 此外:
I misunderstood the question. 我误解了这个问题。

I do not have an answer to your question. 对于您的问题,我没有答案。
However, it seems that this can not be solved by the client side program. 但是,客户端程序似乎无法解决此问题。
Does the administrator need to do something? 管理员需要做些什么吗?

The following article may be a hint for something. 以下文章可能会暗示一些问题。
Overview of inactive mailboxes in Office 365 Office 365中的非活动邮箱概述

Office 365 User Email Settings Office 365用户电子邮件设置
Give mailbox permissions to another user in Office 365 - Admin Help 在Office 365中向其他用户授予邮箱权限-管理员帮助
Convert a user mailbox to a shared mailbox 将用户邮箱转换为共享邮箱
Open and use a shared mailbox in Outlook 在Outlook中打开并使用共享邮箱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM