11

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

administrator@orgname.com

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
Andriy Kozachuk
  • 743
  • 1
  • 13
  • 27

4 Answers4

16

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;
}
GTG
  • 4,914
  • 2
  • 23
  • 27
  • Is there a similar method to get the senders address in visual basic? – ASM2701 Aug 03 '14 at 15:08
  • 1
    Yes @ASM2701, you can to exactly the same in VB.NET. The VB syntax is different from the C# one, no semicolons or curly braces, but you should be able to figure it out without any big problems. – GTG Aug 05 '14 at 10:27
  • If you are interested please commit for this :http://stackoverflow.com/documentation/outlook-addin/commit – Kushan Randima Jul 28 '16 at 04:35
  • 1
    There is absolutely no reason to use Namespace.CreateRecipient. Just use MailItem.Sender.GetExchangeUser().PrimarySmtpAddress, – Dmitry Streblechenko Aug 26 '17 at 17:57
  • @DmitryStreblechenko I'm getting a "Attempted to read or write protected memory. This is often an indication that other memory is corrupt" error when trying to access the ```MailItem.Sender```. Any clue how to get around this? – Brandon Aug 28 '17 at 14:20
  • What version of Outlook are you using? – Dmitry Streblechenko Aug 28 '17 at 16:58
2

Here I am presenting a method that can be used to get sender's email address by passing an email item as a reference. The method it self will decide weather the sender's email address type is SMTP or Exchange. If it is Exchange, it will convert the email address into SMTP. Here is the code.

    internal static string GetSenderEmailAddress(ref Outlook.MailItem oM)
    {
        Outlook.PropertyAccessor oPA = null;
        Outlook.AddressEntry oSender = null;
        Outlook.ExchangeUser oExUser = null;

        string SenderID;
        string senderEmailAddress;

        try
        {                
            if (oM.SenderEmailAddress.Contains("@") && oM.SenderEmailAddress.Contains(".com")) //Handing smtp email addresses
            {
                senderEmailAddress = oM.SenderEmailAddress;
            }
            else //Handing exchange email addresses
            {
                // Create an instance of PropertyAccessor 
                oPA = oM.PropertyAccessor;
                // Obtain PidTagSenderEntryId and convert to string 
                SenderID = oPA.BinaryToString(oPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0C190102"));
                // Obtain AddressEntry Object of the sender 
                oSender = Globals.ObjNS.GetAddressEntryFromID(SenderID);

                oExUser = oSender.GetExchangeUser();
                senderEmailAddress = oExUser.PrimarySmtpAddress;
            }
            Debug.DebugMessage(3, "OutlookHelper : GetSenderEmailAddress() : Completed");
            return senderEmailAddress;
        }
        catch (Exception ex)
        {
            MessageBox.Show( ex.Message);
            return null;
        }
        finally
        {
            if (oExUser != null) Marshal.ReleaseComObject(oExUser);
            if (oSender != null) Marshal.ReleaseComObject(oSender);
            if (oPA != null) Marshal.ReleaseComObject(oPA);
        }
    }
Kushan Randima
  • 2,174
  • 5
  • 31
  • 58
0

You can use inspector to get current email as follow.

 Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
            if (inspector != null)
            {
                Outlook.MailItem  mi = inspector.CurrentItem as Outlook.MailItem;

              //Then identify whether email sender is exchange user or normal user
               string senderEmail=null;
               if (mi.SenderEmailType == "EX")
                {
                senderEmail = mi.Sender.GetExchangeUser().PrimarySmtpAddress;                    
                 }
              else
                {
                 senderEmail=mi.SenderEmailAddress;
                }
             }

you will get sender email in senderEmail variable.

Shyam sundar shah
  • 2,473
  • 1
  • 25
  • 40
0

email.Sender must be available in a higher version than outlook 2007

  • In what version of outlook is it available then? Or is it just a guess? – Leogout Jul 01 '20 at 08:37
  • According to this website and my test. https://stackoverflow.com/questions/24361726/how-can-i-get-the-sender-email-address-using-outlook-mailitem-in-vb-net – 桐人黑瞳 Jul 02 '20 at 09:17