Click here to Skip to main content
Licence Public Domain
First Posted 30 Mar 2002
Views 248,939
Bookmarked 72 times

Simple MAPI.NET

By | 30 Mar 2002 | Article
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

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
Questionwhat about thunderbird Pinmemberaleccc20:35 16 May '07  
GeneralLotus Notes 6.5 client PinmemberAndreyR7774:10 22 Feb '07  
AnswerRe: Lotus Notes 6.5 client PinmemberAaron Browne19:46 18 Oct '07  
GeneralPut message to inbox with MAPI PinmemberArsenmkrt2:28 20 Feb '07  
QuestionOutlook Express MapiDialog causes error Pinmemberdaues4:39 2 Feb '07  
QuestionError: MAPI login failure [3] Pinmemberdaniel999:26 16 Nov '06  
QuestionOutlook 2007 attachments? PinmemberRichard__1:39 8 Nov '06  
I can open a new message with an attachment in Outlook 2003 and Thunderbird, but not in Outlook 2007, where it opens a new mail message with no attachments. Any ideas how to get it to work in Outlook 2007? I know it's possible since Adobe Acrobat Reader can still attach files in emails with one click.
 
I am using this simple code from the article:
 
Win32Mapi.Mapi ma = new Win32Mapi.Mapi();
ma.Logon(IntPtr.Zero);
ma.Attach(@"c:\temp.txt");
ma.Send("Subjectheading", "Bodytext", false);
ma.Logoff();
QuestionWhy hourglass from secondary form on send? Pinmemberc-a-b-2:06 31 Aug '06  
AnswerRe: Why hourglass from secondary form on send? Pinmembervzxcasdfgasd13:15 14 Nov '06  
GeneralGreat Article Pinmemberben_9:27 5 Jun '06  
GeneralGreat article, very useful PinmemberRanjan Banerji7:47 29 Apr '06  
QuestionHow to compare this approach with using the MAPI namespace as in this article? Pinmembertank20912:42 6 Apr '06  
GeneralMS Outlook Hangs Pinmemberkperwaiz19:01 13 Feb '06  
GeneralRe: MS Outlook Hangs PinmemberJeff Lindholm3:28 15 Sep '06  
QuestionHow do you avoid the Outlook security popup? PinmemberyoungDev10:45 26 Oct '05  
AnswerRe: How do you avoid the Outlook security popup? PinmemberAntony M Kancidrowski6:10 15 Mar '06  
AnswerRe: How do you avoid the Outlook security popup? Pinmembersinanju1:31 29 Aug '06  
AnswerRe: How do you avoid the Outlook security popup? PinmemberJohn Boero11:38 30 May '07  
GeneralWonderfull, Need Help PinmemberMd Saleem Navalur21:09 26 Jul '05  
GeneralMAPISendDocuments PinmemberJim Rogers5:18 16 Jun '05  
GeneralSending HTML messages PinmemberThomas at Wendia0:23 11 Apr '05  
GeneralSend email without email client being open Pinmemberspaceboy9:55 27 Mar '05  
Generalbringing up a new mail message instead of sending automatically PinsussJon Kruger7:33 4 Mar '05  
GeneralRe: bringing up a new mail message instead of sending automatically PinsussJon Kruger7:34 4 Mar '05  
GeneralRe: bringing up a new mail message instead of sending automatically PinmemberArtem Kliatchkine22:19 6 Apr '05  

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