Search code examples
c#vstooutlook-addinoutlook-2007

Outlook 2007 vsto add-in. Get email sender address


I have a VSTO Outlook 2007 add-in. I am trying to get sender e-mail address when new email comes to Inbox.
To do it I use the following code:

void inboxItems_ItemAdd(object Item)
{     
    Outlook.MailItem mailItem = Item as Outlook.MailItem;

    if (mailItem != null)
        string emailAdress = mailItem.SenderEmailAddress;  
}

The problem is when e-mail comes from the same domain, emailAdress contains LDAP address like

/O=FIRST ORGANIZATION/OU=FIRST ADMINISTRATIVE GROUP/CN=RECIPIENTS/CN=ADMINISTRATOR

while I want to get SMTP address like

[email protected]

My question is how to get SMTP sender address of e-mail from internal domain?

P. S.
In Outlook 2010 this problem can be solved by using Sender property. But it is not supported in 2007.

item.Sender.GetExchangeUser().PrimarySmtpAddress

Solution

  • In Outlook 2007 you can do it like this:

    private string GetSmtpAddress(Outlook.MailItem oItem)
    {
        Outlook.Recipient recip;
        Outlook.ExchangeUser exUser;
        string sAddress;
    
        if (oItem.SenderEmailType.ToLower() == "ex")
        {
            recip = Globals.ThisAddIn.Application.GetNamespace("MAPI").CreateRecipient(oItem.SenderEmailAddress);
            exUser = recip.AddressEntry.GetExchangeUser();
            sAddress = exUser.PrimarySmtpAddress;
        }
        else
        {
            sAddress = oItem.SenderEmailAddress.Replace("'", "");
        }
        return sAddress;
    }