none
Outlook > Add-In > Mail Item > Address > How to get the email address for Receipents & Sender? RRS feed

  • Question

  • Hello,

    I am developing an outlook add-in and unable to retrieve the email address from the To,CC,BCC field & the "Sender".

    I have written the below code - 

     //Prepare list of receipients.
                List<string> emailIds = new List<string>();
                foreach (Outlook.Recipient recipient in mailItem.Recipients)
                { emailIds.Add(recipient.Address); }
                emailIds.Add(mailItem.SenderEmailAddress);

    In the code above, if the receipient/sender address is saved in the address book, I get the details else "null".

    Could someone please help me in fetching the correct email address irrespective of it been saved in the address book or not?

    Thanks!


    -Vinay Pugalia
    If a post answers your question, please click "Mark As Answer" on that post or "Vote as Helpful".
    Web : Inkey Solutions
    Blog : My Blog
    Email : Vinay Pugalia

    Thursday, January 15, 2015 7:59 AM

Answers

  • Hi Vinay,

    For this requirement, we could get address through PropertyAccessor object.

    For example:

    const string PR_SMTP_ADDRESS =
           "http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
            void Application_ItemSend(object Item, ref bool Cancel)
            {
                Outlook.MailItem mail = (Outlook.MailItem)Item;
               foreach(Outlook.Recipient r in mail.Recipients)
               {
                   Outlook.PropertyAccessor pa = r.PropertyAccessor;
                   string userAddress = pa.GetProperty(PR_SMTP_ADDRESS).ToString();
               }
             //TODO
           }

    More information, please refer to:

    # Obtain the E-mail Address of a Recipient

    http://msdn.microsoft.com/en-us/library/office/ff868695(v=office.15).aspx

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by vinaypugalia Friday, January 30, 2015 4:10 AM
    Friday, January 16, 2015 6:55 AM
    Moderator

All replies

  • These recipients may be Exchange users.  You will need to obtain the AddressEntry object from the recipient to get an ExchangeUser object, then evaluate the PrimarySMTPAddress property:

    http://msdn.microsoft.com/en-us/library/office/microsoft.office.interop.outlook.addressentry.getexchangeuser(v=office.15).aspx


    Eric Legault (MVP: Outlook)
    Co-author, Microsoft Office 2013 Professional Step-By-Step
    Try Outlook Touch!

    Thursday, January 15, 2015 3:59 PM
    Moderator
  • Hi,

    Since the question is more about Outlook Development, I've moved this thread to Outlook for Developers forum, thus you can get more dedicated support from the experts here.

    Regards,

    Melon Chen
    TechNet Community Support


    It's recommended to download and install Configuration Analyzer Tool (OffCAT), which is developed by Microsoft Support teams. Once the tool is installed, you can run it at any time to scan for hundreds of known issues in Office programs.

    Friday, January 16, 2015 6:05 AM
  • Hello,

    The below code when executed after the mail is sent, has worked for me - 

    //Prepare list of receipients.
                List<string> emailIds = new List<string>();
                foreach (Outlook.Recipient recipient in mailItem.Recipients)
                { emailIds.Add(recipient.AddressEntry.Address); }
                emailIds.Add(mailItem.SenderEmailAddress);
    Hope, this helps.

    -Vinay Pugalia
    If a post answers your question, please click "Mark As Answer" on that post or "Vote as Helpful".
    Web : Inkey Solutions
    Blog : My Blog
    Email : Vinay Pugalia

    • Marked as answer by vinaypugalia Friday, January 16, 2015 6:35 AM
    • Unmarked as answer by vinaypugalia Friday, January 30, 2015 4:10 AM
    Friday, January 16, 2015 6:35 AM
  • Hi Vinay,

    For this requirement, we could get address through PropertyAccessor object.

    For example:

    const string PR_SMTP_ADDRESS =
           "http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
            void Application_ItemSend(object Item, ref bool Cancel)
            {
                Outlook.MailItem mail = (Outlook.MailItem)Item;
               foreach(Outlook.Recipient r in mail.Recipients)
               {
                   Outlook.PropertyAccessor pa = r.PropertyAccessor;
                   string userAddress = pa.GetProperty(PR_SMTP_ADDRESS).ToString();
               }
             //TODO
           }

    More information, please refer to:

    # Obtain the E-mail Address of a Recipient

    http://msdn.microsoft.com/en-us/library/office/ff868695(v=office.15).aspx

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by vinaypugalia Friday, January 30, 2015 4:10 AM
    Friday, January 16, 2015 6:55 AM
    Moderator
  • Hello Starain,

    Thanks for the code snippet.

    However, I fail to understand how is this different from the code I have shared?

    Could you please help me in understanding the concept?

    Thanks!


    -Vinay Pugalia
    If a post answers your question, please click "Mark As Answer" on that post or "Vote as Helpful".
    Web : Inkey Solutions
    Blog : My Blog
    Email : Vinay Pugalia

    Friday, January 16, 2015 12:26 PM
  • Hi Vinay,

    Base on my test with outlook 2013, if the recipients are saved in the address book, the value of recipient.Address/ recipient.AddressEntry.Address is not real address, we could to get the address through PropertyAccessor object.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 19, 2015 1:26 AM
    Moderator
  • Hello Starain,

    I replaced my code with the "PropertyAccessor" code you have shared & it worked for me the same way as it worked for recipient.Address/ recipient.AddressEntry.Address irrespective of the recipient existing in the AddressBook contacts or not.

    Could you please explain my which approach guarantees to return correct email Ids every time?

    Thanks,

    Vinay 


    -Vinay Pugalia
    If a post answers your question, please click "Mark As Answer" on that post or "Vote as Helpful".
    Web : Inkey Solutions
    Blog : My Blog
    Email : Vinay Pugalia

    Monday, January 19, 2015 6:32 AM
  • Hi Vinay,

    What’s the version of Outlook that you used?

    What’s the value your get if the recipients are existing in the address book?

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 19, 2015 8:54 AM
    Moderator
  • Hello Starain,

    I am using Outlook 2013.

    I get proper email address irrespective of the contact being saved to the address book or not.

    Also, as I said, I get the same output in my - "List<string> emailIds" from the above code snippet.


    -Vinay Pugalia
    If a post answers your question, please click "Mark As Answer" on that post or "Vote as Helpful".
    Web : Inkey Solutions
    Blog : My Blog
    Email : Vinay Pugalia

    Monday, January 19, 2015 9:53 AM
  • Hi Vinay,

    The value that I get is like "/o=MMS/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=Starain Chen 9f04709c-d737-485c-9f25-c58c3ba58dc9” if the recipient is existing in the address book.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 20, 2015 7:50 AM
    Moderator
  • Hello Starain,

    I am sorry but I do not understand why this is happening as I get proper results.

    Coming to the recipient from the address book, I just want to re-confirm what we mean by "recipient from the address book". By this, I mean that whether my recipient is a contact in the outlook address book or not, I get proper email address.

    Please confirm are we on the same page? As other than this, I do not see any difference.

    Thanks!


    -Vinay Pugalia
    If a post answers your question, please click "Mark As Answer" on that post or "Vote as Helpful".
    Web : Inkey Solutions
    Blog : My Blog
    Email : Vinay Pugalia

    Tuesday, January 20, 2015 8:07 AM
  • Hi Vinay,

    You may share the project on the OneDrive, we will check the result in our environment.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 21, 2015 3:12 AM
    Moderator
  • Hello Starain,

    Project sharing is not possible due to NDA.

    If you want me to share any specific code snippet, I can prepare a dummy code & share it.

    Please guide.


    -Vinay Pugalia
    If a post answers your question, please click "Mark As Answer" on that post or "Vote as Helpful".
    Web : Inkey Solutions
    Blog : My Blog
    Email : Vinay Pugalia

    Wednesday, January 21, 2015 4:32 AM
  • Hi Vinay,

    You could create a new project with the necessary logic (get recipient), then share this project.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 21, 2015 5:53 AM
    Moderator
  • Sure, would share one soon.

    Thanks!


    -Vinay Pugalia
    If a post answers your question, please click "Mark As Answer" on that post or "Vote as Helpful".
    Web : Inkey Solutions
    Blog : My Blog
    Email : Vinay Pugalia

    Wednesday, January 21, 2015 1:59 PM
  • Hello Starain,

    Below is the necessary logic which works for me - 

    //Prepare list of receipients.
                  List<string> emailIds = new List<string>();
                  foreach (Outlook.Recipient recipient in mailItem.Recipients)
                  {
                    string emailId = GetEmailAddress(recipient);
    
                    if (!String.IsNullOrEmpty(emailId) &&
                        !emailIds.Contains(emailId))
                    { emailIds.Add(emailId); }
                  }
    
    
     private string GetEmailAddress(Outlook.Recipient recipient)
        {
          string returnValue = string.Empty;
          try
          {
            Outlook.PropertyAccessor propertyAccessor = recipient.PropertyAccessor;
            returnValue = GetEmailAddress(propertyAccessor);
          }
          catch
          {
            //Suppress
          }
          return returnValue;
        }
    
    
    private string GetEmailAddress(Outlook.PropertyAccessor propertyAccessor)
        {
    const string PR_SMTP_ADDRESS = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
          string returnValue = string.Empty;
          try
          {
            returnValue = propertyAccessor.GetProperty(PR_SMTP_ADDRESS).ToString();
          }
          catch
          {
            //Suppress
          }
          return returnValue;
        }
    Hope, the above code helps!


    -Vinay Pugalia
    If a post answers your question, please click "Mark As Answer" on that post or "Vote as Helpful".
    Web : Inkey Solutions
    Blog : My Blog
    Email : Vinay Pugalia

    Thursday, January 29, 2015 12:00 PM
  • Hello Starain,

    I think the difference might be in the environments. I use locally installed Outlook 2013 & you might be using Office 365.


    -Vinay Pugalia
    If a post answers your question, please click "Mark As Answer" on that post or "Vote as Helpful".
    Web : Inkey Solutions
    Blog : My Blog
    Email : Vinay Pugalia

    Thursday, January 29, 2015 1:07 PM
  • Hello Starain,

    Could you please help me to retrieve the correct email address of the current user?

    Thanks!


    -Vinay Pugalia
    If a post answers your question, please click "Mark As Answer" on that post or "Vote as Helpful".
    Web : Inkey Solutions
    Blog : My Blog
    Email : Vinay Pugalia

    Thursday, January 29, 2015 2:00 PM
  • Hi vinaypugalia,

    Base on your code, the solution is the same as my (using PropertyAccessor object) and it is the correct solution, however it isn’t the same as you provided (the marked answer).

    On the other hand, I use local installed outlook 2013 instead of office 365.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, January 30, 2015 2:38 AM
    Moderator
  • Hello Starain,

    Yes, you are right that my current code is same as what you had guided & hence I have marked your reply as answer & un-marked mine.

    But please note that my previous code with 

    recipient.AddressEntry.Address

    works perfectly well with a locally installed outlook 365 but I had to change the code as it was not giving expected results for Office 365 users.

    Thanks for your support!


    -Vinay Pugalia
    If a post answers your question, please click "Mark As Answer" on that post or "Vote as Helpful".
    Web : Inkey Solutions
    Blog : My Blog
    Email : Vinay Pugalia

    Friday, January 30, 2015 4:14 AM