Fill the destination email field using MAPISendMail

1k views Asked by At

I'm implementing a "Print and Email" function in my application, and I launch the default email client using MAPISendMail, with a file attachment pre-selected. I want to be able to automatically fill the "To" field, as well, but I couldn't find an option for that in the MapiMessage structure. There are options for subject, body, attachments, but not for the "To" field.

Is there any way to fill the "To" field using MAPISendMail?

enter image description here

1

There are 1 answers

0
sashoalm On BEST ANSWER

I found the answer, there is a recipient field in the MapiMessage structure. Here some sample code I found at http://www.experts-exchange.com/Programming/Microsoft_Development/A_1820-Sending-Email-with-MAPI.html that illustrates how to fill the recipient field:

BOOL SendMail(LPCSTR lpszSubject, LPCSTR lpszTo, 
    LPCSTR lpszName, LPCSTR lpszText)
{
    HINSTANCE hMAPI = ::LoadLibrary(L"mapi32.dll");
    LPMAPISENDMAIL lpfnMAPISendMail = 
        (LPMAPISENDMAIL)::GetProcAddress(hMAPI, "MAPISendMail");

    char szTo[MAX_PATH] = { 0 };
    strcat_s(szTo, lpszTo);

    char szName[MAX_PATH] = { 0 };
    strcat_s(szName, lpszName);

    MapiRecipDesc recipient[1] = { 0 };
    recipient[0].ulRecipClass = MAPI_TO;
    recipient[0].lpszAddress = szTo;
    recipient[0].lpszName = szName;

    char szSubject[MAX_PATH] = { 0 };
    strcat_s(szSubject, lpszSubject);

    char szText[MAX_PATH] = { 0 };
    strcat_s(szText, lpszText);

    MapiMessage MAPImsg = { 0 };
    MAPImsg.lpszSubject = szSubject;
    MAPImsg.lpRecips = recipient;
    MAPImsg.nRecipCount = 1;
    MAPImsg.lpszNoteText = szText;

    ULONG nSent = lpfnMAPISendMail(0, 0, 
        &MAPImsg, MAPI_LOGON_UI | MAPI_DIALOG, 0);

    FreeLibrary(hMAPI);
    return (nSent == SUCCESS_SUCCESS || nSent == MAPI_E_USER_ABORT);
}