Click here to Skip to main content
16,015,658 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 447.6K   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

 
GeneralPut message to inbox with MAPI Pin
ArsenMkrt20-Feb-07 2:28
ArsenMkrt20-Feb-07 2:28 
QuestionOutlook Express MapiDialog causes error Pin
daues2-Feb-07 4:39
daues2-Feb-07 4:39 
QuestionError: MAPI login failure [3] Pin
daniel9916-Nov-06 9:26
daniel9916-Nov-06 9:26 
QuestionOutlook 2007 attachments? Pin
Richard__8-Nov-06 1:39
Richard__8-Nov-06 1:39 
QuestionWhy hourglass from secondary form on send? Pin
c-a-b-31-Aug-06 2:06
c-a-b-31-Aug-06 2:06 
AnswerRe: Why hourglass from secondary form on send? Pin
vzxcasdfgasd14-Nov-06 13:15
vzxcasdfgasd14-Nov-06 13:15 
GeneralGreat Article Pin
ben_5-Jun-06 9:27
ben_5-Jun-06 9:27 
GeneralGreat article, very useful Pin
Ranjan Banerji29-Apr-06 7:47
Ranjan Banerji29-Apr-06 7:47 
Thanks for this article. Well written and provides a very useful class. I am using it in one of my projects, with your name intact on the class documentation Smile | :) . I did document the rest of your code though ..... lol
QuestionHow to compare this approach with using the MAPI namespace as in this article? Pin
tank2096-Apr-06 12:42
tank2096-Apr-06 12:42 
GeneralMS Outlook Hangs Pin
kperwaiz13-Feb-06 19:01
kperwaiz13-Feb-06 19:01 
GeneralRe: MS Outlook Hangs Pin
Jeff Lindholm15-Sep-06 3:28
Jeff Lindholm15-Sep-06 3:28 
QuestionHow do you avoid the Outlook security popup? Pin
youngDev26-Oct-05 10:45
youngDev26-Oct-05 10:45 
AnswerRe: How do you avoid the Outlook security popup? Pin
Antony M Kancidrowski15-Mar-06 6:10
Antony M Kancidrowski15-Mar-06 6:10 
AnswerRe: How do you avoid the Outlook security popup? Pin
sinanju29-Aug-06 1:31
sinanju29-Aug-06 1:31 
AnswerRe: How do you avoid the Outlook security popup? Pin
John Boero30-May-07 11:38
John Boero30-May-07 11:38 
GeneralWonderfull, Need Help Pin
Md Saleem Navalur26-Jul-05 21:09
Md Saleem Navalur26-Jul-05 21:09 
GeneralMAPISendDocuments Pin
Jim Rogers16-Jun-05 5:18
Jim Rogers16-Jun-05 5:18 
GeneralSending HTML messages Pin
Thomas at Wendia11-Apr-05 0:23
Thomas at Wendia11-Apr-05 0:23 
GeneralSend email without email client being open Pin
spaceboy27-Mar-05 9:55
spaceboy27-Mar-05 9:55 
Generalbringing up a new mail message instead of sending automatically Pin
Jon Kruger4-Mar-05 7:33
Jon Kruger4-Mar-05 7:33 
GeneralRe: bringing up a new mail message instead of sending automatically Pin
Jon Kruger4-Mar-05 7:34
Jon Kruger4-Mar-05 7:34 
GeneralRe: bringing up a new mail message instead of sending automatically Pin
Artem Kliatchkine6-Apr-05 22:19
Artem Kliatchkine6-Apr-05 22:19 
AnswerRe: bringing up a new mail message instead of sending automatically Pin
madmortigan8-Oct-07 22:36
madmortigan8-Oct-07 22:36 
GeneralError in SimpleMAPIdotNET Pin
TNL6-Jan-05 3:06
TNL6-Jan-05 3:06 
GeneralFailure with App.config !!??!! Pin
ptorrsmith21-Nov-04 15:32
ptorrsmith21-Nov-04 15: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.