function SendEMail(
Handle: THandle; Mail: TStrings): Cardinal;
var
lMessage: TMapiMessage;
lMailTo :
array of TMapiRecipDesc;
iCounter, j, mc, lMailToCount: integer;
lMapiSession: Cardinal;
lWndList : Pointer;
lAMailCounter:
array[1..3]
of integer;
lAttachements:
array of TMapiFileDesc;
lAttachCount: Integer;
lFileName:
string;
i: Integer;
function GetEMailValue(lMailToClass,
index: integer) :
string;
begin
result := Mail.Values[AnsiString(lMailToClass)+inttostr(
index)];
end;
begin
result := MAPI_E_FAILURE;
if (MapiLogOn(
Handle, PAnsiChar('
'), PAnsiChar('
'), MAPI_LOGON_UI
or MAPI_NEW_SESSION, 0, @lMapiSession) <> SUCCESS_SUCCESS)
then
begin
MessageBox(
Handle, PChar('
Versenden fehlgeschlagen'), PChar('
Fehler'), MB_ICONERROR
or MB_OK)
end
else
begin
for mc := MAPI_TO
to MAPI_BCC
do
begin
lAMailCounter[mc] := 0;
end;
//++Empfänger zählen, evtl raus++
for j := 0
to MaxInt
do
begin
if GetEMailValue(mc,j) = '
'
then
Break;
Inc(lAMailCounter[mc]);
end;
//++ ++
end;
//++Gesamtempfängerzahl - evtl raus++
lMailToCount := lAMailCounter[MAPI_TO] + lAMailCounter[MAPI_CC] + lAMailCounter[MAPI_BCC];
//++ ++
//++ Empfängerarray - evtl raus++
SetLength(lMailTo, lMailToCount);
//++ ++
iCounter := 0;
if lMailToCount > 0
then
begin
for mc := MAPI_TO
to MAPI_BCC
do
if lAMailCounter[mc] > 0
then
for j := 0
to lAMailCounter[mc] -1
do
begin
lMailTo[iCounter].ulReserved := 0;
lMailTo[iCounter].ulRecipClass := mc;
lMailTo[iCounter].lpszName := StrNew(PAnsiChar(AnsiString(GetEMailValue(mc,j))));
lMailTo[iCounter].lpszAddress := StrNew(PAnsiChar(AnsiString('
SMTP:' + GetEMailValue(mc, j))));
lMailTo[iCounter].lpEntryID :=
nil;
lMailTo[iCounter].ulEIDSize := 0;
Inc(iCounter);
end;
end;
//++ Empfängeranzahl in Mail und Pointer auf Empfängerarray - evtl raus++
lMessage.nRecipCount := lMailToCount;
lMessage.lpRecips := Pointer(lMailTo);
//++ ++
if Mail.Values['
subject'] <> '
'
then
begin
lMessage.lpszSubject := PAnsiChar(AnsiString(Mail.Values['
subject']));
end;
if Mail.Values['
body'] <> '
'
then
begin
lMessage.lpszNoteText := PAnsiChar(AnsiString(Mail.Values['
body']));
end;
lAttachCount := 0;
for i := 0
to MaxInt
do
begin
if Mail.Values['
attachment' + IntToStr(icounter)] = '
'
then
Break;
Inc(lAttachCount);
end;
SetLength(lAttachements, lAttachCount);
if lAttachCount > 0
then
begin
for iCounter := 0
to lAttachCount -1
do
begin
lFileName := Mail.Values['
attachement' + IntToStr(iCounter)];
lAttachements[iCounter].ulReserved := 0;
lAttachements[iCounter].flFlags := 0;
lAttachements[iCounter].nPosition := ULONG($FFFFFFFF);
lAttachements[iCounter].lpszPathName := StrNew(PAnsiChar(AnsiString(lFileName)));
lAttachements[iCounter].lpszFileName := StrNew(PAnsiChar(AnsiString(ExtractFileName(lFileName))));
lAttachements[iCounter].lpFileType :=
nil;
end;
lMessage.nFileCount := lAttachCount;
lMessage.lpFiles := Pointer(lAttachements);
end;
lWndList := DisableTaskWindows(0);
try
result := MapiSendMail(lMapiSession,
Handle, lMessage, MAPI_DIALOG, 0);
finally
EnableTaskWindows(lWndList);
for iCounter := 0
to lMailToCount-1
do
begin
StrDispose(lMailTo[iCounter].lpszName);
StrDispose(lMailTo[iCounter].lpszAddress);
end;
for i := 0
to lAttachCount-1
do
begin
StrDispose(lAttachements[iCounter].lpszPathName);
StrDispose(lAttachements[iCounter].lpszFileName);
end;
Finalize(lAttachements);
Finalize(lMailTo);
end;
MapiLogOff(lMapiSession,
Handle, 0, 0);
end;