Search code examples
c#reflectionexchange-serveroutlook-addinoutlook-redemption

Convert x500 email address into a smtp address


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.


Solution

  • 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);                
                }
            }