C++ SimpleMAPI SendMail always fails?

3.2k Views Asked by At

I am trying to use SimpleMAPI to display a 'write message' dialogue with an attachment on Vista SP1 with either Windows Mail or Thunderbird in a C++ app (Borland C++ Builder 2006). I should be able to use MAPISendMail to do this.

I don't fill in a recipient address as I expect the user to do that when the mail client displays a 'write message' dialog. I also don't fill in an originator address as I expect the mail client to use the default. I have tried hardcoding them to see if thats the problem and it is not.

My code looks like this:

HINSTANCE hMAPI;
LPMAPISENDMAIL pSendMail;
MapiMessage message;
MapiFileDesc file;

ZeroMemory( &message, sizeof( MapiMessage ) );
ZeroMemory( &file, sizeof( MapiFileDesc ) );

hMAPI = LoadLibraryA( "MAPI32.DLL" );

pSendMail = (LPMAPISENDMAIL)GetProcAddress( hMAPI, "MAPISendMail" );

// setup the attachment...
file.nPosition     = -1;
file.lpszPathName  = "C:\\my_attachment.dat";

// set up the message...
message.lpszSubject     = "My Subject";
message.lpszNoteText    = "My Message...";
message.lpszMessageType = "";
message.nRecipCount     = 0;
message.lpRecips        = NULL; // we don't know the recipient address(s)
message.nFileCount      = 1;
message.lpFiles         = &file;
message.lpOriginator    = NULL; // we don't know the users from address

dwResult = pSendMail( lhSessionNull, (DWORD)Application->Handle, &message, MAPI_LOGON_UI | MAPI_DIALOG, 0 );
if( dwResult == SUCCESS_SUCCESS )
{ 
  // ...yay! :)
}
else
{
  // ...we always fail here with: MAPI_E_FAILURE
}

It always fails with error code 2 (MAPI_E_FAILURE). What am I doing wrong?

Many thanks in advance.

4

There are 4 best solutions below

0
On

Using similar in Delphi and discovered that it does not work from inside a thread. (Delphi TThread component) I used the exact same code and call from within the thread always failed, even though I used synchronize

0
On
0
On

message.lpRecips = NULL; // we don't know the recipient address(s)

Try assinging lpRecips and set it's lpszAddress to "SMTP:"

2
On

You have to first logon to the MAPI session

LHANDLE hMapiSession;
status = lpMapiLogon(NULL, NULL, NULL, MAPI_NEW_SESSION | MAPI_LOGON_UI, 0, &hMapiSession);

then, you can call SendMail(). And after that, you have to logoff again:

lpMapiLogoff(hMapiSession, NULL, 0, 0);