Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / C#
Article

Simple MAPI.NET

Rate me:
Please Sign up or sign in to vote.
4.76/5 (57 votes)
30 Mar 2002Public Domain 436.3K   6K   80   87
Usage of the simple MAPI API.

Sample Image - simplemapidotnet.jpg

Abstract

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.

Details

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:

C#
[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:

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

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

C#
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:

C#
Mapi ma = new Mapi();
ma.Logon( IntPtr.Zero );
ma.AddRecip( "anybody@anywhere.org", null, false );
ma.Send( "Subject", "Mail text here" );
ma.Logoff();

Sample App

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

Limitations

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!

License

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


Written By
Web Developer
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionOutlook hangs (freezes) after popup Pin
jonney309927-Oct-20 23:56
jonney309927-Oct-20 23:56 
QuestionOutlook app freezes until press ALT+Tab Pin
jonney309927-Oct-20 23:56
jonney309927-Oct-20 23:56 
Questioncan simple MAPI run without STAThread? Pin
Member 1479008120-Apr-20 0:07
Member 1479008120-Apr-20 0:07 
QuestionCan i send email automatically Pin
Member 147900817-Apr-20 1:06
Member 147900817-Apr-20 1:06 
QuestionCan Use MAPI in ASP MVC? Pin
j.ghadiri19-Dec-18 3:32
j.ghadiri19-Dec-18 3:32 
BugCannot save bitmap after e-mail Pin
Siddiqui's6-May-15 4:08
Siddiqui's6-May-15 4:08 
QuestionFixed arithmetic overflow with pointer Pin
AJC Software23-Jan-15 0:07
AJC Software23-Jan-15 0:07 
GeneralWriting a PST file with non email items without outlook installed Pin
uddipto8-Aug-12 0:01
uddipto8-Aug-12 0:01 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 18:23
professionalManoj Kumar Choubey26-Feb-12 18:23 
QuestionSend Items Pin
loende12-Oct-11 8:26
loende12-Oct-11 8:26 
Question.NET and Microsoft's MAPI DLL are not compatible. Pin
BMaguire27-Sep-11 15:08
BMaguire27-Sep-11 15:08 
SuggestionMAPILogon failed with 80004005, BUT: Pin
Pascal Damman19-Jul-11 2:48
Pascal Damman19-Jul-11 2:48 
QuestionConsole Application vs. GUI Application Pin
Bart Huls21-Apr-11 4:37
Bart Huls21-Apr-11 4:37 
GeneralMy vote of 5 Pin
waritsar23-Aug-10 0:25
waritsar23-Aug-10 0:25 
GeneralProblems with Login on Vista with Office 2007 Pin
DanielEarwicker21-Jul-09 5:44
DanielEarwicker21-Jul-09 5:44 
GeneralThanks Pin
NaveenPrabhu22-Feb-09 19:23
NaveenPrabhu22-Feb-09 19:23 
QuestionIs it possible to decrease the time for enabling the allow button on the security warning programmatically Pin
AmokMe10-Sep-08 2:35
AmokMe10-Sep-08 2:35 
AnswerRe: Is it possible to decrease the time for enabling the allow button on the security warning programmatically Pin
british27-Nov-08 9:28
british27-Nov-08 9:28 
GeneralAwesome sample Pin
jchalfant21-May-08 6:09
jchalfant21-May-08 6:09 
Questionhow to read mail from directly exchange server Pin
usha gupta9-Mar-08 20:39
usha gupta9-Mar-08 20:39 
QuestionIs it possible? [modified] Pin
Member 41003802-Jan-08 3:17
Member 41003802-Jan-08 3:17 
Questionread mail which is in folder that is out from indox Pin
agjsps29-Oct-07 17:21
agjsps29-Oct-07 17:21 
QuestionHow to send HTML page as body Pin
amar bhattad16-Oct-07 20:12
amar bhattad16-Oct-07 20:12 
GeneralProblems setting the messageTypeProperty Pin
planzaplanza16-Oct-07 12:12
planzaplanza16-Oct-07 12:12 
GeneralRe: Problems setting the messageTypeProperty Pin
planzaplanza17-Oct-07 10:43
planzaplanza17-Oct-07 10:43 

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.