Obtain SMTP Address using CDO/Outlook in managed environments


If you were targeting office 2003 clients, you can download the Office 2003 primary interop libraries here:

http://www.microsoft.com/downloads/details.aspx?familyid=3c9a983a-ac14-4125-8ba0-d36d67e0f4ad&displaylang=en

 

This will allow your application to communicate with the Outlook object model (OOM) using interop.

Once you have installed and connected with this, then you can query the OOM.

 

For example:

Outlook.Application myApp = new Outlook.Application();

Outlook.NameSpace myOlns = myApp.GetNamespace("MAPI");

MessageBox.Show(myOlns.CurrentUser.Address); // to show email address

 

However there are certain attributes that are not exposed by the Outlook object model but you can access them using another layer of mapi access called Collaboration data objects (CDO).

 

There are no primary interop files created for cdo but you can still use C# using the interop layer but this is not tested fully nor supported. CDO is designed for the older programming environments such as Visual Studio 6, Vb6 and Vc++. CDO is an outdated library for managed environments hence no support for it but programmers have used this successfully.  Here is some information and steps to help you get the senders smtp address using c# and cdo using reflection

 

1.  Start Microsoft Visual Studio .NET. On the File menu, click New and then click Project . Select Console Application from the Visual C# Projects types. Class1.cs is created by default. 

2.  Add a reference to the Microsoft CDO 1.21 Object Library . To do this, follow these steps: a.  On the Project menu, click Add Reference . 

b.  On the COM tab, locate Microsoft CDO 1.21 Object Library and click Select . 

c.  Click OK in the Add References dialog box to accept your selections. If you receive a prompt to generate wrappers for the libraries you selected, click Yes . 

 

3.  In the code window, replace the whole code with: namespace Cdo121

{

 using System;

 using System.Reflection;

 

 class Class1

 {

  [STAThread]

  static void Main(string[] args)

  {

   // Will use vEmpty for Empty parameter

   Object vEmpty = Missing.Value;

   try

   {

    int g_PR_SMTP_ADDRESS_W = 0x39FE001F;  

 

    // Create Mapi Session

    MAPI.Session oSession = new MAPI.Session();

    oSession.Logon(vEmpty, vEmpty, true, true, 0, true, vEmpty);

 

    // Get the first Message from INbox

    MAPI.Folder oFolder = (MAPI.Folder)oSession.Inbox;

    Console.WriteLine("Folder: {0}", oFolder.Name);

 

    MAPI.Messages oMsgs = (MAPI.Messages)oFolder.Messages;

    Console.WriteLine("Messages Count : {0}", oMsgs.Count);

 

    MAPI.Message oMsg = (MAPI.Message)oMsgs.GetFirst(vEmpty);

  

    // Get some common properties

    Console.WriteLine("Subject : {0}", oMsg.Subject);

    Console.WriteLine("Text : {0}", oMsg.Text);

 

    // Get the Sender

    MAPI.AddressEntry oEntry = (MAPI.AddressEntry)oMsg.Sender;

    Console.WriteLine("Sender Name: {0}", oEntry.Name);

    Console.WriteLine("Sender Address : {0}", oEntry.Address); 

  

    // Get Sender SMTP address

    MAPI.Fields oFlds = (MAPI.Fields)oEntry.Fields;

    MAPI.Field oFld = (MAPI.Field)oFlds.get_Item((int)g_PR_SMTP_ADDRESS_W, vEmpty);

    Console.WriteLine("SMTP: {0}", oFld.Value);  

  

    // Logoff

    oSession.Logoff();

 

    // Clean Up

    oEntry = null;

    oMsgs = null;

    oMsg = null;

    oFolder = null;

    oSession = null;    

   }

   catch (Exception e)

   {

    Console.WriteLine("{0} Exception caught.", e);

   }

  }

 }

 

}

 

4.  Modify code where you see TODO. 

5.  Press F5 to build and run the program. 

6.  Verify that you got the sender's SMTP e-mail address. 

 

Comments (0)

Skip to main content