11.15 Send and Retrieve E-Mail with MAPI


Problem

You want to send an e-mail message, but you don't have a Simple Mail Transfer Protocol (SMTP) mail server configured for the computer.

Solution

Use Simple MAPI (Messaging Application Programming Interface) by importing the required function from the unmanaged system library Mapi32.dll.

Discussion

MAPI is an interface that allows you to interact with the mailing features that are integrated into the Windows operating system. You can use MAPI (either through its unmanaged API, or through the MAPI COM component included with Visual Studio 6) to interact with the default mail client (usually Microsoft Outlook or Outlook Express). Tasks include retrieving contact information from the address book, retrieving the messages in the Inbox, and programmatically composing and sending messages. Unfortunately, there are no classes for using MAPI in the .NET Framework. However, you can use the unmanaged Mapi32.dll.

The main challenge to using Simple MAPI in .NET is marshalling the structures that are used in .NET over to structures that Simple MAPI expects and then marshalling the structures returned by Simple MAPI back to the .NET application. This isn't a trivial task. However, Microsoft provides a full solution in a generic C# component that's freely downloadable. The following two projects are available:

  • A class library component that wraps Simple MAPI functions and makes them available through class methods (downloadable at http://support.microsoft.com/?kbid=315653 ).

  • A test program that works with the component to log on, log off, read mail, send mail, and so on ( http://support.microsoft.com/?kbid=315367 ).

The full code for both of these components is straightforward, but too lengthy to list here. It's also provided with the downloadable code for this chapter.

Note  

For a more sophisticated example that builds on Microsoft's Simple MAPI library to create a rich Windows Forms application, a freely downloadable (although unsupported) sample C# project is provided by Thomas Scheidegger at http://www.codeproject.com/csharp/simplemapidotnet.asp .




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net