EvoCorp Home    Delphi::Advance

 

 

   

 

Easier Extended MAPI

Anyone who's ever implemented anything in extended MAPI knows that getting your project off the ground involves a lot of research and even more patience - extended MAPI is no easy beast!

By now you've probably got your hands on the original extended MAPI headers for Delphi, developed by Alexander Staubo and later updated by Dmitry Streblechenko (can't recommend Dmitry enough when it comes to help on extended MAPI). These units contain a raw translation of the C++ headers and nothing more, providing the basic building blocks required by your application but lacking anything that automates some of the more commonly used MAPI functionality.

I recently created a service that required access to a specific Outlook mailbox; if you've ever developed services that require Outlook access you'll know that Outlook is not designed to work from within a service and your only option for accessing Outlook becomes extended MAPI or CDO. After some initial investigation I realised this code didn't contain simple automation routines like connecting to, or disconnecting from an Outlook account, or to basic folders within an account such as the Inbox and Outbox. So I created the MapiServices unit.

Whilst this unit in no way pretends to offer all extended MAPI functionality in an easy to use manner, it does allow you to connect to someone's Inbox or Outbox (or with minimal development, any other folder) in one simple step. Access to mail items within those folders is straightforward and access to attachments connected with mail items is done via native Delphi streams. The following example demonstrates how easy it is to connect to the Inbox of someone's account and to enumerate available mail items:

uses
  MapiServices;
 
procedure DisplayMyMailItems;
var InBox: TInbox;
    Connection: TMapiConnection;
    MailItem: TMapiMailItem;
begin
  // Insert your actual account name in place
  // of MyAccountName, for instance "Bloggs, Joe"
  // or, if running from within Outlook, leave
  // ProfileName (i.e., MyAccountName) blank.
  // If we're running from within Outlook, we'll
  // also make sure we're using a shared session.

  Connection := TMapiConnection.Create(MyAccountName, True);
  try
    InBox := TInBox.Create(Connection);
    try
      MailItem := InBox.GetFirst;
      while Assigned(MailItem) do
      begin
        ShowMessage('From: ' + MailItem.Sender + #13#10 +
                    'Subject: ' + MailItem.Subject);
        // Do what you need to do here
        // ...
        // and then release MailItem.

        MailItem.Free;
        MailItem := InBox.GetNext;
      end;
    finally
      InBox.Free;
    end;
  finally
    Connection.Free;
  end;
end;

procedure SendMessage(Recipients: TStringList;
  const Subject, Body: string);
var TempStream: TStringStream;
    OutBox: TOutbox;
    MailItem: TMapiMailItem;
    Connection: TMapiConnection;
begin
  // Again, if we're running from Outlook, leave ProfileName
  // blank and identify we want a shared session.

  Connection := TMapiConnection.Create('', True);
  try
    OutBox := TOutBox.Create(Connection);
    try
      MailItem := OutBox.NewMailItem;
      MailItem.SetRecipients(mrrTo, Recipients, True);
      MailItem.Subject := Subject;
      MailItem.Body := Body;
      TempStream := TStringStream.Create('This is a text attachment');
      try
        MailItem.Attachments.Add('test.txt', TempStream);
      finally
        TempStream.Free;
      end;
      MailItem.Submit(False);
    finally
      OutBox.Free;
    end;
  finally
    Connection.Free;
  end;
end;

Note:
It is important to note that all objects returned by GetFirst, GetNext and NewMailItem must be released by the caller (i.e., your code) before the associated mailbox is released. These methods instantiate their resulting objects on the callers behalf, as a result it is the responsibility of the caller to release these objects.

Requirements

Download Code

MapiServices.pas can be downloaded here.

Microsoft Outlook is a registered trademark of Microsoft Corporation.