vbMAPI Outlook/MAPI Code Library
(v1.16.0)

Tips for C#

Overview

vbMAPI is fully supported on the C# platform.  vbMAPI provides support for both 32-bit (x86) and 64-bit (x64) builds of your C# application.

Support is provided through using COM Interop to access our Virtual-COM objects, thereby avoiding the need to distribute any DLLs to your end-users.

How to install

vbMAPI for C# is provided as a single source file - either vbMAPI.cs or vbMAPI_cs4.cs

Use vbMAPI.cs if you are using Visual Studio 2008 or earlier. Use vbMAPI_cs4.cs if you are using VS 2010 or newer (C#4.0).  The vbMAPI_cs4.cs version makes use of optional parameters in the interface definitions, which requires C#4.0.

These files are installed automatically by our installer.  The files can be found in the following folder on your machine:

%appdata%\EverythingAccess.com\vbMAPI\C#

PLEASE NOTE: Prior to v1.13.0 (released 17th April 2012) the installation folder was different.  Please update to the latest release of vbMAPI to access the folder listed above.

Each single source file provides all the functionality (both 32 bit and 64 bit versions) in one single source file.

Limitations

vbMAPI objects are not thread-safe and therefore you will run into problems if you try to access vbMAPI objects across multiple .NET threads.

If you really need multi-threaded support, then you must create a completely separate instance of vbMAPI in each thread using the following snippet:

vbMAPI_Session Sess = (new vbMAPI_Instance()).ClassFactory.NewSession();
But be careful not to share any vbMAPI objects across threads!

Adding vbMAPI to your C# project

To add vbMAPI to your C# application, open the project in Visual Studio and go to the "Project" > "Add Exisiting Item" menu option.  Now, navigate to your %appdata%\EverythingAccess.com\vbMAPI\C# folder and select the vbMAPI.cs source file. The file will now be imported into your project.

Tip: when updating the vbMAPI source module, follow the same procedure as above.

Important: Before you begin!

vbMAPI supports running in both 32 bit and 64 bit modes, which is automatically determined based on the CPU setting of your C# application.

  • If your CPU setting is AnyCPU, then running your application on an x86 system causes the application to run in 32 bit mode.
  • If your CPU setting is AnyCPU, then running your application on an x64 system causes the application to run in 64 bit mode.

Now, here is the important info:.

If vbMAPI is running in 64-bit mode, then your MAPI software also needs to run in 64-bit. Currently the only 64-bit MAPI clients are Outlook 2010 / 2013 / 2016 64-bit editions. If you want to target both 32 bit and 64 bit MAPI clients, then you should offer two separate builds of your application (not setting AnyCPU in your project build configuration).

More commonly, you will just want to support 32-bit MAPI clients (since there is only one 64 bit MAPI client to-date), therefore in most circumstances you need to set your project configuration to target the x86 platform ONLY.

Using vbMAPI in C#

Now that you have imported the vbMAPI source module into your project, you can now use vbMAPI in the same general way as the VBA/VB6 version.  To get you started, add a button to your form and add the following code:

    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();

For more examples, see the general documentation: QuickStart: The Basics

Differences to the VB6 / VBA version

The differences are between the languages themselves.  For example, there are three main differences that you need to be aware of if copying any of the examples from this manual:

  1. Enumerations need to be preceded by the enumeration name (see the example on this page compared to on the QuickStart: The Basics page.
  2. Not all default members are set in the C# version due to the language only allowing default members for collections. For example, in the above example you will note that we had to use Item.HTMLBody.Value - this is due to the Value property not being allowed to be a default member in C#
  3. If using the pre-C#4.0 version, all optional parameters need to be explicity declared when calling procedures - refer to the documentation to determine the default values.