1

I am developing an outlook add-in using add-in express. When I get a outbox mail item, I can see my email address under "mailItem.SenderEmailAddress" as in x500 format. Is there a way to convert it as a SMTP email address.

Here is my code :

        Outlook.Explorer expl = null;
        Outlook.Selection selection = null;
        Outlook.MailItem mailItem = null;
        Outlook.Recipient recipient = null;
        string recipientsEmail = "";

        try
        {
            expl = Globals.ObjOutlook.ActiveExplorer() as Outlook.Explorer; ;
            if (expl != null)
            {
                selection = expl.Selection;
                if (selection.Count == 1)
                {
                    if (selection[1] is Outlook.MailItem)
                    {
                        mailItem = (selection[1] as Outlook.MailItem);
                        if (mailItem != null)
                        {
                            string senderEmailAddress = mailItem.SenderEmailAddress;

What I have tried and Succeeded :- I know how to convert x500 type to SMTP using a Outlook.Recipient object. There I can get the "addressEntry" of the Recipient and obtain ExhangeUser, then go for the "PrimarySmtpAddress" of ExhangeUser. But I am not quiet sure about how to deal with SenderEmailAddress and convert it as SMTP.

Some interesting experiments :- Since the property of "Sender" is not available in the current mailitem object, I managed to use reflection to get the property of "Sender". But When I run following code the "propertyInfo" object value is getting null. I can't understand why.

Here is the code

//...
if (mailItem != null)
{
      var mailItem2 = GetNewObject(mailItem, "Sender", intArray);   
//...


public static object GetNewObject(Outlook.MailItem o, string popertyName, object[] parameters)
        {
            try
            {               
                PropertyInfo propertyInfo = o.GetType().GetProperty(popertyName); // This object is getting null
                return propertyInfo.GetType().GetProperty(popertyName).GetValue(o,parameters) as Outlook.MailItem;

            }
            catch (MissingMethodException ex)
            {
                // discard or do something
                Debug.DebugMessage(2, "AddinModule : Error in GetNewObject() : " + ex.Message);
                return null;
            }
        }

Please advice me. Thank you.

Kushan Randima
  • 2,174
  • 5
  • 31
  • 58
  • Since you tagged your question as "Outlook-Redemption", are you looking for a Redemption solution? – Dmitry Streblechenko Nov 18 '15 at 17:19
  • @DmitryStreblechenko, Yes, I was. Because I am always comfortable with redemption. But your comment about "PR_SENDER_ENTRYID" completely addressed ny question. Thanks a lot for that. – Kushan Randima Nov 20 '15 at 05:14
  • @DmitryStreblechenko: If you are interested please commit for this : http://stackoverflow.com/documentation/outlook-addin/commit – Kushan Randima Jul 27 '16 at 05:23

2 Answers2

1

You can obtain the AddressEntry of the sender using the Sender property like this:

Outlook.AddressEntry senderAddressEntry = mailItem.Sender;
Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62
  • Thank you. I also thought about this. But the property of "Sender" is not there in the mailtem. – Kushan Randima Nov 18 '15 at 12:55
  • What do you mean? Does it return null for example? Does this message come from an exchange mailbox? – Yacoub Massad Nov 18 '15 at 13:03
  • MailItem.Sender property was added in Outlook 2010? Are you using an older version of Outlook or just an old interop dll? – Dmitry Streblechenko Nov 18 '15 at 17:18
  • @DmitryStreblechenko, I need to support older versions of outlook too. So, I had to select the minimum version of outlook as 2007. I tried reflection to get the property of "Sender". I will update the question for more information. Thanks for the consideration – Kushan Randima Nov 19 '15 at 05:12
  • @Yacoub Massad, Visual Studio intellisense won't recognize the "Sender" as a property of mailitem. The reason is I am using an older version of assembly. Unfortunately I can't change it. I am trying to use reflection to get the property of "Sender". – Kushan Randima Nov 19 '15 at 05:14
  • In Outlook 2007, use MailItem.PropertyAccessor to retrieve the PR_SENDER_ENTRYID property (DASL name `http://schemas.microsoft.com/mapi/proptag/0x0C190102`), convert it to hex using PropertyAccessor.BinaryToString, use it to call Namespace.GetAddressEntryFromID.. – Dmitry Streblechenko Nov 19 '15 at 17:04
1

After reading the comment from "Dmitry Streblechenko" I was able to develop a fully working solution for my self. Here is the code

        private void adxRibBtnAddEmailAddress_OnClick(object sender, IRibbonControl control, bool pressed)
        {
            Outlook.MailItem mailItem = null;
            Outlook.Recipient recipient = null;
            string recipientsEmail = "";
            string sendersEmail = "";

            try
            {               
                mailItem = OutlookHelper.GetSelectedMailItem();
                if (mailItem != null)
                {
                    if (mailItem.SenderEmailType == "EX")
                        sendersEmail = GetSenderEmailAddress(mailItem);
                    else
                        sendersEmail = mailItem.SenderEmailAddress;

                    if (!string.IsNullOrEmpty(sendersEmail))
                    {
                        if (sendersEmail == Globals.OLCurrentUserEmail) // Sent mail
                        {
                            recipient = mailItem.Recipients[1];
                            if (recipient != null)
                            {
                                recipientsEmail = OutlookHelper.GetAddress(ref recipient);
                                if (!string.IsNullOrEmpty(recipientsEmail))
                                {
                                    // Do Something
                                }
                            }
                        }
                        else // inbox mail
                        {
                            // Do Something
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.DebugMessage(2, "AddinModule : Error in adxRibBtnsddemailaddress_OnClick() : " + ex.Message);
            }
            finally
            {
                if (recipient != null) Marshal.ReleaseComObject(recipient);
                if (mailItem != null) Marshal.ReleaseComObject(mailItem);
                Cursor.Current = Cursors.Default;
            }
        }

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

            string SenderID;
            string senderEmailAddress;

            try
            {
                // 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;

                return senderEmailAddress;
            }
            catch (Exception ex)
            {
                Debug.DebugMessage(2, "AddinModule : Error in adxRibBtnAddGenInteraction_OnClick() : " + 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