function String2AnsiString(s:
String):AnsiString;
var
h: AnsiString;
begin
h:= s;
Result:= PAnsiChar(h);
end;
// uses MAPI
function SendEMail(
Handle:THandle; Mail:TStrings):Cardinal;
type
TAttachAccessArray=
array [0..0]
of TMapiFileDesc;
PAttachAccessArray= ^TAttachAccessArray;
var
MapiMessage: TMapiMessage;
Receip: TMapiRecipDesc;
Attachments: PAttachAccessArray;
AttachCount: Integer;
i1: integer;
FileName:
string;
dwRet: Cardinal;
MAPI_Session: Cardinal;
WndList: Pointer;
h1,h2: AnsiString;
begin
Result:= MAPI_E_FAILURE;
dwRet:= MapiLogon(
Handle, PAnsiChar('
'), PAnsiChar('
'), MAPI_LOGON_UI
or MAPI_NEW_SESSION, 0, @MAPI_Session);
if (dwRet<>SUCCESS_SUCCESS)
then
begin
MessageBox(
Handle, PChar('
Error while trying to send email'), PChar('
Error'), MB_ICONERROR
or MB_OK);
end
else
begin
FillChar(MapiMessage, SizeOf(MapiMessage), #0);
Attachments:=
nil;
FillChar(Receip, SizeOf(Receip), #0);
if Mail.Values['
to']<>'
'
then begin
Receip.ulReserved:= 0;
Receip.ulRecipClass:= MAPI_TO;
Receip.lpszName:= PAnsiChar(String2AnsiString(Mail.Values['
to']));
// geht
Receip.lpszAddress:= PAnsiChar(String2AnsiString('
SMTP:' + Mail.Values['
to']));
// geht
Receip.ulEIDSize:= 0;
MapiMessage.nRecipCount:= 1;
MapiMessage.lpRecips:= @Receip;
end;
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
showmessage(inttostr(AttachCount));
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);
h1:= FileName;
Attachments[i1].lpszPathName:= PAnsiChar(h1);
// geht nicht mit String2AnsiString
h2:= ExtractFileName(FileName);
Attachments[i1].lpszFileName:= PAnsiChar(h2);
// geht nicht mit String2AnsiString
Attachments[i1].lpFileType:=
nil;
end;
MapiMessage.nFileCount := AttachCount;
MapiMessage.lpFiles := @Attachments^;
end;
if Mail.Values['
subject']<>'
'
then MapiMessage.lpszSubject:= PAnsiChar(String2AnsiString(Mail.Values['
subject']));
// geht
if Mail.Values['
body']<>'
'
then MapiMessage.lpszNoteText:= PAnsiChar(String2AnsiString(Mail.Values['
body']));
// geht
WndList := DisableTaskWindows(0);
try
Result:= MapiSendMail(MAPI_Session,
Handle, MapiMessage, MAPI_DIALOG, 0);
finally
EnableTaskWindows(WndList);
end;
MapiLogOff(MAPI_Session,
Handle, 0, 0);
end;
end;