How to get sender's email address if user is a

2019-09-14 22:27发布

I am using get_SenderEmailAddress() of Outlook::_MailItem object to get sender's email address. But if user is an active directory user, then recipientitem.address looks like this: /o=organizationg/ou=exchange administrative group /cn=recipients/cn=xxxxxxxxxx.

Is there any other way to get sender's email address?

2条回答
Explosion°爆炸
2楼-- · 2019-09-14 22:55

That looks like a perfectly valid email address of type "EX" (as opposed to "SMTP").

If you want the SMTP address, use MailItem.Sender.GetExchangeUser().PrimarySmtpAddress. Be prepared to handle nulls and exceptions. But first check the MailItems.SenderEmailType property - if it is "SMTP", you can still use SenderEmailAddress.

查看更多
做自己的国王
3楼-- · 2019-09-14 23:21

I am using this to get the Sender Mail Address.

 private string GetSenderSMTPAddress(Outlook.MailItem mail)
    {
        try
        {
            string PR_SMTP_ADDRESS =
                    @"http://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;
            }
        }
        catch (Exception ex)
        {
            return null;
        }
    }
查看更多
登录 后发表回答