Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 430
Andriy Kozachuk Avatar asked Dec 28 '11 11:12

Andriy Kozachuk


1 Answers

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;
}
like image 194
GTG Avatar answered Sep 26 '22 02:09

GTG