none
C# - How to get the name of account (i have 3 accounts configured at Outlook) when I receive an email? RRS feed

  • Question

  • At my Outlook 2013 I have 3 different accounts configured (two personal and 1 professional). When I receive an email at real time I need to know which account the mail is addressed, because I only want to work with the professional account!

    I use outlook.NewMailEx += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_NewMailExEventHandler(olApp_NewMailEx);   It is ok to get body, sender, subject but I cannot find the account name ! Example - my customer customer@company.com sends one email for my account novreis@mycompany.com, and a friend sends an email to my personal account novreis@hotmail.com. At my application I want only to process the novreis@mycompany.com and I cannot discover this account name on the mail received! thks

    Thursday, January 8, 2015 11:20 AM

All replies

  • Hello,

    You can use the To property of the MailItem class. It returns a semicolon-delimited string list of display names for the To recipients for the Outlook item.

    Also you can use the Recipients property of the MailItem class which returns a Recipients collection that represents all the recipients for the Outlook item. The Recipient class provides the Address and Name properties.

    Thursday, January 8, 2015 11:42 AM
  • If you were BCC'ed, the current user will not be in the Recipients collection. Take a look at the MailItem.SendUsingAccount property.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!


    Thursday, January 8, 2015 5:22 PM
  • Hi Eugene,

    I had already tried To property, but gives the Display Name of account "Manel Costa" and not the valid address manuel.costa@xxxxx.pt.

    Using recipients I have a truncation at address property     ....  and Manuel Costa at name property!

      Address "/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=20a6b37e7dea46b78df45e2127289558-manuel.cost" So unless, "display name" is = a valid mail address, no lucky!

    Thks anyway

    Thursday, January 8, 2015 9:21 PM
  • Hi Dimitry, for the moment I just need the To complete mail address!

    I will take a look at redemption!

    Thks

    Thursday, January 8, 2015 9:23 PM
  • This looks like a perfectly valid EX type address (as opposed to SMTP). Check if Recipient.AddressEntry.Type = "EX". If it is, use Recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, January 8, 2015 10:37 PM
  • Here is my code and what is strange, there are incoming mails (I see them at upper corner and at inboxes) with zero recipients, giving me the message below. So I will give up and change ,at Outlook accounts configuration ",display names" to the valid address names (ie Instead Manuel Costa I will put manuel.costa@xxxxx.pt), so I can use the mail.To property!

    private string GetSenderSMTPAddress(Outlook.MailItem mail)
               {
                   string smtpAddress = "";
                   if (mail.Recipients.Count > 0)
                   {
                       string teste = OlMailRecipientType.olTo.ToString();
                       foreach (Outlook.Recipient recip in mail.Recipients)
                       {
                           Outlook.AddressEntry entry = recip.AddressEntry;
                           if (entry.Type == "EX")
                           {
                               smtpAddress = GetSMTPAddressViaOutlookObjectModel(entry);
                           }
                       }
                       return smtpAddress;
                   }
                   else
                   {
                       MessageBox.Show("no recipients");
                       return "";
                   }
                  }

    Thanks anyway Dmitry

    Friday, January 9, 2015 4:07 PM
  • Here is my code and what is strange, there are incoming mails (I see them at upper corner and at inboxes) with zero recipients, giving me the message below. So I will give up and change ,at Outlook accounts configuration ",display names" to the valid address names (ie Instead Manuel Costa I will put manuel.costa@xxxxx.pt), so I can use the mail.To property!

    private string GetSenderSMTPAddress(Outlook.MailItem mail)
               {
                   string smtpAddress = "";
                   if (mail.Recipients.Count > 0)
                   {
                       string teste = OlMailRecipientType.olTo.ToString();
                       foreach (Outlook.Recipient recip in mail.Recipients)
                       {
                           Outlook.AddressEntry entry = recip.AddressEntry;
                           if (entry.Type == "EX")
                           {
                               smtpAddress = GetSMTPAddressViaOutlookObjectModel(entry);
                           }
                       }
                       return smtpAddress;
                   }
                   else
                   {
                       MessageBox.Show("no recipients");
                       return "";
                   }
                  }

    Thanks anyway Eugene

    Friday, January 9, 2015 4:07 PM
  • If you are looking for the sender email address (as the function name implies), why are you looping through the recipients?

    And if you need the recipients, why do you only return the last EX recipient (there can be multiple) and no address for the SMTP recipients?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, January 9, 2015 9:42 PM
  • Sorry Dmitry, but the name of function was one of several functions I tried after googling , so I must change it for better code reading. The content is quite different from the original! Even the line string teste = OlMailRecipientType.olTo.ToString(); is irrelevant!. I do not want the sender, only the first receiver(recipient?) No need second ones, CC or BCC! Once more sorry for this wrong name!

    Friday, January 9, 2015 11:44 PM
  • Are you sure the first EX recipient will be the mailbox owner?

    Have you looked into using SentUsingAccoutn instead of looping through the recipients?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Saturday, January 10, 2015 12:00 AM
  • This Business implies that only one To address is used (one to one relationship)!  All emails that are Replied, Forwarded and so on are not processed. As far I understand SentUsingAccount is the account I want to use when I send an email ... and I'm just receiving emails, not sending!

    Thks Dmitry

    Saturday, January 10, 2015 12:27 AM
  • but now what was confusing me is to receive in my function the message  "no recipients". There are no mails with 0 recipients, I think!!! One will exist always! What I am doing wrong, I don't know! As we are not at work hours, I am not receiving emails at  this moment, so I cannot make extensive tests! 
    Saturday, January 10, 2015 12:37 AM
  • Hi novreis,

    >>but now what was confusing me is to receive in my function the message  "no recipients". There are no mails with 0 recipients, I think!!! <<

    I am trying to reprdouce the issue in Outlook 2013, however failed. Here are my steps:

    1. Send email to myself and add myself to the cc

    2. The recipients I got were two

    How the item was recived in the inbox? Would you mind sharing the detail step we can reproduce the issue.

    Regards & Fei


    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 12, 2015 6:05 AM
    Moderator