1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| function SendEMail(Handle: THandle; Mail: TStrings): Cardinal;
type
TReceipArray = ARRAY [0..0] of TMapiRecipDesc;
PReceipArray = ^TReceipArray;
TAttachAccessArray = array [0..0] of TMapiFileDesc;
PAttachAccessArray = ^TAttachAccessArray;
var
MapiMessage: TMapiMessage;
Receips: PReceipArray;
Attachments: PAttachAccessArray;
nbrDestTo,nbrDestCC,nbrDestBcc,AttachCount: Integer;
i1, iR: integer;
tox,Destinataire,FileName: string;
dwRet: Cardinal;
MAPI_Session: Cardinal;
WndList: Pointer;
begin
dwRet := MapiLogon(Handle,
PChar(''),
PChar(''),
MAPI_LOGON_UI or MAPI_NEW_SESSION,
0, @MAPI_Session);
if (dwRet <> SUCCESS_SUCCESS) then
begin
MessageBox(Handle,
PChar('Erreur pendant l''envoi du mail'),
PChar('Erreur'),
MB_ICONERROR or MB_OK); end
else
begin
FillChar(MapiMessage, SizeOf(MapiMessage), #0);
Attachments := nil;
Receips := nil;
nbrDestTo := 0;
nbrDestCC := 0;
nbrDestBCC := 0;
for i1 := 0 to MaxInt do
begin
if Mail.Values['to' + IntToStr(i1)] = '' then break;
Inc(nbrDestTo);
end;
for i1 := 0 to MaxInt do
begin
if Mail.Values['cc' + IntToStr(i1)] = '' then break;
Inc(nbrDestCC);
end;
for i1 := 0 to MaxInt do
begin
if Mail.Values['bcc' + IntToStr(i1)] = '' then break;
Inc(nbrDestBCC);
end;
if (nbrDestTo+nbrDestCC+nbrDestBcc) > 0 then
GetMem(Receips, SizeOf(TMapiRecipDesc) * (nbrDestTo+nbrDestCC+nbrDestBcc));
iR := -1;
if nbrDestTo > 0 then
For i1 := 0 to nbrDestTo -1 do
begin
Inc(iR);
Destinataire := Mail.Values['to' + IntToStr(i1)];
Receips[iR].ulReserved := 0;
Receips[iR].ulRecipClass := MAPI_TO;
Receips[iR].lpszName := StrNew(PChar(Destinataire));
Receips[iR].lpszAddress := StrNew(PChar('SMTP:' + Destinataire));
Receips[iR].ulEIDSize := 0;
end;
if nbrDestCC > 0 then
For i1 := 0 to nbrDestCC -1 do
begin
Inc(iR);
Destinataire := Mail.Values['cc' + IntToStr(i1)];
Receips[iR].ulReserved := 0;
Receips[iR].ulRecipClass := MAPI_CC;
Receips[iR].lpszName := StrNew(PChar(Destinataire));
Receips[iR].lpszAddress := StrNew(PChar('SMTP:' + Destinataire));
Receips[iR].ulEIDSize := 0;
end;
if nbrDestBcc > 0 then
For i1 := 0 to nbrDestBcc -1 do
begin
Inc(iR);
Destinataire := Mail.Values['bcc' + IntToStr(i1)];
Receips[iR].ulReserved := 0;
Receips[iR].ulRecipClass := MAPI_BCC;
Receips[iR].lpszName := StrNew(PChar(Destinataire));
Receips[iR].lpszAddress := StrNew(PChar('SMTP:' + Destinataire));
Receips[iR].ulEIDSize := 0;
end;
MapiMessage.nRecipCount := NbrDestTo+NbrDestCC+NbrDestBcc;
MapiMessage.lpRecips := @Receips^;
AttachCount := 0;
for i1 := 0 to MaxInt do
begin
if Mail.Values['attachment' + IntToStr(i1)] = '' then
break;
Inc(AttachCount);
end;
if AttachCount > 0 then
begin
GetMem(Attachments, SizeOf(TMapiFileDesc) * AttachCount);
for i1 := 0 to AttachCount - 1 do
begin
FileName := Mail.Values['attachment' + IntToStr(i1)];
Attachments[i1].ulReserved := 0;
Attachments[i1].flFlags := 0;
Attachments[i1].nPosition := ULONG($FFFFFFFF);
Attachments[i1].lpszPathName := StrNew(PChar(FileName));
Attachments[i1].lpszFileName :=
StrNew(PChar(ExtractFileName(FileName)));
Attachments[i1].lpFileType := nil;
end;
MapiMessage.nFileCount := AttachCount;
MapiMessage.lpFiles := @Attachments^;
end;
if Mail.Values['subject'] <> '' then
MapiMessage.lpszSubject := StrNew(PChar(Mail.Values['subject']));
if Mail.Values['body'] <> '' then
MapiMessage.lpszNoteText := StrNew(PChar(Mail.Values['body']));
WndList := DisableTaskWindows(0);
try
Result := MapiSendMail(MAPI_Session, Handle,
MapiMessage, MAPI_DIALOG, 0);
finally
EnableTaskWindows( WndList );
end;
for i1 := 0 to AttachCount - 1 do
begin
StrDispose(Attachments[i1].lpszPathName);
StrDispose(Attachments[i1].lpszFileName);
end;
if Assigned(MapiMessage.lpszSubject) then
StrDispose(MapiMessage.lpszSubject);
if Assigned(MapiMessage.lpszNoteText) then
StrDispose(MapiMessage.lpszNoteText);
for i1 := 0 to (NbrDestTo+NbrDestCC+NbrDestBcc) - 1 do
begin
if Assigned(Receips[i1].lpszAddress) then
StrDispose(Receips[i1].lpszAddress);
if Assigned(Receips[i1].lpszName) then
StrDispose(Receips[i1].lpszName);
end;
MapiLogOff(MAPI_Session, Handle, 0, 0);
end;
end; |
Partager