//------------------------------------------------------------------------
procedure TfrmGetMail.btnRefreshClick(Sender: TObject);
//------------------------------------------------------------------------
// Abruf der Mails
//------------------------------------------------------------------------
var i : longint;
iMp : integer;
iMax : longint;
aMsg : tIdMessage;
s :
string;
lWasActive : boolean;
begin
if not lRunning
then begin
HideIcon(0);
ShowIcon(IconActive,0, '
Checking for mail');
Logg('
Checking for mail');
lRunning := true;
// Timer disablen
Mailtimer.Enabled := false;
// Werte setzen
tPopMail.Host := sServerName;
tPopMail.Username := sUserName;
tPopMail.Password := sPassWord;
// lbxMail.Clear;
// Verbinden
try
btnRefresh.Enabled := false;
Logg('
Connecting');
lblStatus.Caption := '
Connecting';
lWasActive := true;
// Automatisch mit dem Internet verbinden
if not InternetGetConnectedState(
NIL, 0)
then begin
lWasActive := false;
InternetAutodial(internet_autodial_force_unattended,0 );
end;
// Timeout ist in Millisekunden
tPopMail.Connect(iTimeOut * 1000);
// Anzahl der Mails
iMax := tPopMail.CheckMessages;
// Alle Mails durchlaufen
for i := 1
to iMax
do begin
lblStatus.Caption := '
Getting mail '+inttostr(i)+'
/'+inttostr(iMax);
Logg('
Getting mail '+inttostr(i)+'
/'+inttostr(iMax));
Application.ProcessMessages;
aMsg := tIdMessage.Create(
NIL);
// Zur Analyse der einzelnen Bestandteile
aMsg.NoDecode := false;
tPopMail.Retrieve(i, aMsg);
aMsg.ProcessHeaders;
Application.ProcessMessages;
// Protokoll eintragen
Logg('
From: '+aMsg.From.Address);
if (sFromAdress = '
')
or
(sFromAdress = aMsg.From.Address)
then begin
// eMail analysieren
for iMp := 0
to aMsg.MessageParts.Count-1
do begin
if aMsg.MessageParts.Items[iMp]
is tIdAttachment
then begin
// Dateinamen holen
s := (aMsg.MessageParts.Items[iMp]
as tIdAttachment).Filename;
// Extension ändern
if sFileExt <> '
'
then
s := ChangeFileExt(s, '
.'+sFileExt);
// Datei speichern
(aMsg.MessageParts.Items[iMp]
as tIdAttachment).savetofile(sDataPath+s);
// Protokoll eintragen
Logg('
Attachment saved as '+sDataPath+s);
end;
end;
// Gesamte Nachricht speichern
DateTimeToString(s,'
yyyymmdd_hhnnsszzz',now);
aMsg.SaveToFile(sMailPath+s+'
.eml');
// Protokoll eintragen
Logg('
Mail saved as '+sMailPath+s+'
.eml');
// Nachricht auf dem Server löschen
tPopMail.Delete(i)
end
else begin
Logg('
Not accepted, wrong from Adress');
end;
// Message-Objekt löschen
aMsg.Free;
end;
except
on E:
Exception do begin
Logg(e.
Message);
if lPopupMsg
then begin
ErrorFlashStart;
// MessageDlg(e.Message, mtError,[mbOk], 0);
end;
end;
end;
// Verbindung beenden
Logg('
Disconnecting');
tPopMail.Disconnect;
if not lWasActive
then
InternetAutoDialHangup(0);
btnRefresh.Enabled := true;
MailTimer.Enabled := true;
lRunning := false;
lblStatus.Caption := '
Waiting';
iSeconds := iInterval;
HideIcon(0);
ShowIcon(IconInactive,0, '
Waiting');
end;
end;