简体   繁体   中英

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.

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.

 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.

_MailItem.SenderEmailType Property

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.

Please see also here.
Get the SMTP address of the sender of a mail item

To determine the SMTP address for a received mail item, use the SenderEmailAddress property of the MailItem object. 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.

In the following code example, GetSenderSMTPAddress uses the PropertyAccessor object to obtain values that are not exposed directly in the Outlook object model. GetSenderSMTPAddress takes in a 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. GetSenderSMTPAddress uses the Sender property of the MailItem object to get the sender, represented by the AddressEntry object.

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 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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