Outlook : How to get email from Recipient field?

有些话、适合烂在心里 提交于 2019-12-06 23:26:39

问题


I'm attempting to get the email address typed into the To field of a compose mail window.

I try to get the Address property of a Recipient, which according to VS, should give me the email.

I am instead receiving a string that looks like this:

"/c=US/a=att/p=Microsoft/o=Finance/ou=Purchasing/s=Furthur/g=Joe"

How can I get the email address in the recipient field?

My code so far:

List <string> emails = new List<string>();

if (thisMailItem.Recipients.Count > 0)
{
    foreach (Recipient rec in thisMailItem.Recipients)
    {
        emails.Add(rec.Address);
    }
}
return emails;

回答1:


Can you try this ?

emails.Add(rec.AddressEntry.Address);

Reference link

EDIT:

I don't have the right environment to test so I'm just guessing all this, but how about

string email1Address = rec.AddressEntry.GetContact().Email1Address;

or .Email2Adress or .Email3Address

Also there is,

rec.AddressEntry.GetExchangeUser().Address

that you might want to try.




回答2:


Try this

private string GetSMTPAddressForRecipients(Recipient recip)
        {
            const string PR_SMTP_ADDRESS =
                "http://schemas.microsoft.com/mapi/proptag/0x39FE001E";

            PropertyAccessor pa = recip.PropertyAccessor;
            string smtpAddress = pa.GetProperty(PR_SMTP_ADDRESS).ToString();
            return smtpAddress;

        }

This is available on MSDN here

I have used the same way to get email addresses in my application and its working.




回答3:


the AddressEntry also has an SMTPAddress property that exposes the primary smtp adress of the user.




回答4:


I don't know if this helps or how accurate

it is, a sample

    private string GetSmtp(Outlook.MailItem item)
        {
            try
            {
                if (item == null || item.Recipients == null || item.Recipients[1] == null) return "";
                var outlook = new Outlook.Application();
                var session = outlook.GetNamespace("MAPI");
                session.Logon("", "", false, false);
                var entryId = item.Recipients[1].EntryID;
                string address = session.GetAddressEntryFromID(entryId).GetExchangeUser().PrimarySmtpAddress;
                if (string.IsNullOrEmpty(address))
                {
                    var rec = item.Recipients[1];
                    var contact = rec.AddressEntry.GetExchangeUser();
                    if (contact != null)
                        address = contact.PrimarySmtpAddress;
                }
                if (string.IsNullOrEmpty(address))
                {
                    var rec = item.Recipients[1];
                    var contact = rec.AddressEntry.GetContact();
                    if (contact != null)
                        address = contact.Email1Address;
                }
                return address;
            }
            finally
            {

            }
        }


来源:https://stackoverflow.com/questions/5036995/outlook-how-to-get-email-from-recipient-field

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