MAPI Class ‘& Simple MAPI Project

Here is a simple CMapi Class
to help developers configure an Exchange Profile. By using this class

you can:

  • Add/Delete Microsoft Exchange Profiles
  • Configure Microsof t Exchange Profiles
  • Add/Delete Microsoft Exchange Messaging Services
    • Exchange
    • MS Mail
    • Lotus
    • Personal Folders
    • Personal Address Books
    • Outlook Address Books
  • Configure Message Services (Server name, User name)
  • Set Default Messaging Stores (i.e. Setting the Exchange Mailbox as the Default
    Messaging Store)

The project that is included is basically a simple dialog box that will show
the progress of the wizard. To use run the project you will need to have an
Exchange box on the network. Supply the Exchange server name in the code below
and make sure you have the user name correct. When you run this app it will
attempt to connect to the server when it tries to configure the messaging service
parts and set the default messaging stores. If the username is not correct,
it will stop. So verify the format that your Exchange server users for names
(i.e. Last Name, First Name OR First Name Last Name). This application has been
tested with Microsoft Windows NT 4.0 and Microsoft Visual C++ 6.0. Please don’t
forget to include MAPI32.LIB in the linker tab! Please feel free to modify this
code. This code is a good starting point for anyone who is going to be doing
any MAPI code – and there is some extra code that you can add (you find it!)
for some cool MAPI coding.

All the actual code is in the OnStart() Function – Please note that this code
does NOT create a NEW Microsoft Exchange Profile, it uses the default profile
(MAPI_USE_DEFAULT) – but this can easily be changed…

  void CProfileDemoDlg::OnStart()
  {
      CMapi Mapi;

      // Look in MAPISVC.INF for Service names (i.e. MSEMS)
      LPSTR lpszAddService = "MSEMS", // Microsoft Exchange Service to Add (in this case Exchange)
          lpszDelService = "MSFS", // Microsoft Exchange Service to Delete (in this case MSMAIL)
          lpszServiceName = "Microsoft Exchange Server", // Exchange Service Official Name
          lpszMailbox = "Christopher Backen", // Person to Configure
          lpszMailStore = "Mailbox - Christopher Backen", // Message Store to Configure
          lpszServerName = "CORDALEX1"; // Server to Configure

      // Set Progress Bar 
      m_pBar.SetRange(0, 10);
      m_pBar.SetStep(1);

      // Start the session
      m_Info.SetWindowText("Initializing MAPI Dynamic Link Library");
      hr = Mapi.Initialize();
      if (HR_FAILED(hr)){ AfxMessageBox("Failed..."); return;}

      hr = Mapi.AdminProfiles();
      if (HR_FAILED(hr)){ AfxMessageBox("Failed..."); return;}
      m_pBar.SetPos(2);

      m_Info.SetWindowText("Logging on to the Default Profile");
      hr = Mapi.LogonDefaultEx();
      if (HR_FAILED(hr)){ AfxMessageBox("Failed..."); return;}

      hr = Mapi.OpenAdminServices();
      if (HR_FAILED(hr)){ AfxMessageBox("Failed..."); return;}
      m_pBar.SetPos(4);

      m_Info.SetWindowText("Creating Microsoft Exchange Messaging Service");
      hr = Mapi.CreateMessagingService(lpszAddService, lpszServiceName);
      if (HR_FAILED(hr)){ AfxMessageBox("Failed..."); return;}
      m_pBar.SetPos(5);

      m_Info.SetWindowText("Configuring Microsoft Exchange Messaging Service");
      hr = Mapi.ConfigureMessagingService(lpszAddService, lpszMailbox, lpszServerName);
      if (HR_FAILED(hr)){ AfxMessageBox("Failed..."); return;}
      m_pBar.SetPos(6);

      m_Info.SetWindowText("Deleting Microsoft Mail Messaging Service");
      hr = Mapi.DeleteMessagingService(lpszDelService);
      if (HR_FAILED(hr)){ AfxMessageBox("Failed..."); return;}

      hr = Mapi.GetMessageStoresTable();
      if (HR_FAILED(hr)){ AfxMessageBox("Failed..."); return;}
      m_pBar.SetPos(8);

      m_Info.SetWindowText("Setting the default mailbox to the server");
      // More complex

      hr = Mapi.SetDefaultMessagingStore(lpszMailStore);
      if (HR_FAILED(hr)){ AfxMessageBox("Failed..."); return;}
      m_pBar.SetPos(9);

      Mapi.CloseMapi();

      m_pBar.SetPos(10);

      // DONE
      return;
  }

To find any service names – consult the MAPISVC.INF file. For Example:

[MAPI]
 ServiceOrder=AWFAX,MSEMS,MSFS,IMAIL,MSN
 [Services]
 MSEMS=Microsoft Exchange Server <--- Here is the name of the Exchange Service
 IMAIL=Internet E-mail
 MSPST AB=Personal Address Book
 MSPST MS=Personal Folders
 MSFS=Microsoft Mail
 CONTAB=Outlook Address Book
 MSN=The Microsoft Network Online Service
 [Default Services]
 MSEMS=Microsoft Exchange Server
 MSPST AB=Personal Address Book
 [MSEMS]

 ....Blah Blah Blah

This code really helped my group. I hope it helps you out just as much. If
you have any comments or suggestions please send them to the above address.
Or visit VIZ Group AppDev Division.

ProfileDemo.zip - 33 KB

CMapi Class (Mapi.h) - 3 KB

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read