Click here to Skip to main content
15,890,123 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 438.8K   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

 
QuestionWhat condition for MAPI? Pin
jewlin3-Feb-04 16:31
jewlin3-Feb-04 16:31 
GeneralUse in commercial application. Pin
abrian14-Jan-04 23:28
abrian14-Jan-04 23:28 
QuestionPassword ignored? Pin
banduraj8-Jan-04 6:45
banduraj8-Jan-04 6:45 
AnswerRe: Password ignored? Pin
Jan Pichler22-May-04 12:22
sussJan Pichler22-May-04 12:22 
GeneralAdd repipientS Pin
Harald Geißelhart4-Jan-04 7:32
Harald Geißelhart4-Jan-04 7:32 
GeneralRe: Add repipientS Pin
NETMaster4-Jan-04 14:02
NETMaster4-Jan-04 14:02 
QuestionNice Article - Bulk Mail ? Pin
Braulio Dez23-Sep-03 0:59
Braulio Dez23-Sep-03 0:59 
GeneralAOL v8 Problem Pin
mike1orb18-Sep-03 16:20
mike1orb18-Sep-03 16:20 
Does anyone know if AOL version 8 only supports "extended MAPI"? I tested with Simple MAPI.NET some code to bring up a compose form with a pre-filled in body and message:
<br />
Dim mapi As New Win32Mapi.Mapi<br />
mapi.Send("Test Subject", "Test body")<br />

Under AOL8 a compose window was raised but the subject/body was empty. This code worked with many other mail programs I tested (Eudora, Netscape, Outlook, etc.).

IE's "Send Page" function works when AOL8 is my default mail client. Is IE using "extended MAPI" perhaps? I can launch a compose window via the mailto: trick via Process.Start() but this places a limit of approximately 500 characters on the URL (at least for AOL -- other clients accept up to what seems to be IE or W3C's limit of ~2028 chars). For example:

Click to launch e-mail

I know under Outlook/OE this will not yield HTML but it does with AOL8. But the 500 char URL limit of AOL (based on testing) is a roadblock.

So to summarize I'm trying to determine what is causing the blank message form to popup: AOL8 lack of full support for Simple MAPI, this library, ???

Many thanks,
-Mike
GeneralExtended MAPI to modify emails being sent Pin
Gorgrael29-May-03 18:46
Gorgrael29-May-03 18:46 
GeneralRe: Extended MAPI to modify emails being sent Pin
NETMaster30-May-03 0:19
NETMaster30-May-03 0:19 
GeneralRead *.eml files Pin
AlexCode22-May-03 12:17
professionalAlexCode22-May-03 12:17 
GeneralRe: DllImport Pin
NETMaster23-Jan-03 14:25
NETMaster23-Jan-03 14:25 
GeneralRe: DllImport Pin
NETMaster26-Jan-03 12:21
NETMaster26-Jan-03 12:21 
GeneralMAPI Service Pin
fadi17-Dec-02 5:29
fadi17-Dec-02 5:29 
GeneralRe: MAPI Service Pin
NETMaster17-Dec-02 6:15
NETMaster17-Dec-02 6:15 
GeneralRe: MAPI Service Pin
ChainsawDon27-Feb-05 2:03
ChainsawDon27-Feb-05 2:03 
GeneralVery Cool Pin
Anonymous11-Nov-02 14:33
Anonymous11-Nov-02 14:33 
Questionhow about other folders? Pin
Anonymous19-Oct-02 10:02
Anonymous19-Oct-02 10:02 
AnswerRe: how about other folders? Pin
NETMaster20-Oct-02 22:50
NETMaster20-Oct-02 22:50 
GeneralExchange Pin
Anonymous26-Sep-02 14:03
Anonymous26-Sep-02 14:03 
GeneralGreat sample Pin
Anonymous2-Sep-02 1:24
Anonymous2-Sep-02 1:24 
GeneralBug with OE when saving attachment Pin
NETMaster31-Jul-02 6:45
NETMaster31-Jul-02 6:45 
GeneralSend e-mail from within service Pin
1-Jul-02 9:45
suss1-Jul-02 9:45 
GeneralExtended MAPI Pin
Mark Pitman29-Apr-02 11:53
Mark Pitman29-Apr-02 11:53 
GeneralRe: Extended MAPI Pin
NETMaster29-Apr-02 21:47
NETMaster29-Apr-02 21:47 

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.