function TDataModule1.Mail(an,
cc,bcc,subject,bodytext,att:
string):boolean;
var Zaehler,
Anzahl,i : LongInt;
IdMessage1 : TIdMessage;
IdPart : TIdMessageParts;
IdAtt : TIdAttachmentFile;
str : TStringList;
begin
IdMessage1:=Nil;
if IdSMTP1.Connected
then IdSMTP1.Disconnect;
//
//
// Einrichten
IdSMTP1.Host:=SMTP_Server;
IdSMTP1.Username:=SMTP_USER;
IdSMTP1.Port:=StrToInt(SMTP_PORT);
IdSMTP1.Password:=SMTP_Pass;
IdSMTP1.MailAgent:='
AFS-Messeplanner';
//
if IdSMTP1.Port>25
then begin
IdSSLIOHandlerSocketOpenSSL1.Destination:=IdSMTP1.Host+'
:'+IntToStr(IdSMTP1.Port);
IdSSLIOHandlerSocketOpenSSL1.Host:=IdSMTP1.Host;
IdSSLIOHandlerSocketOpenSSL1.Port:=IdSMTP1.Port;
IdSMTP1.IOHandler:=IdSSLIOHandlerSocketOpenSSL1;
IdSMTP1.UseTLS:=utUseExplicitTLS;
end;
//
//Write_Protokoll('Mail config Port: '+SMTP_PORT+' '+IdSMTP1.Host);
result:=False;
// wird bei onConnect auf True gesetze
try
IdSMTP1.Connect;
except
on E:
Exception do begin
Write_Protokoll('
Es konnte keine Verbindung aufgebaut werden!'+#13+e.
Message);
end;
end;
//
//Write_Protokoll('connected: '+IdSMTP1.Connected.ToString+' '+att);
//
str:=TStringList.Create;
IdMessage1:=TIdMessage.Create(self);
//
IdMessage1.From.Address:=SMTP_Mail;
IdMessage1.From.DisplayName:=SMTP_MailDisplay;
//
IdMessage1.ContentType := '
multipart/related; type="text/plain';
///"text/html"'; //'text';
IdMessage1.CharSet:='
UTF-8';
IdMessage1.Subject:= AnsiToUtf8(subject);
if Trim(IdMessage1.Subject)='
'
then IdMessage1.Subject:='
kein Betreff';
IdMessage1.Body.Text:=AnsiToUtf8(bodytext);
//
with TIdText.Create(IdMessage1.MessageParts,
nil)
do
begin
Body.Text := AnsiToUtf8(bodytext);
ContentType := '
text/plain';
CharSet:='
UTF-8';
end;
//
if trim(att)<>'
'
then begin
str.Text:=att;
for i:=0
to str.Count-1
do begin
if FileExists(Trim(str[i]))
then begin
IdAtt:=TIdAttachmentFile.Create(IdMessage1.MessageParts,Trim(str[i]));
end;
end;
end;
//
str.Text:=an;
if str.Count>0
then begin
for i:=str.Count-1
downto 0
do begin
if p_Instr(1,str[i],'
@')>0
then begin
if IdMessage1.Recipients.EMailAddresses<>'
'
then IdMessage1.Recipients.EMailAddresses:=IdMessage1.Recipients.EMailAddresses+'
;';
IdMessage1.Recipients.EMailAddresses :=IdMessage1.Recipients.EMailAddresses+str[i];
end;
end;
end;
str.Text:=
cc;
IdMessage1.ccList.EMailAddresses:='
';
if str.Count>0
then begin
for i:=0
to str.Count-1
do begin
if p_Instr(1,str[i],'
@')>0
then begin
if IdMessage1.ccList.EMailAddresses<>'
'
then IdMessage1.ccList.EMailAddresses:=IdMessage1.ccList.EMailAddresses+'
;';
IdMessage1.ccList.EMailAddresses :=IdMessage1.ccList.EMailAddresses+str[i];
end;
end;
end;
str.Text:=bcc;
IdMessage1.BccList.EMailAddresses:='
';
if str.Count>0
then begin
for i:=0
to str.Count-1
do begin
if p_Instr(1,str[i],'
@')>0
then begin
if IdMessage1.BccList.EMailAddresses<>'
'
then IdMessage1.BccList.EMailAddresses:=IdMessage1.BccList.EMailAddresses+'
;';
IdMessage1.BccList.EMailAddresses :=IdMessage1.BccList.EMailAddresses+str[i];
end;
end;
end;
//
try
IdSMTP1.Send(IdMessage1);
result:=True;
except
on E:
Exception do begin
Write_Protokoll('
es ist ein Fehler beim E-Mail senden aufgetreten!'+#13+e.
Message);
end;
end;
try
if IdSMTP1.Connected
then IdSMTP1.Disconnect;
//
FreeAndNil(IdMessage1);
FreeAndNil(str);
//
except
end;
end;