Click here to Skip to main content
13,801,468 members
Click here to Skip to main content
Add your own
alternative version


79 bookmarked
Posted 30 Mar 2002
Licenced Public Domain


, 30 Mar 2002
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

You may also be interested in...

Comments and Discussions

QuestionHow to compare this approach with using the MAPI namespace as in this article? Pin
tank2096-Apr-06 13:42
membertank2096-Apr-06 13:42 
GeneralMS Outlook Hangs Pin
kperwaiz13-Feb-06 20:01
memberkperwaiz13-Feb-06 20:01 
GeneralRe: MS Outlook Hangs Pin
Jeff Lindholm15-Sep-06 4:28
memberJeff Lindholm15-Sep-06 4:28 
QuestionHow do you avoid the Outlook security popup? Pin
youngDev26-Oct-05 11:45
memberyoungDev26-Oct-05 11:45 
AnswerRe: How do you avoid the Outlook security popup? Pin
Antony M Kancidrowski15-Mar-06 7:10
memberAntony M Kancidrowski15-Mar-06 7:10 
AnswerRe: How do you avoid the Outlook security popup? Pin
sinanju29-Aug-06 2:31
membersinanju29-Aug-06 2:31 
AnswerRe: How do you avoid the Outlook security popup? Pin
John Boero30-May-07 12:38
memberJohn Boero30-May-07 12:38 
GeneralWonderfull, Need Help Pin
Md Saleem Navalur26-Jul-05 22:09
memberMd Saleem Navalur26-Jul-05 22:09 
Thanx for the Great article.

It seems that there are security issues with the app.
when I start the app, I get a messagae from Microsoft Outlook..
Saying that "A program is trying to access the email addresses you have stored in the outlook. Do you want to allow this ?"...

How do we stop this message from coming ???

And what are the security isssues if any ???

Any help will be great.

GeneralMAPISendDocuments Pin
Jim Rogers16-Jun-05 6:18
memberJim Rogers16-Jun-05 6:18 
GeneralSending HTML messages Pin
Thomas at Wendia11-Apr-05 1:23
memberThomas at Wendia11-Apr-05 1:23 
GeneralSend email without email client being open Pin
spaceboy27-Mar-05 10:55
memberspaceboy27-Mar-05 10:55 
Generalbringing up a new mail message instead of sending automatically Pin
Jon Kruger4-Mar-05 8:33
memberJon Kruger4-Mar-05 8:33 
GeneralRe: bringing up a new mail message instead of sending automatically Pin
Jon Kruger4-Mar-05 8:34
memberJon Kruger4-Mar-05 8:34 
GeneralRe: bringing up a new mail message instead of sending automatically Pin
Artem Kliatchkine6-Apr-05 23:19
memberArtem Kliatchkine6-Apr-05 23:19 
AnswerRe: bringing up a new mail message instead of sending automatically Pin
madmortigan8-Oct-07 23:36
membermadmortigan8-Oct-07 23:36 
GeneralError in SimpleMAPIdotNET Pin
TNL6-Jan-05 4:06
memberTNL6-Jan-05 4:06 
GeneralFailure with App.config !!??!! Pin
ptorrsmith21-Nov-04 16:32
memberptorrsmith21-Nov-04 16:32 
Generalwon't work with XP + Outlook 2002/2003 Pin
topikpl16-Sep-04 22:17
membertopikpl16-Sep-04 22:17 
GeneralRe: won't work with XP + Outlook 2002/2003 Pin
Craigus_H10-Jan-05 8:15
memberCraigus_H10-Jan-05 8:15 
QuestionHow to make a script read new mails automatically ? Pin
faizq1-Sep-04 20:38
memberfaizq1-Sep-04 20:38 
QuestionWhat condition for MAPI? Pin
jewlin3-Feb-04 17:31
memberjewlin3-Feb-04 17:31 
GeneralUse in commercial application. Pin
abrian15-Jan-04 0:28
memberabrian15-Jan-04 0:28 
QuestionPassword ignored? Pin
banduraj8-Jan-04 7:45
memberbanduraj8-Jan-04 7:45 
AnswerRe: Password ignored? Pin
Jan Pichler22-May-04 13:22
sussJan Pichler22-May-04 13:22 
GeneralAdd repipientS Pin
Harald Geißelhart4-Jan-04 8:32
memberHarald Geißelhart4-Jan-04 8:32 

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

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.181215.1 | Last Updated 31 Mar 2002
Article Copyright 2002 by NETMaster
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid