QuickStart: The basics

A simple example

Let's dive in with an example of how to send a simple e-mail with vbMAPI:

 Code language:   VBA / VB6    VB.NET    C#            (changing code language requires javascript to be enabled)
Dim Session As vbMAPI_Session
Dim Store As vbMAPI_Store
Dim Folder As vbMAPI_Folder
Dim FolderItems As vbMAPI_FolderItems
Dim Item As vbMAPI_MailItem

Set Session = vbMAPI_Init.NewSession
Session.LogOn , , True

Set Store = Session.Stores.DefaultStore

Set Folder = Store.GetDefaultFolder(FolderType_Outbox)

Set FolderItems = Folder.Items

Set Item = FolderItems.Add

Item.To_ = "some_email_address@mail.com"
Item.Subject = "Some Subject"
Item.HTMLBody = "<b>HTML message here...</b>"

Item.Send

Session.OutlookSendReceiveAll   ' Required if not dealing with an Exchange account
Dim Session As vbMAPI_Session
Dim Store As vbMAPI_Store
Dim Folder As vbMAPI_Folder
Dim FolderItems As vbMAPI_FolderItems
Dim Item As vbMAPI_MailItem

Session = vbMAPI_Init.NewSession
Session.LogOn(, , True)

Store = Session.Stores.DefaultStore

Folder = Store.GetDefaultFolder(EnumDefaultFolderType.FolderType_Outbox)

FolderItems = Folder.Items

Item = FolderItems.Add

Item.To_ = "some_email_address@mail.com"
Item.Subject = "Some Subject"
Item.HTMLBody.Value = "<b>HTML message here...</b>"

Item.Send

Session.OutlookSendReceiveAll()   ' Required if not dealing with an Exchange account
vbMAPI_Session      Session;
vbMAPI_Store        Store;
vbMAPI_Folder       Folder;
vbMAPI_FolderItems  FolderItems;
vbMAPI_MailItem     Item;

Session = vbMAPI_Init.NewSession();
Session.LogOn("", "", true, true, 0, false);

Store = Session.Stores.DefaultStore;

Folder = Store.GetDefaultFolder(EnumDefaultFolderType.FolderType_Outbox);

FolderItems = Folder.Items;

Item = FolderItems.Add(null);

Item.To_ = "some_email_address@mail.com";
Item.Subject = "Some Subject";
Item.HTMLBody.Value = "<b>HTML message here...</b>";

Item.Send();

Session.OutlookSendReceiveAll();

Description:

The root class of vbMAPI is called vbMAPI_Init - this is the starting point. We call the NewSession method which returns an object of type vbMAPI_Session.

We then need to log-on to a MAPI profile - we do this by using the Session.LogOn method. There are several optional parameters for the LogOn method (such as ProfileName, Password), but if you just want to log on to the default profile, then you can leave the parameters blank as shown here.

Next, we need to determine which MAPI store we are interested in. Stores identify the physical storage places for your e-mails. For example, if you have one Outlook PST file for your active e-mails and a separate one for your archived e-mails, then each of these is a "store", in MAPI terminology.

Usually you will only be interested in the default store (the one where new messages get delivered to). We get a pointer to the default store by calling the Session.Stores.DefaultStore method which returns an object of type vbMAPI_Store.

The vbMAPI_Store object provides access to all of the folders within a MAPI store. We use the vbMAPI_Store GetDefaultFolder method to obtain a reference to a standard folder within the MAPI store. Standard folders like "Inbox", "Drafts" and "Sent Items" (and more) are accessed in this manner. Intellisense will provide a full list for you.

The vbMAPI_Folder object provides access to messages and sub-folders within a MAPI folder. We then use the vbMAPI_Folder FolderItems method to obtain a reference to a collection of messages (type vbMAPI_FolderItems). Similarly we use the vbMAPI_Folder Folders method to obtain a reference to a collection of sub-folders type vbMAPI_Folders.  In this particular example, we use the FolderItems method as we want to create a new mail item.

For this example, we then call the Add method of the vbMAPI_FolderItems object to create a new mail item (type vbMAPI_MailItem). With the mail item we then set some properties of the mail item, and eventually call the Send method which submits the message to the recipient.

You will note that the To method has a trailing underscore. This is due to VB/VBA not allowing "To" to be the name of a procedure (it sees it as a reserved word), so we had to improvise a little.

Naturally, the vbMAPI_MailItem object has all that you would expect to find from such an object. For example; a Recipients collection, an Attachments collection, and many properties similar to the Outlook.MailItem.

Simplifying the example further

 Code language:   VBA / VB6    VB.NET    C#            (changing code language requires javascript to be enabled)
Dim Session As vbMAPI_Session
Dim Item As vbMAPI_MailItem

Set Session = vbMAPI_Init.NewSession
Session.LogOn , , True

Set Item = Session.GetDefaultFolder(FolderType_Outbox).Items.Add

Item.To_ = "some_email_address@mail.com"
Item.Subject = "Some Subject"
Item.HTMLBody = "<b>HTML message here...</b>"

Item.Send
Dim Session As vbMAPI_Session
Dim Item As vbMAPI_MailItem

Session = vbMAPI_Init.NewSession
Session.LogOn(, , True)

Item = Session.GetDefaultFolder(EnumDefaultFolderType.FolderType_Outbox).Items.Add

Item.To_ = "some_email_address@mail.com"
Item.Subject = "Some Subject"
Item.HTMLBody.Value = "<b>HTML message here...</b>"

Item.Send
vbMAPI_Session Session;
vbMAPI_MailItem Item;

Session = vbMAPI_Init.NewSession();
Session.LogOn("", "", true, true, 0, false);

Item = Session.GetDefaultFolder(EnumDefaultFolderType.FolderType_Outbox).Items.Add(null);

Item.To_ = "some_email_address@mail.com";
Item.Subject = "Some Subject";
Item.HTMLBody.Value = "<b>HTML message here...</b>";

Item.Send();

Things to note:

  1. Session.GetDefaultFolder is a simplified method to obtain a default folder from within the default MAPI store.
  2. As shown here, you don't need to hold the reference to each object open if you are only calling a single method on the object (so we have eliminated the object references to vbMAPI_Folder and vbMAPI_FolderItems in this example).

Where to get further examples

To get further vbMAPI examples, please refer to the Code Examples document