Tips for VB.NET

Overview

vbMAPI is fully supported on the VB.NET platform (Windows Forms only - not ASP.NET).  vbMAPI provides support for both 32-bit (x86) and 64-bit (x64) builds of your VB.NET 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 VB.NET is provided as a single source file: vbMAPI.vb

This file is installed automatically by our installer.  The file can be found in the following folder on your machine:

%appdata%\EverythingAccess.com\vbMAPI\VB.NET

PLEASE NOTE: Prior to v1.13.0 the installation folder was different.  Please update to the latest release of vbMAPI to access the folder listed above.

Note: The single vbMAPI.vb file provides all the functionality (both 32 bit and 64 bit versions) in one single source file.

Threading

vbMAPI objects are not inheritently 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 need to create a completely separate instance of vbMAPI in each thread using the following snippet:

Dim Session As vbMAPI_Session
Session = (New vbMAPI_Instance).NewSession()
But be careful not to share any vbMAPI objects across threads!

Adding vbMAPI to your VB.NET project

To add vbMAPI to your VB.NET 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\VB.NET folder and select the vbMAPI.vb 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 VB.NET 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 client is Outlook 2010 64-bit edition. 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 VB.NET

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

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

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

Differences to the VB6 / VBA version

The only differences are between the languages themselves.  For example, there are two 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 VB.NET 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 VB.NET