Click here to Skip to main content
6,936,634 members and growing! (20,042 online)
Email Password   helpLost your password?
 
General Programming » Internet / Network » Email     Intermediate License: A Public Domain dedication

Simple MAPI.NET

By NETMaster

Usage of the simple MAPI API.
C#.NET1.0, Win2K, WinXP, Dev
Posted:30 Mar 2002
Views:195,304
Bookmarked:65 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
54 votes for this article.
Popularity: 7.61 Rating: 4.39 out of 5
1 vote, 2.0%
1

2
2 votes, 4.1%
3
8 votes, 16.3%
4
38 votes, 77.6%
5

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


Member

Occupation: Web Developer
Location: Switzerland Switzerland

Other popular Internet / Network articles:

 
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 73 (Total in Forum: 73) (Refresh)FirstPrevNext
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  
Generalhow 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  
GeneralHow 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  
Generalwhat 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  
GeneralWhy hourglass from secondary form on send? Pinmemberc-a-b-3:06 31 Aug '06  
GeneralRe: Why hourglass from secondary form on send? Pinmembervzxcasdfgasd14:15 14 Nov '06  
GeneralGreat Article Pinmemberben_10:27 5 Jun '06  
GeneralGreat article, very useful PinmemberRanjan Banerji8:47 29 Apr '06  
GeneralHow to compare this approach with using the MAPI namespace as in this article? Pinmembertank20913:42 6 Apr '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 30 Mar 2002
Editor: Chris Maunder
Copyright 2002 by NETMaster
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project