unit Unit1;
interface
uses
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.SvcMgr,
Vcl.Dialogs,
ZAbstractConnection, ZConnection, Data.DB, ZAbstractRODataset,ComCtrls,
ZAbstractDataset, ZDataset, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack,
IdSSL, IdSSLOpenSSL, IdMessage, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdExplicitTLSClientServerBase,IdAttachment, IdMessageClient, IdPOP3,IniFiles,
Vcl.ExtCtrls,IdText,Registry;
type
TMountainWebMailer = class(TService)
pop: TIdPOP3;
msg: TIdMessage;
IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
MAILDATENBANK: TZQuery;
DMAILDATENBANK: TDataSource;
MAILKONTEN: TZQuery;
DMAILKONTEN: TDataSource;
Z_NUMMER: TZQuery;
ZConnection1: TZConnection;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
private
{ Private-Deklarationen }
ini: TIniFile;
filename:String;
public
function GetServiceController: TServiceController; override;
{ Public-Deklarationen }
end;
var
MountainWebMailer: TMountainWebMailer;
implementation
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
MountainWebMailer.Controller(CtrlCode);
end;
function TMountainWebMailer.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TMountainWebMailer.Timer1Timer(Sender: TObject);
var MailCount,Mailgroesse,intIndex :Integer;
itm:TListItem;
i:integer;
s,extensio:string;
neue_nummer:String;
ininame:String;
lokaler_pfad,
hostname,
datenpfad,
installationspfad,
domain_name :String;
begin
with TEventLogger.Create('Mailservice') do
begin
try
LogMessage('ServiceStart...', EVENTLOG_INFORMATION_TYPE);
finally
Free;
end;
end;
ininame := extractfilepath(ParamStr(0)) + 'server.ini';
filename := extractfilepath(ParamStr(0));
ini := TIniFile.Create(ininame);
try
lokaler_pfad := ini.ReadString('DATABASE','Pfad',extractfilepath(ParamStr(0))+'files\database\DATABASE.FDB');
hostname := ini.ReadString('DATABASE','Host','
localhost');
datenpfad := ini.ReadString('FILES', 'Pfad',extractfilepath(ParamStr(0)));
installationspfad := ini.ReadString('PFAD', 'Pfad','');
domain_name := ini.ReadString('DATABASE','Domain','');
finally
ini.Free;
end;
with TEventLogger.Create('Mailservice') do
begin
try
ZConnection1.Disconnect;
ZConnection1.HostName:=hostname;
ZConnection1.Port:=3050;
ZConnection1.Database:=lokaler_pfad;
ZConnection1.User:='SYSDBA';
ZConnection1.Password:='masterkey';
ZConnection1.Connect;
LogMessage('Connection.Connect ...', EVENTLOG_INFORMATION_TYPE);
LogMessage('Connection.Database ...' + lokaler_pfad, EVENTLOG_INFORMATION_TYPE);
finally
Free;
end;
end;
with TEventLogger.Create('Mailservice') do
begin
try
LogMessage('Loop starts ...', EVENTLOG_INFORMATION_TYPE);
finally
Free;
end;
end;
with TEventLogger.Create('Mailservice') do
begin
try
MAILKONTEN.SQL.Clear;
MAILKONTEN.SQL.Append('SELECT * FROM MAILKONTEN');
MAILKONTEN.Open;
LogMessage('MAILKONTEN Open...', EVENTLOG_INFORMATION_TYPE);
finally
Free;
end;
end;
while not MAILKONTEN.Eof do
begin
POP.Disconnect;
POP.Host := MAILKONTEN.FieldByName('
POP3').AsString;
POP.Port := strtoint(MAILKONTEN.FieldByName('PORT_POP3').AsString);
POP.Username := MAILKONTEN.FieldByName('BENUTZERNAME').AsString;
POP.Password := MAILKONTEN.FieldByName('KENNWORT').AsString;
POP.Connect;
MailCount := POP.CheckMessages;
LogMessage('MAILKONTO "'+POP.Username+'" hat '+inttostr(MailCount)+' Mails', EVENTLOG_INFORMATION_TYPE);
if MailCount > 0 then
begin
for intIndex := 1 to MailCount do
begin
msg.Clear;
pop.Retrieve(intIndex, Msg);
MAILDATENBANK.SQL.Clear;
MAILDATENBANK.SQL.Append('SELECT MESSAGID FROM MAILDATENBANK WHERE MESSAGID = '+#39+msg.MsgId+#39+'');
MAILDATENBANK.Open;
// Die Mail habe ich noch nicht !!!!!!!!!
if MAILDATENBANK.RecordCount = 0 then
begin
Z_NUMMER.SQL.Clear;
Z_NUMMER.SQL.add('SELECT GEN_ID( POOL, 1 ) AS IDR FROM RDB$DATABASE');
Z_NUMMER.open;
neue_nummer:=inttostr(Z_NUMMER.FieldByName('IDR').AsInteger);
MAILDATENBANK.SQL.Clear;
MAILDATENBANK.SQL.Append('SELECT * FROM MAILDATENBANK WHERE INR = -1');
MAILDATENBANK.Open;
MAILKONTEN.SQL.Clear;
MAILKONTEN.SQL.Append('SELECT * FROM MAILKONTEN WHERE INR = -1');
MAILKONTEN.Open;
MAILDATENBANK.Append;
//MAILDATENBANK.FieldByName('USER').AsInteger:=strtoint(main.MainForm.benutzernummer);
MAILDATENBANK.FieldByName('INR').AsString:=neue_nummer;
MAILDATENBANK.FieldByName('VON').AsString:=Msg.From.Text;
MAILDATENBANK.FieldByName('ANTWORTADRESSE').AsString:=msg.from.Address;
MAILDATENBANK.FieldByName('BETREFF').AsString:=Msg.Subject;
MAILDATENBANK.FieldByName('DATUM').AsDateTime:=Msg.Date;
MAILDATENBANK.FieldByName('MIMETYPE').AsString:=msg.AttachmentEncoding;
MAILDATENBANK.FieldByName('CONTENTTYPE').AsString:=msg.ContentType;
MAILDATENBANK.FieldByName('MESSAGID').AsString:=msg.MsgId;
MAILDATENBANK.FieldByName('KONTONAME').AsString:=MAILKONTEN.FieldByName('KONTONAME').AsString;
MAILDATENBANK.FieldByName('KONTONUMMER').AsString:=MAILKONTEN.FieldByName('INR').AsString;
if Msg.MessageParts.Count = 0 then MAILDATENBANK.FieldByName('ANHANG').AsString:='';
if Msg.MessageParts.Count > 0 then MAILDATENBANK.FieldByName('ANHANG').AsString:='Anhang';
MAILDATENBANK.FieldByName('GELESEN').AsInteger:=0;
MAILDATENBANK.FieldByName('PR').AsInteger:=0;
MAILDATENBANK.FieldByName('ZUORDNUNG').AsString:='';
MAILDATENBANK.Post;
//Anhang
for i := 0 to Msg.MessageParts.Count-1 do begin
if Msg.MessageParts.Items[i] is tIdAttachment then
begin
s := (Msg.MessageParts.Items[i] as tIdAttachment).Filename;
// Extension ändern
extensio:= ExtractFileExt(s) ;
(Msg.MessageParts.Items[i] as tIdAttachment).savetofile(filename +'files\Mails\Anhang_' + neue_nummer +extensio);
end;
end;
//
HTML Mail
if msg.MessageParts.Count > 0 then
begin
for i := 0 to Msg.MessageParts.Count-1 do begin
if Msg.MessageParts.Items[i] is TIdText then
begin
//Logger.AddLogStrings(TIdText(Msg.MessageParts.Items[i]).Body);
TIdText(Msg.MessageParts.Items[i]).Body.SaveToFile(filename +'files\Mails\html_' + neue_nummer + '.html');
end;
end;
end;
// Text Mail
if msg.ContentType = 'text/
html' then
begin
//Logger.AddLogStrings(msg.body);
Msg.Body.SaveToFile(filename +'files\Mails\html_' + neue_nummer + '.html');
end;
// Text Mail
if msg.ContentType = 'text/plain' then
begin
//Logger.AddLogStrings(msg.body);
Msg.Body.SaveToFile(filename +'files\Mails\text_' + neue_nummer + '.txt');
end;
end; // Die Mail habe ich noch nicht
///////
//Kopie am Server belassen: also NICHT löschen
//pop.Delete(intIndex); // Löscht die aktuelle Mail !!!!
end;
end;
// POP.Disconnect;
MAILKONTEN.Next;
end;
with TEventLogger.Create('Mailservice') do
begin
try
LogMessage('ServiceStop...', EVENTLOG_INFORMATION_TYPE);
finally
Free;
end;
end;
ZConnection1.Disconnect;
end; // procedure
end.