Click here to Skip to main content
Licence Public Domain
First Posted 30 Mar 2002
Views 242,083
Downloads 1,175
Bookmarked 71 times

Simple MAPI.NET

By NETMaster | 30 Mar 2002
Usage of the simple MAPI API.
1 vote, 1.9%
1

2
2 votes, 3.8%
3
8 votes, 15.1%
4
42 votes, 79.2%
5
4.83/5 - 58 votes
3 removed
μ 4.42, σa 1.28 [?]

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

About the Author

NETMaster

Web Developer

Switzerland Switzerland

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionSend Items Pinmemberloende9:26 12 Oct '11  
Question.NET and Microsoft's MAPI DLL are not compatible. PinmemberBMaguire16:08 27 Sep '11  
SuggestionMAPILogon failed with 80004005, BUT: PinmemberPascal Damman3:48 19 Jul '11  
QuestionConsole Application vs. GUI Application PinmemberBart Huls5:37 21 Apr '11  
GeneralMy vote of 5 Pinmemberwaritsar1:25 23 Aug '10  
GeneralProblems with Login on Vista with Office 2007 PinmemberDanielEarwicker6:44 21 Jul '09  
GeneralThanks PinmemberNaveenPrabhu20:23 22 Feb '09  
QuestionIs it possible to decrease the time for enabling the allow button on the security warning programmatically PinmemberAmokMe3:35 10 Sep '08  
AnswerRe: Is it possible to decrease the time for enabling the allow button on the security warning programmatically Pinmemberbritish10:28 27 Nov '08  
GeneralAwesome sample Pinmemberjchalfant7:09 21 May '08  
Questionhow to read mail from directly exchange server Pinmemberusha gupta21:39 9 Mar '08  
QuestionIs it possible? [modified] PinmemberMember 41003804:17 2 Jan '08  
Questionread mail which is in folder that is out from indox Pinmemberagjsps18:21 29 Oct '07  
QuestionHow to send HTML page as body Pinmemberabhattad21:12 16 Oct '07  
GeneralProblems setting the messageTypeProperty Pinmemberplanzaplanza13:12 16 Oct '07  
GeneralRe: Problems setting the messageTypeProperty Pinmemberplanzaplanza11:43 17 Oct '07  
GeneralRe: Problems setting the messageTypeProperty PinmemberVraith841:11 1 Feb '09  
QuestionGet only the last received mail from thunderbird Pinmembertomthekid1:18 22 May '07  
Questionwhat about thunderbird Pinmemberaleccc21:35 16 May '07  
GeneralLotus Notes 6.5 client PinmemberAndreyR7775:10 22 Feb '07  
AnswerRe: Lotus Notes 6.5 client PinmemberAaron Browne20:46 18 Oct '07  
GeneralPut message to inbox with MAPI PinmemberArsenmkrt3:28 20 Feb '07  
QuestionOutlook Express MapiDialog causes error Pinmemberdaues5:39 2 Feb '07  
QuestionError: MAPI login failure [3] Pinmemberdaniel9910:26 16 Nov '06  
QuestionOutlook 2007 attachments? PinmemberRichard__2:39 8 Nov '06  

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.

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