How do I programmatically send an email in the same way that I can "Send To Mail Recipient" in Windows Explorer? How do I programmatically send an email in the same way that I can "Send To Mail Recipient" in Windows Explorer? shell shell

How do I programmatically send an email in the same way that I can "Send To Mail Recipient" in Windows Explorer?


This is my MAPI solution:

#include <tchar.h>#include <windows.h>#include <mapi.h>#include <mapix.h>int _tmain( int argc, wchar_t *argv[] ){    HMODULE hMapiModule = LoadLibrary( _T( "mapi32.dll" ) );    if ( hMapiModule != NULL )    {        LPMAPIINITIALIZE lpfnMAPIInitialize = NULL;        LPMAPIUNINITIALIZE lpfnMAPIUninitialize = NULL;        LPMAPILOGONEX lpfnMAPILogonEx = NULL;        LPMAPISENDDOCUMENTS lpfnMAPISendDocuments = NULL;        LPMAPISESSION lplhSession = NULL;        lpfnMAPIInitialize = (LPMAPIINITIALIZE)GetProcAddress( hMapiModule, "MAPIInitialize" );        lpfnMAPIUninitialize = (LPMAPIUNINITIALIZE)GetProcAddress( hMapiModule, "MAPIUninitialize" );        lpfnMAPILogonEx = (LPMAPILOGONEX)GetProcAddress( hMapiModule, "MAPILogonEx" );        lpfnMAPISendDocuments = (LPMAPISENDDOCUMENTS)GetProcAddress( hMapiModule, "MAPISendDocuments" );        if ( lpfnMAPIInitialize && lpfnMAPIUninitialize && lpfnMAPILogonEx && lpfnMAPISendDocuments )        {            HRESULT hr = (*lpfnMAPIInitialize)( NULL );            if ( SUCCEEDED( hr ) )            {                hr = (*lpfnMAPILogonEx)( 0, NULL, NULL, MAPI_EXTENDED | MAPI_USE_DEFAULT, &lplhSession );                if ( SUCCEEDED( hr ) )                {                    // this opens the email client with "C:\attachment.txt" as an attachment                    hr = (*lpfnMAPISendDocuments)( 0, ";", "C:\\attachment.txt", NULL, NULL );                    if ( SUCCEEDED( hr ) )                    {                        hr = lplhSession->Logoff( 0, 0, 0 );                        hr = lplhSession->Release();                        lplhSession = NULL;                    }                }            }            (*lpfnMAPIUninitialize)();        }        FreeLibrary( hMapiModule );    }    return 0;}


You can use a standard "mailto:" command in windows shell. It will run the default mail client.


The following C++ example shows how to invoke the SendTo mail shortcut used by Windows Explorer:

http://www.codeproject.com/KB/shell/sendtomail.aspx