Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

MAPISendMail (mapi32)
 
.
Summary
MAPISendMail - Sends a message.

C# Signature:

  /// <summary>
  /// The MAPISendMail function sends a message.
  ///
  /// This function differs from the MAPISendDocuments function in that it allows greater
  /// flexibility in message generation.
  /// </summary>
  [DllImport("MAPI32.DLL", CharSet=CharSet.Ansi)]
  public static extern uint MAPISendMail(IntPtr lhSession, IntPtr ulUIParam,
    MapiMessage lpMessage, uint flFlags, uint ulReserved);

VB Signature:

    <DllImport("MAPI32.DLL")> _
    Private Shared Function MAPISendMail(ByVal sess As IntPtr, ByVal hwnd As IntPtr, ByVal message As MapiMessage,
       ByVal flg As Integer, ByVal rsv As Integer) As Integer
    End Function

User-Defined Structures:

MAPI

User-Defined Constants:

MAPI

Notes:

This signature is valid, if the MapiMessage is defined as a class. If MapiMessage is defined as a structure a 'ref' has to be added before 'MapiMessage lpMessage' parameter

Tips & Tricks:

Watch it! MAPI32 is not supported from managed code [http://blogs.msdn.com/mstehle/archive/2007/10/03/fyi-why-are-mapi-and-cdo-1-21-not-supported-in-managed-net-code.aspx]

Please add some!

Sample Code:

    [DllImport("MAPI32.DLL")]
    static extern int MAPISendMail(IntPtr sess, IntPtr hwnd, MapiMessage message, int flg, int rsv);

    public int SendMail(string fileName)
    {
        MapiMessage msg = new MapiMessage();
        msg.subject = "Hello World!";
        msg.noteText = "See attached file for details";
        msg.files = GetAttachments(fileName, out msg.fileCount);

        int result = MAPISendMail(new IntPtr(0), new IntPtr(0), msg, 0x00000001 | 0x00000008, 0);
        if (result > 1 || result < 0)
        throw new System.InvalidOperationException();
        return result;
    }

    IntPtr GetAttachments(string fileName, out int fileCount)
    {
        int size = Marshal.SizeOf(typeof(MapiFileDesc));
        IntPtr intPtr = Marshal.AllocHGlobal(size);

        MapiFileDesc mapiFileDesc = new MapiFileDesc();
        //An integer used to indicate where in the message text to render the attachment.
        mapiFileDesc.position = -1;
        int ptr = (int)intPtr;

        mapiFileDesc.name = Path.GetFileName(fileName);
        mapiFileDesc.path = fileName;
        Marshal.StructureToPtr(mapiFileDesc, (IntPtr)ptr, false);
        ptr += size;

        fileCount = 1;
        return intPtr;
    }
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public class MapiMessage
    {
    public int reserved;
    public string subject;
    public string noteText;
    public string messageType;
    public string dateReceived;
    public string conversationID;
    public int flags;
    public IntPtr originator;
    public int recipCount;
    public IntPtr recips;
    public int fileCount;
    public IntPtr files;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public class MapiFileDesc
    {
    public int reserved;
    public int flags;
    public int position;
    public string path;
    public string name;
    public IntPtr type;
    }

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions