function SendEMail(
Handle:THandle; Mail:TStrings):Cardinal;
var
MapiMessage: TMapiMessage;
Receip: TMapiRecipDesc;
Attachments:
array of TMapiFileDesc;
i, AttachCount: Integer;
FileName:
string;
MAPI_Session: Cardinal;
WndList: Pointer;
begin
Result:= MAPI_E_FAILURE;
if (MapiLogon(
Handle, PAnsiChar('
'), PAnsiChar('
'), MAPI_LOGON_UI
or MAPI_NEW_SESSION, 0, @MAPI_Session) <> SUCCESS_SUCCESS)
then
MessageBox(
Handle, PChar('
Error while trying to send email'), PChar('
Error'), MB_ICONERROR
or MB_OK)
else
begin
if Mail.Values['
to']<>'
'
then begin
Receip.ulReserved:= 0;
Receip.ulRecipClass:= MAPI_TO;
Receip.lpszName:= PAnsiChar(AnsiString(Mail.Values['
to']));
Receip.lpszAddress:= PAnsiChar(AnsiString('
SMTP:' + Mail.Values['
to']));
Receip.ulEIDSize:= 0;
MapiMessage.nRecipCount:= 1;
MapiMessage.lpRecips:= @Receip;
end;
if Mail.Values['
subject']<>'
'
then MapiMessage.lpszSubject:= PAnsiChar(AnsiString(Mail.Values['
subject']));
if Mail.Values['
body']<>'
'
then MapiMessage.lpszNoteText:= PAnsiChar(AnsiString(Mail.Values['
body']));
AttachCount:= 0;
for i:= 0
to MaxInt
do begin
if Mail.Values['
attachment' + IntToStr(i)] = '
'
then break;
Inc(AttachCount);
end;
SetLength(Attachments, AttachCount);
if AttachCount>0
then begin
for i:=0
to AttachCount-1
do begin
FileName:= Mail.Values['
attachment' + IntToStr(i)];
Attachments[i].ulReserved:= 0;
Attachments[i].flFlags:= 0;
Attachments[i].nPosition:= ULONG($FFFFFFFF);
Attachments[i].lpszPathName:= StrNew(PAnsiChar(AnsiString(FileName)));
Attachments[i].lpszFileName:= StrNew(PAnsiChar(AnsiString(ExtractFileName(FileName))));
Attachments[i].lpFileType:=
nil;
end;
MapiMessage.nFileCount := AttachCount;
MapiMessage.lpFiles := Pointer(Attachments);
end;
WndList:= DisableTaskWindows(0);
try
Result:= MapiSendMail(MAPI_Session,
Handle, MapiMessage, MAPI_DIALOG, 0);
finally
EnableTaskWindows(WndList);
for i:=0
to AttachCount-1
do begin
StrDispose(Attachments[i].lpszPathName);
StrDispose(Attachments[i].lpszFileName);
end;
Finalize(Attachments);
end;
MapiLogOff(MAPI_Session,
Handle, 0, 0);
end;
end;