How to get sender's email address if user is an active directory user?

╄→尐↘猪︶ㄣ 提交于 2020-01-07 04:41:15

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/44178822/how-to-get-senders-email-address-if-user-is-an-active-directory-user

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!