Click here to Skip to main content
11,432,168 members (60,241 online)
Click here to Skip to main content

Simple MAPI.NET

, 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

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:

  [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( "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

Share

About the Author

NETMaster
Web Developer
Switzerland Switzerland
No Biography provided

Comments and Discussions

 
QuestionFixed arithmetic overflow with pointer Pin
AJC Software23-Jan-15 1:07
memberAJC Software23-Jan-15 1:07 
GeneralWriting a PST file with non email items without outlook installed Pin
uddipto8-Aug-12 1:01
memberuddipto8-Aug-12 1:01 
GeneralMy vote of 5 Pin
manoj kumar choubey26-Feb-12 19:23
membermanoj kumar choubey26-Feb-12 19:23 
QuestionSend Items Pin
loende12-Oct-11 9:26
memberloende12-Oct-11 9:26 
Question.NET and Microsoft's MAPI DLL are not compatible. Pin
BMaguire27-Sep-11 16:08
memberBMaguire27-Sep-11 16:08 
They might seem to work together, but they are not compatible. You'll get memory leaks and crashes. If you want to use MAPI I suggest using a 3rd party library.


http://blogs.msdn.com/b/mstehle/archive/2007/10/03/fyi-why-are-mapi-and-cdo-1-21-not-supported-in-managed-net-code.aspx[^]
SuggestionMAPILogon failed with 80004005, BUT: Pin
Pascal Damman19-Jul-11 3:48
memberPascal Damman19-Jul-11 3:48 
QuestionConsole Application vs. GUI Application Pin
Bart Huls21-Apr-11 5:37
memberBart Huls21-Apr-11 5:37 
GeneralMy vote of 5 Pin
waritsar23-Aug-10 1:25
memberwaritsar23-Aug-10 1:25 
GeneralProblems with Login on Vista with Office 2007 Pin
DanielEarwicker21-Jul-09 6:44
memberDanielEarwicker21-Jul-09 6:44 
GeneralThanks Pin
NaveenPrabhu22-Feb-09 20:23
memberNaveenPrabhu22-Feb-09 20:23 
QuestionIs it possible to decrease the time for enabling the allow button on the security warning programmatically Pin
AmokMe10-Sep-08 3:35
memberAmokMe10-Sep-08 3:35 
AnswerRe: Is it possible to decrease the time for enabling the allow button on the security warning programmatically Pin
british27-Nov-08 10:28
memberbritish27-Nov-08 10:28 
GeneralAwesome sample Pin
jchalfant21-May-08 7:09
memberjchalfant21-May-08 7:09 
Questionhow to read mail from directly exchange server Pin
usha gupta9-Mar-08 21:39
memberusha gupta9-Mar-08 21:39 
QuestionIs it possible? [modified] Pin
Member 41003802-Jan-08 4:17
memberMember 41003802-Jan-08 4:17 
Questionread mail which is in folder that is out from indox Pin
agjsps29-Oct-07 18:21
memberagjsps29-Oct-07 18:21 
QuestionHow to send HTML page as body Pin
abhattad16-Oct-07 21:12
memberabhattad16-Oct-07 21:12 
GeneralProblems setting the messageTypeProperty Pin
planzaplanza16-Oct-07 13:12
memberplanzaplanza16-Oct-07 13:12 
GeneralRe: Problems setting the messageTypeProperty Pin
planzaplanza17-Oct-07 11:43
memberplanzaplanza17-Oct-07 11:43 
GeneralRe: Problems setting the messageTypeProperty Pin
Vraith841-Feb-09 1:11
memberVraith841-Feb-09 1:11 
QuestionGet only the last received mail from thunderbird Pin
tomthekid22-May-07 1:18
membertomthekid22-May-07 1:18 
Questionwhat about thunderbird Pin
aleccc16-May-07 21:35
memberaleccc16-May-07 21:35 
GeneralLotus Notes 6.5 client Pin
AndreyR77722-Feb-07 5:10
memberAndreyR77722-Feb-07 5:10 
AnswerRe: Lotus Notes 6.5 client Pin
Aaron Browne18-Oct-07 20:46
memberAaron Browne18-Oct-07 20:46 
GeneralPut message to inbox with MAPI Pin
Arsenmkrt20-Feb-07 3:28
memberArsenmkrt20-Feb-07 3:28 
QuestionOutlook Express MapiDialog causes error Pin
daues2-Feb-07 5:39
memberdaues2-Feb-07 5:39 
QuestionError: MAPI login failure [3] Pin
daniel9916-Nov-06 10:26
memberdaniel9916-Nov-06 10:26 
QuestionOutlook 2007 attachments? Pin
Richard__8-Nov-06 2:39
memberRichard__8-Nov-06 2:39 
QuestionWhy hourglass from secondary form on send? Pin
c-a-b-31-Aug-06 3:06
memberc-a-b-31-Aug-06 3:06 
AnswerRe: Why hourglass from secondary form on send? Pin
vzxcasdfgasd14-Nov-06 14:15
membervzxcasdfgasd14-Nov-06 14:15 
GeneralGreat Article Pin
ben_5-Jun-06 10:27
memberben_5-Jun-06 10:27 
GeneralGreat article, very useful Pin
Ranjan Banerji29-Apr-06 8:47
memberRanjan Banerji29-Apr-06 8:47 
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 
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
sussJon Kruger4-Mar-05 8:33 
GeneralRe: bringing up a new mail message instead of sending automatically Pin
Jon Kruger4-Mar-05 8:34
sussJon 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 

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
Web02 | 2.8.150428.2 | Last Updated 31 Mar 2002
Article Copyright 2002 by NETMaster
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid