mercredi 18 novembre 2015

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.





Aucun commentaire:

Enregistrer un commentaire