I Have been using the following function to send mail using MAPI and it works OK, but now I would like to add the option to send Cc and Bcc messages also. and That gives me problems as I haven't been able to find out how
The fileattachments does not work either but I don't use it for now.
function SendMailMAPI(const aFrom, aTo, aBcc, aSubject, aBody: string; aReceipt: boolean; FileNames: TStringList): boolean;
var
MAPIError: DWord;
MapiMessage: TMapiMessage;
Originator: TMapiRecipDesc;
Recipient: TMapiRecipDesc;
MapiFiles: PMapiFileDesc;
FilesTmp: PMapiFileDesc;
FilesCount: Integer;
begin
MAPIError := SUCCESS_SUCCESS;
Result := True;
try
FillChar(MapiMessage, Sizeof(TMapiMessage), 0);
if aReceipt then
MapiMessage.flFlags := MAPI_RECEIPT_REQUESTED;
MapiMessage.lpszSubject := PAnsiChar(AnsiString(aSubject));
MapiMessage.lpszNoteText := PAnsiChar(AnsiString(aBody));
FillChar(Originator, Sizeof(TMapiRecipDesc), 0);
Originator.lpszName := PAnsiChar(AnsiString(aFrom));
Originator.lpszAddress := PAnsiChar(AnsiString(aFrom));
MapiMessage.lpOriginator := nil;
MapiMessage.nRecipCount := 1;
FillChar(Recipient, Sizeof(TMapiRecipDesc), 0);
Recipient.ulRecipClass := MAPI_TO;
Recipient.lpszName := PAnsiChar(AnsiString(aTo));
Recipient.lpszAddress := PAnsiChar(AnsiString(aTo));
MapiMessage.lpRecips := @Recipient;
MapiMessage.nFileCount := FileNames.Count;
MapiFiles := AllocMem(SizeOf(TMapiFileDesc) * MapiMessage.nFileCount);
MapiMessage.lpFiles := MapiFiles;
FilesTmp := MapiFiles;
for FilesCount := 0 to FileNames.Count - 1 do
begin
FilesTmp.nPosition := $FFFFFFFF;
FilesTmp.lpszPathName := PAnsiChar(AnsiString(FileNames.Strings[FilesCount]));
Inc(FilesTmp)
end;
try
MAPIError := MapiSendMail(0, Application.MainForm.Handle, MapiMessage, MAPI_LOGON_UI + MAPI_DIALOG, 0);
{or MAPI_NEW_SESSION}
finally
FreeMem(MapiFiles)
end;
except
on E:Exception do
Logfile.Error('U_Mailing.Mapi.SendMailMAPI: ' + E.Message);
end;
case MAPIError of
MAPI_E_AMBIGUOUS_RECIPIENT:
Showmessage('A recipient matched more than one of the recipient descriptor structures and MAPI_DIALOG was not set. No message was sent.');
MAPI_E_ATTACHMENT_NOT_FOUND:
Showmessage('The specified attachment was not found; no message was sent.');
MAPI_E_ATTACHMENT_OPEN_FAILURE:
Showmessage('The specified attachment could not be opened; no message was sent.');
MAPI_E_BAD_RECIPTYPE:
Showmessage('The type of a recipient was not MAPI_TO, MAPI_CC, or MAPI_BCC. No message was sent.');
MAPI_E_FAILURE:
Showmessage('One or more unspecified errors occurred; no message was sent.');
MAPI_E_INSUFFICIENT_MEMORY:
Showmessage('There was insufficient memory to proceed. No message was sent.');
MAPI_E_LOGIN_FAILURE:
Showmessage('There was no default logon, and the user failed to log on successfully when the logon dialog box was displayed. No message was sent.');
MAPI_E_TEXT_TOO_LARGE:
Showmessage('The text in the message was too large to sent; the message was not sent.');
MAPI_E_TOO_MANY_FILES:
Showmessage('There were too many file attachments; no message was sent.');
MAPI_E_TOO_MANY_RECIPIENTS:
Showmessage('There were too many recipients; no message was sent.');
MAPI_E_UNKNOWN_RECIPIENT:
Showmessage('A recipient did not appear in the address list; no message was sent.');
MAPI_E_USER_ABORT:
Showmessage('The user canceled the process; no message was sent.');
else
Showmessage('MAPISendMail failed with an unknown error code.');
Result := False;
end;
end;
Replace Recipient.ulRecipClass := MAPI_TO
with Recipient.ulRecipClass := MAPI_CC
or Recipient.ulRecipClass := MAPI_BCC