Wednesday, October 5, 2011

Dynamics Ax 4: Using Outlook object to resolve email address from Exchange User

Today I was using the outlook object model to read emails from outlook.  I was using

   1: fromEmail = mailItemClass.get_SenderEmailAddress();

I thought this would return something like john.smith@perfect-10.tv.  Instead it returned something like this
"/O=PERFECT10/OU=FIRST GROUP/CN=RECIPIENTS/CN=JOHNSM"
I needed to get the smtp email address, after a lot of searching I did not find anything that really worked within Ax.  So I wrote a function to do this in Ax..


   1: str ResolveEmail(str _emailAddress)
   2: {
   3:     Microsoft.Office.Interop.Outlook.Recipients recipients;
   4:     Microsoft.Office.Interop.Outlook.Recipient recipient, newRecipient;
   5:     Microsoft.Office.Interop.Outlook.AddressEntry addressEntry;
   6:     Microsoft.Office.Interop.Outlook.ExchangeUserClass exchangeUser;
   7:  
   8:     Microsoft.Office.Interop.Outlook._Application outAppl;
   9:     Microsoft.Office.Interop.Outlook.MAPIFolder mapiFolder;
  10:     Microsoft.Office.Interop.Outlook.NameSpaceClass Nspace;
  11:     Microsoft.Office.Interop.Outlook.ItemsClass itemClass;
  12:     Microsoft.Office.Interop.Outlook.OlDefaultFolders olFolderInbox
  13:         = CLRInterop::parseClrEnum('Microsoft.Office.Interop.Outlook.OlDefaultFolders','olFolderInbox');
  14:  
  15:  
  16:     int numOfEmails;
  17:     int numOfFolders;
  18:     str value;
  19:     int cnt;
  20:  
  21:     ;
  22:         try
  23:         {
  24:  
  25:             recipients = mailItemClass.get_Recipients();
  26:             cnt = recipients.get_Count();
  27:             recipients.Add(_emailAddress);
  28:             cnt = recipients.get_Count();
  29:             recipients.ResolveAll();
  30:             recipient = recipients.get_Item(cnt);
  31:             addressEntry = recipient.get_AddressEntry();
  32:             exchangeUser = addressEntry.GetExchangeUser();
  33:             if(exchangeUser)
  34:             {
  35:                 value = exchangeUser.get_PrimarySmtpAddress();
  36:             }
  37:             else
  38:             {
  39:                 value = _emailAddress;
  40:             }
  41:  
  42:         }
  43:         catch(Exception::CLRError)
  44:         {
  45:             error('cannot read email');
  46:             continue;
  47:         }
  48: //    }
  49:         return value;
  50: }

 


NOTE #1: The Dynamics Ax code to get the mailItemClass this uses is a global variable on the class and so is the FolderClass.  See this article to get the first mailItem..  http://dc313.4shared.com/doc/J3LRQLgs/preview.html


NOTE #2:  I found a article http://anoriginalidea.wordpress.com/2008/01/11/getting-the-smtp-email-address-of-an-exchange-sender-of-a-mailitem-from-outlook-in-vbnet-vsto/

which probably works but seemed like overkill to me, but luckily there was a comment in the article that had a simple .Net solution, which is what I used to translate into my Dynamics Ax method.  Here is the .Net solution I used based off the comment..Just create a standard VB App for windows forms and copy paste this code, add a reference to outlook dll.


   1:  
   2: Imports Microsoft.Office.Interop
   3:  
   4:  
   5:  
   6:  
   7: Public Class Form1
   8:  
   9:  
  10:  
  11:     Private Sub btnButton1_Click(sender As System.Object, e As System.EventArgs) Handles btnButton1.Click
  12:         MsgBox(fnGetSMTPAddress("/O=PERFECT10/OU=FIRST ADMINISTRATIVE GROUP/CN=RECIPIENTS/CN=LARRYB"))
  13:     End Sub
  14:  
  15:     Public Function fnGetSMTPAddress(ExchangeMailAddress As String) As String
  16:         Dim objOutlook As Outlook.Application
  17:         Dim objMailItem As Outlook.MailItem
  18:  
  19:         objOutlook = New Outlook.Application
  20:         objMailItem = objOutlook.CreateItem(0)
  21:         objMailItem.To = ExchangeMailAddress
  22:         objMailItem.Recipients.ResolveAll()
  23:         fnGetSMTPAddress = objMailItem.Recipients.Item(1).AddressEntry.GetExchangeUser.PrimarySmtpAddress
  24:         objMailItem = Nothing
  25:         objOutlook = Nothing
  26:  
  27:     End Function
  28:  
  29:  
  30: End Class

No comments:

Post a Comment