Click here to Skip to main content
Licence Public Domain
First Posted 30 Mar 2002
Views 248,996
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
GeneralMy vote of 5 Pinmembermanoj kumar choubey18:23 26 Feb '12  
QuestionSend Items Pinmemberloende8:26 12 Oct '11  
Question.NET and Microsoft's MAPI DLL are not compatible. PinmemberBMaguire15:08 27 Sep '11  
SuggestionMAPILogon failed with 80004005, BUT: PinmemberPascal Damman2:48 19 Jul '11  
QuestionConsole Application vs. GUI Application PinmemberBart Huls4:37 21 Apr '11  
GeneralMy vote of 5 Pinmemberwaritsar0:25 23 Aug '10  
GeneralProblems with Login on Vista with Office 2007 PinmemberDanielEarwicker5:44 21 Jul '09  
GeneralThanks PinmemberNaveenPrabhu19:23 22 Feb '09  
QuestionIs it possible to decrease the time for enabling the allow button on the security warning programmatically PinmemberAmokMe2:35 10 Sep '08  
AnswerRe: Is it possible to decrease the time for enabling the allow button on the security warning programmatically Pinmemberbritish9:28 27 Nov '08  
GeneralAwesome sample Pinmemberjchalfant6:09 21 May '08  
Questionhow to read mail from directly exchange server Pinmemberusha gupta20:39 9 Mar '08  
QuestionIs it possible? [modified] PinmemberMember 41003803:17 2 Jan '08  
Questionread mail which is in folder that is out from indox Pinmemberagjsps17:21 29 Oct '07  
QuestionHow to send HTML page as body Pinmemberabhattad20:12 16 Oct '07  
GeneralProblems setting the messageTypeProperty Pinmemberplanzaplanza12:12 16 Oct '07  
GeneralRe: Problems setting the messageTypeProperty Pinmemberplanzaplanza10:43 17 Oct '07  
I found out how to fix this. I had to append the messageType to the recipient address string like so:
 
recipientAddress = "SMTP:foo@bar.com"
 
This fixes the problem. I found this out by digging around on msdn and looking at the MAPI API documentation. The link to the recipients section where I found this is below.
 
http://msdn2.microsoft.com/en-us/library/ms529097.aspx
 
Hopefully this will help someone somewhere some day. hahaha
GeneralRe: Problems setting the messageTypeProperty PinmemberVraith840:11 1 Feb '09  
QuestionGet only the last received mail from thunderbird Pinmembertomthekid0:18 22 May '07  
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  

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
Web04 | 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