views:

870

answers:

4

ShellExecute() allows me to perform simple shell tasks, allowing the system to take care of opening or printing files. I want to take a similar approach to sending an email attachment programmatically.

I don't want to manipulate Outlook directly, since I don't want to assume which email client the user uses by default. I don't want to send the email directly, as I want the user to have the opportunity to write the email body using their preferred client. Thus, I really want to accomplish exactly what Windows Explorer does when I right click a file and select Send To -> Mail Recipient.

I'm looking for a C++ solution.

Thank you in advance for your responses.

+2  A: 

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

TcKs
A: 

You'll need to implement a MAPI client. This will let you prefill the document, add attachments, etc.. before presenting the message to the user to send off. You can use the default message store to use their default mail client.

Eclipse
+1  A: 

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

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

Craig Lebakken
+4  A: 

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;
}
Jeff Hillman
How do I set the recipient(s)?
Tim
This is not a 100% programmatic solution. This code opens up the email client with the provided file added as an attachment. It is then up to the user to type in the recipient(s), just as they would any other time.
Jeff Hillman