5,548,129 members and growing! (18,337 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#Windows, .NET, .NET 1.0, Win2K, WinXPVS.NET2002, Visual Studio, Dev

Posted: 30 Mar 2002
Updated: 30 Mar 2002
Views: 163,376
Bookmarked: 52 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
51 votes for this Article.
Popularity: 7.46 Rating: 4.37 out of 5
1 vote, 2.2%
1
0 votes, 0.0%
2
2 votes, 4.3%
3
8 votes, 17.4%
4
35 votes, 76.1%
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



Occupation: Web Developer
Location: Switzerland Switzerland

Other popular Internet / Network articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 69 (Total in Forum: 69) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionIs it possible to decrease the time for enabling the allow button on the security warning programmaticallymemberAmokMe3:35 10 Sep '08  
GeneralAwesome samplememberjchalfant7:09 21 May '08  
Generalhow to read mail from directly exchange servermemberusha gupta21:39 9 Mar '08  
QuestionIs it possible? [modified]memberMember 41003804:17 2 Jan '08  
Questionread mail which is in folder that is out from indoxmemberagjsps18:21 29 Oct '07  
GeneralHow to send HTML page as bodymemberabhattad21:12 16 Oct '07  
GeneralProblems setting the messageTypePropertymemberplanzaplanza13:12 16 Oct '07  
GeneralRe: Problems setting the messageTypePropertymemberplanzaplanza11:43 17 Oct '07  
QuestionGet only the last received mail from thunderbirdmembertomthekid1:18 22 May '07  
Generalwhat about thunderbirdmemberaleccc21:35 16 May '07  
GeneralLotus Notes 6.5 clientmemberAndreyR7775:10 22 Feb '07  
AnswerRe: Lotus Notes 6.5 clientmemberAaron Browne20:46 18 Oct '07  
GeneralPut message to inbox with MAPImemberArsenmkrt3:28 20 Feb '07  
QuestionOutlook Express MapiDialog causes errormemberdaues5:39 2 Feb '07  
QuestionError: MAPI login failure [3]memberdaniel9910:26 16 Nov '06  
QuestionOutlook 2007 attachments?memberRichard__2:39 8 Nov '06  
GeneralWhy hourglass from secondary form on send?memberc-a-b-3:06 31 Aug '06  
GeneralRe: Why hourglass from secondary form on send?membervzxcasdfgasd14:15 14 Nov '06  
GeneralGreat Articlememberben_10:27 5 Jun '06  
GeneralGreat article, very usefulmemberRanjan Banerji8:47 29 Apr '06  
GeneralHow to compare this approach with using the MAPI namespace as in this article?membertank20913:42 6 Apr '06  
GeneralMS Outlook Hangsmemberkperwaiz20:01 13 Feb '06  
GeneralRe: MS Outlook HangsmemberJeff Lindholm4:28 15 Sep '06  
GeneralHow do you avoid the Outlook security popup?memberyoungDev11:45 26 Oct '05  
GeneralRe: How do you avoid the Outlook security popup?memberAntony M Kancidrowski7:10 15 Mar '06  

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

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