Fetch Email Address for SentOnBehalfOfName in outlook 2007 VSTO code

215 Views Asked by At

I am developing Outlook 2007 VSTO addin. I need to fetch the email address specified in from section like given below:

[email protected]; on behalf of; KANxyz SAXYZ [[email protected]] enter image description here

I need to fetch [[email protected]] in code. But I just get SentOnBehalfOfName property that only returns the name (in this case "KANxyz SAXYZ").

        try
        {

            Outlook.Selection objSelection = null;
            // Get the Application object
            Microsoft.Office.Interop.Outlook.Application application = Globals.ThisAddIn.Application;

            try
            {
                var window = application.ActiveWindow();
                try
                {
                    objSelection = application.ActiveExplorer().Selection;
                }
                catch (System.Exception ex)
                {
                    //Ignore; NULL check below will branch out 
                }
                objMail = default(Outlook.MailItem);

                try
                {
                    objMail = objSelection[1];
                }
                catch (Exception ex)
                {
                }
                if ((objMail != null) && !string.IsNullOrEmpty(objMail.SenderEmailAddress))
                {
                    email = objMail.SenderEmailAddress;
                    emailFrom = objMail.SentOnBehalfOfName;
                }
            }
            catch { }

            if ((objMail == null) || string.IsNullOrEmpty(objMail.SenderEmailAddress))
            {
                // Get the active Inspector object and check if is type of MailItem
                Microsoft.Office.Interop.Outlook.Inspector inspector = application.ActiveInspector();
                if (inspector != null)
                {
                    mailItem = inspector.CurrentItem as Microsoft.Office.Interop.Outlook.MailItem;
                    if (mailItem != null)
                    {
                        email = mailItem.SenderEmailAddress;
                        emailFrom = objMail.SentOnBehalfOfName;
                    }       
                }
            }
        }
        catch (Exception ex){ }
0

There are 0 best solutions below