Click here to Skip to main content
Click here to Skip to main content


, 30 Mar 2002 Public Domain
Rate this:
Please Sign up or sign in to vote.
Usage of the simple MAPI API.

Sample Image - simplemapidotnet.jpg


As you know, .NET has SMTP email support built-in. For some special environments, the usage of e.g. the "Simple MAPI' API is prefered.


This API can be called like most other Win32 APIs with 'PInvoke'. By reading the MSDN documentation: Platform SDK - Simple MAPI and some hints from the C++ include header file MAPI.h, we can declare the calls in C# like:

  [DllImport( "MAPI32.DLL", CharSet=CharSet.Ansi)]
  private static extern int MAPIDeleteMail( IntPtr session, IntPtr winhandle,
                                        string id, int flags, int reserved );

For sending email with 'MAPISendMail', the structure 'MapiMessage' has to be filled with pointers to sub-structures like 'MapiRecipDesc'. For this, I used the System.Runtime.InteropServices.Marshal class:

  Marshal.SizeOf(), Marshal.AllocHGlobal(),
  Marshal.StructureToPtr(), Marshal.PtrToStructure(),
  Marshal.DestroyStructure(), Marshal.FreeHGlobal()

Most SimpleMAPI functions are 'wrapped' in a handy class (MapiApi.cs):

public class Mapi
  // Session
  public bool Logon( IntPtr winhandle )
  public void Reset()
  public void Logoff()

  // Sending
  public void AddRecip( string name, string address, bool cc )
  public void Attach( string filepath )
  public bool Send( string subject, string text )

  // Finding
  public bool Next( ref MailEnvelop env )

  // Reading
  public string Read( string id, out MailAttach[] aat )
  public bool Delete( string id )
  public bool SaveAttachm( string id, string filename, string savepath )

  // Addressing
  public bool SingleAddress( string label, out string name, out string addr )

A small (e.g. console) client could do just this few steps:

  Mapi ma = new Mapi();
  ma.Logon( IntPtr.Zero );
  ma.AddRecip( "", null, false ); 
  ma.Send( "Subject", "Mail text here" );

Sample App

The source for a sample GUI (Windows Form) client and a console client are included in the download.


All code was tested only on Windows XP with MS Outlook XP in the internet-mode. On this system, some security warnings show-up if accessing MAPI. Any feedback for other environments (especially Exchange) are very welcome!


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Web Developer
Switzerland Switzerland
No Biography provided

Comments and Discussions

QuestionFixed arithmetic overflow with pointer PinmemberAJC Software23-Jan-15 1:07 
GeneralWriting a PST file with non email items without outlook installed Pinmemberuddipto8-Aug-12 1:01 
GeneralMy vote of 5 Pinmembermanoj kumar choubey26-Feb-12 19:23 
QuestionSend Items Pinmemberloende12-Oct-11 9:26 
Question.NET and Microsoft's MAPI DLL are not compatible. PinmemberBMaguire27-Sep-11 16:08 
SuggestionMAPILogon failed with 80004005, BUT: PinmemberPascal Damman19-Jul-11 3:48 
After closing the project for the 10th time, I figured out:
Windows 7: x64
Office: x86
This project compiled to x64, so couldn't use the x86 Messaging Subsystem. I changed the 'Platform target" from "Any CPU" to x86: and it works perfectly!!!!
So, if you encounter this unspecified error, consider the match between the project and Office/Outlook.
Chau chau,

QuestionConsole Application vs. GUI Application PinmemberBart Huls21-Apr-11 5:37 
GeneralMy vote of 5 Pinmemberwaritsar23-Aug-10 1:25 
GeneralProblems with Login on Vista with Office 2007 PinmemberDanielEarwicker21-Jul-09 6:44 
GeneralThanks PinmemberNaveenPrabhu22-Feb-09 20:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150326.1 | Last Updated 31 Mar 2002
Article Copyright 2002 by NETMaster
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid