unit MultiSendMail;
interface
uses Windows, Classes, Forms, SysUtils,
Mapi ;
type TStrArray =
array of String;
const MapiRecipClassStrings :
array[1..3]
of String =('
to','
cc','
bcc');
function SendEMail(
Handle:THandle; Mail:TStrings):Cardinal;
implementation
{ **** Verwendung ***
procedure TTestForm.Mail1Click(Sender: TObject);
var
mail: TStringList;
begin
mail:= TStringList.Create;
try
mail.values['to0']:= 'fifi@abc.de';
mail.values['to1']:= 'otto@abc.de';
mail.values['to2']:= 'Maria@abc.de';
mail.values['to3']:= 'Joseph@abc.de';
mail.values['cc0']:= 'detlef@abc.de';
mail.values['cc1']:= 'Mordo@abc.de';
mail.values['bcc0']:= 'Klaus@abc.de';
mail.values['subject']:= 'subject';
mail.values['body']:= 'text text text';
mail.values['attachment0']:= 'C:\Anhang1.txt';
mail.values['attachment1']:= 'C:\Anhang2.txt';
mail.values['attachment2']:= 'C:\Anhang3.txt';
SendEMail(Self.Handle, mail);
finally
mail.Free;
end;
end;
}
function SendEMail(
Handle:THandle; Mail:TStrings):Cardinal;
var
MapiMessage: TMapiMessage;
Receip:
array of TMapiRecipDesc;
Attachments:
array of TMapiFileDesc;
i,
j,
mc,
ReceipCount,
AttachCount: Integer;
FileName:
string;
MAPI_Session: Cardinal;
WndList: Pointer;
MapiRecipClassCounter :
array[1..3]
of Integer;
function GetMailValue (AMapiRecipClass,
Index : Integer) :
string;
begin
result:=Mail.Values[MapiRecipClassStrings[AMapiRecipClass]+IntToStr(
index)];
end;
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('
Fehler beim Versuch eMails zu versenden'), PChar('
Error'), MB_ICONERROR
or MB_OK)
else
begin
for MC:=MAPI_TO
to MAPI_BCC
do
begin
MapiRecipClassCounter[MC]:=0;
for j:= 0
to MaxInt
do // Empfänger zählen
begin
if GetMailValue(MC,j) = '
'
then break;
Inc(MapiRecipClassCounter[MC]);
end;
end;
ReceipCount:=MapiRecipClassCounter[MAPI_TO]+
MapiRecipClassCounter[MAPI_CC]+
MapiRecipClassCounter[MAPI_BCC];
// Gesamtempfängerzahl errechnen
SetLength(Receip,ReceipCount);
// Empfängerarray dimensionieren
i:=0;
if ReceipCount>0
then
begin
for MC:=MAPI_TO
to MAPI_BCC
do
if MapiRecipClassCounter[MC]>0
then
for j:=0
to MapiRecipClassCounter[MC]-1
do
begin
Receip[i].ulReserved:= 0;
Receip[i].ulRecipClass:= MC;
Receip[i].lpszName:= StrNew(PAnsiChar(AnsiString(GetMailValue(MC,j))));
Receip[i].lpszAddress:= StrNew(PAnsiChar(AnsiString('
SMTP:' + GetMailValue(MC,j))));
Receip[i].lpEntryID:=nil;
Receip[i].ulEIDSize:= 0;
inc(i);
end;
end;
MapiMessage.nRecipCount:= ReceipCount;
// Empfängeranzahl in Message setzen
MapiMessage.lpRecips:= Pointer(Receip);
// Pointer auf Empfängerarray übergeben
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;
// Anhänge verarbeiten
for i:= 0
to MaxInt
do // zählen
begin
if Mail.Values['
attachment' + IntToStr(i)] = '
'
then break;
Inc(AttachCount);
end;
SetLength(Attachments, AttachCount);
// array dimensinieren
if AttachCount>0
then
begin
for i:=0
to AttachCount-1
do
begin
FileName:= Mail.Values['
attachment' + IntToStr(i)];
// Einträge erzeugen
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;
// Anzahl der Anhänge in der Message setzen
MapiMessage.lpFiles := Pointer(Attachments);
// Pointer auf AnhangsArray übergeben
end;
WndList:= DisableTaskWindows(0);
// "semimodal" machen
try
Result:= MapiSendMail(MAPI_Session,
Handle, MapiMessage, MAPI_DIALOG, 0);
// senden der Mail
finally
EnableTaskWindows(WndList);
for i:=0
to ReceipCount-1
do // aufräumen
begin
StrDispose(Receip[i].lpszName);
StrDispose(Receip[i].lpszAddress);
end;
for i:=0
to AttachCount-1
do
begin
StrDispose(Attachments[i].lpszPathName);
StrDispose(Attachments[i].lpszFileName);
end;
Finalize(Attachments);
// richtig aufräumen
Finalize(Receip);
end;
MapiLogOff(MAPI_Session,
Handle, 0, 0);
end;
end;
end.