开发者

How do I programmatically send email w/attachment to a known recipient using MAPI in C++? MAPISendMail()

This question is similar, but does not show how to add a recipient.

How do I do both?

We'd like the widest support possible for as many Windows platforms as possible (from XP and greater)

We're using visual studio 2008

Essentially we want to send an email with:

  • pre-filled destination address
  • file attachment
  • subject line

from our program and give the user the ability to add any information or cancel it.

EDIT I am trying to use MAPISendMail() I copied much of the code from the questions linked near the top, but I get no email dlg box and the error return I get from the call is: 0x000f - "The system cannot find the drive specified"

If I comment out the lines to set the recipient, it works fine (of course then I have no recipient pre-filled in)

Here is the code:

#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;
        LPMAPISENDMAIL lpfnMAPISendMail = NULL;

        lpfnMAPIInitialize = (LPMAPIINITIALIZE)GetProcAddress( hMapiModule, "MAPIInitialize" );
        lpfnMAPIUninitialize = (LPMAPIUNINITIALIZE)GetProcAddress( hMapiModule, "MAPIUninitialize" );
        lpfnMAPILogonEx = (LPMAPILOGONEX)GetProcAddress( hMapiModule, "MAPILogonEx" );
        lpfnMAPISendDocuments = (LPMAPISENDDOCUMENTS)GetProcAddress( hMapiModule, "MAPISendDocuments" );
        lpfnMAPISendMail =      (LPMAPISENDMAIL)GetProcAddress( hMapiModule, "MAPISendMail" );

        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 
                    // create the msg.  We need to add recipients AND subject AND the dmp file              

                    // file attachment
                    MapiFileDesc filedesc;              
                    ::ZeroMemory(&filedesc, sizeof(filedesc));                  
                    filedesc.nPosition = (ULONG)-1;
                    filedesc.lpszPathName = "E:\\Development\\Open\\testmail\\testmail.cpp";    

                    // recipient(s)
                    MapiRecipDesc recip;
                    ::ZeroMemory(&recip, sizeof(recip));        
                    recip.lpszName = "QA email";
                    recip.lpszAddress = "qa@myaccount.com";

                    // the message
                    MapiMessage msg;
                    ::ZeroMemory(&msg, sizeof(msg));
                    msg.lpszSubject     = "Test";   
                    msg.nRecipCount     = 1; // if I comment out this line it works fine...                 
                    msg.lpRecips        = &recip;             开发者_JAVA技巧                          
                    msg.nFileCount      = 1;
                    msg.lpFiles         = &filedesc;                

                    hr = (*lpfnMAPISendMail)(0, NULL, &msg, MAPI_LOGON_UI|MAPI_DIALOG, 0);

                    if ( SUCCEEDED( hr ) )
                    {
                        hr = lplhSession->Logoff( 0, 0, 0 );
                        hr = lplhSession->Release();
                        lplhSession = NULL;
                    }
                }
            }

            (*lpfnMAPIUninitialize)();
        }

        FreeLibrary( hMapiModule );
    }

    return 0;
}


Oops - I forgot to set

recip.ulRecipClass = MAPI_TO;

Works great now.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜