unit Unit1;
interface
uses
Winapi.Windows,
Winapi.Messages, System.SysUtils,StrUtils , 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, StdCtrls;
type
TMountainWebMailer =
class(TService)
pop: TIdPOP3;
msg: TIdMessage;
IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
MAILDATENBANK: TZQuery;
MAILKONTEN: TZQuery;
Z_NUMMER: TZQuery;
ZConnection1: TZConnection;
Timer1: TTimer;
MAILANHANG: TZQuery;
TEMP: TZQuery;
procedure Timer1Timer(Sender: TObject);
private
{ Private-Deklarationen }
ini: TIniFile;
filename:
String;
meinMemo:TStrings;
meinMemoFertig:TStrings;
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;
neue_nummer_integer:Integer;
ininame:
String;
lokaler_pfad,
hostname,
datenpfad,
installationspfad,
domain_name :
String;
anzahl_anhang:Integer;
erledigt:Integer;
bereinigter_text:
String;
x:Integer;
user_inr:Integer;
stepper,schleife,zaehler1:Integer;
KOPIE_BELASSEN:
String;
BlobStream: TMemoryStream;
NUR_SENDEN:Integer;
KEIN_ANHANG:Integer;
NUR_WENN_ADRESSE:Integer;
relevant,gefunden:Integer;
begin
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;
ZConnection1.Disconnect;
ZConnection1.HostName:=hostname;
ZConnection1.Port:=3050;
ZConnection1.Database:=lokaler_pfad;
ZConnection1.User:='
****';
ZConnection1.Password:='
***';
ZConnection1.Connect;
MAILKONTEN.SQL.Clear;
MAILKONTEN.SQL.Append('
SELECT * FROM MAILKONTEN');
MAILKONTEN.Open;
MAILANHANG.SQL.Clear;
MAILANHANG.SQL.Append('
SELECT * FROM MAILANHANG WHERE INR = -1');
MAILANHANG.Open;
while not MAILKONTEN.Eof
do
begin
NUR_SENDEN :=MAILKONTEN.FieldByName('
NUR_SENDEN').AsInteger;
KEIN_ANHANG :=MAILKONTEN.FieldByName('
KEIN_ANHANG').AsInteger;
NUR_WENN_ADRESSE :=MAILKONTEN.FieldByName('
NUR_WENN_ADRESSE').AsInteger;
if NUR_SENDEN = 0
then
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;
USER_INR :=MAILKONTEN.FieldByName('
USER_INR').AsInteger;
KOPIE_BELASSEN:=MAILKONTEN.FieldByName('
KOPIE_BELASSEN').AsString;
MailCount := POP.CheckMessages;
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
relevant:=0;
if NUR_WENN_ADRESSE = 1
then
begin
relevant:=1;
gefunden:=0;
// suche adresse
TEMP.SQL.Clear;
TEMP.SQL.Append('
SELECT EMAIL FROM V_KONTAKT WHERE (EMAIL = '+#39+Msg.From.Text+#39+'
) OR (EMAIL = '+#39+msg.from.Address+#39+'
)');
TEMP.open;
if TEMP.RecordCount <> 0
then gefunden:=1;
TEMP.SQL.Clear;
TEMP.SQL.Append('
SELECT EMAIL FROM V_PERSON WHERE (EMAIL = '+#39+Msg.From.Text+#39+'
) OR (EMAIL = '+#39+msg.from.Address+#39+'
)');
TEMP.open;
if TEMP.RecordCount <> 0
then gefunden:=1;
TEMP.SQL.Clear;
TEMP.SQL.Append('
SELECT EMAIL FROM V_PERSON WHERE (EMAIL = '+#39+Msg.From.Text+#39+'
) OR (EMAIL = '+#39+msg.from.Address+#39+'
)');
TEMP.open;
if TEMP.RecordCount <> 0
then gefunden:=1;
TEMP.SQL.Clear;
TEMP.SQL.Append('
SELECT EMAIL FROM V_MANDANT WHERE (EMAIL = '+#39+Msg.From.Text+#39+'
) OR (EMAIL = '+#39+msg.from.Address+#39+'
)');
TEMP.open;
if TEMP.RecordCount <> 0
then gefunden:=1;
if gefunden = 1
then relevant:=2;
end;
if (relevant = 0)
OR (relevant = 2)
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);
neue_nummer_integer:=strtoint(neue_nummer);
{
// ---------------------------- STREAM Speichern
try
BlobStream:= TMemoryStream.Create;
try
Msg.SaveToStream(BlobStream);
BlobStream.Position := 0;
MAILDATENBANK.Close;
MAILDATENBANK.SQL.Text := 'insert into MAILDATENBANK (INR, USER_INR, DATEN) values (:inr, :user_inr, :daten)';
MAILDATENBANK.ParamByName('inr').AsString := neue_nummer;
MAILDATENBANK.ParamByName('user_inr').AsInteger := USER_INR;
MAILDATENBANK.ParamByName('daten').LoadFromStream(BlobStream, ftBlob);
MAILDATENBANK.ExecSQL;
BlobStream.Free;
except
// Handle exceptions
Raise;
end;
except
BlobStream.Free;
end;
// ---------------------------- STREAM Speichern ENDE
}
MAILDATENBANK.SQL.Clear;
MAILDATENBANK.SQL.Append('
SELECT * FROM MAILDATENBANK WHERE INR = '+neue_nummer);
MAILDATENBANK.Open;
MAILDATENBANK.APPEND;
MAILDATENBANK.FieldByName('
ZUGRIFFSRECHT').AsString:= MAILKONTEN.FieldByName('
ZUGRIFFSRECHT').AsString;
MAILDATENBANK.FieldByName('
INR').AsString:=neue_nummer;
MAILDATENBANK.FieldByName('
USER_INR').AsInteger:=USER_INR;
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('
S_DATUM').AsString:= DateToStr(msg.Date);
MAILDATENBANK.FieldByName('
S_UHRZEIT').AsString:=TimeToStr(msg.Date);
MAILDATENBANK.FieldByName('
ZEITSTEMPEL').AsExtended:=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('
GELOESCHT').AsInteger:=0;
MAILDATENBANK.FieldByName('
IN_OUT').AsString:='
IN';
MAILDATENBANK.FieldByName('
PR').AsInteger:=0;
MAILDATENBANK.FieldByName('
ZUORDNUNG').AsString:=MAILKONTEN.FieldByName('
ZUGRIFFSRECHT').AsString;
MAILDATENBANK.Post;
anzahl_anhang:=0;
stepper:=0; zaehler1:=0;
for schleife := 1
to neue_nummer_integer
do
begin
stepper:=stepper+1;
if stepper > 99
then
begin
stepper:=0;
zaehler1:=zaehler1+100;
end;
end;
ForceDirectories(filename +'
files\Mails\'+inttostr(zaehler1)+'
\'+inttostr(neue_nummer_integer));
{if AnsiContainsStr(Msg.ContentType, 'text/html') then
begin
Msg.Body.SaveToFile(filename +'files\Mails\'+inttostr(zaehler1)+'\'+inttostr(neue_nummer_integer)+'\html_' + neue_nummer + '.html',TEncoding.UTF8);
end;
if AnsiContainsStr(Msg.ContentType, 'text/plain') then
begin
Msg.Body.SaveToFile(filename +'files\Mails\'+inttostr(zaehler1)+'\'+inttostr(neue_nummer_integer)+'\html_' + neue_nummer + '.html',TEncoding.UTF8);
end;
if AnsiContainsStr(Msg.ContentType, 'multipart/alternative') then
begin
Msg.Body.SaveToFile(filename +'files\Mails\'+inttostr(zaehler1)+'\'+inttostr(neue_nummer_integer)+'\html_' + neue_nummer + '.html',TEncoding.UTF8);
end;
if AnsiContainsStr(Msg.ContentType, 'multipart/mixed') then
begin
Msg.Body.SaveToFile(filename +'files\Mails\'+inttostr(zaehler1)+'\'+inttostr(neue_nummer_integer)+'\html_' + neue_nummer + '.html',TEncoding.UTF8);
end;
if AnsiContainsStr(Msg.ContentType, 'multipart/related') then
begin
Msg.Body.SaveToFile(filename +'files\Mails\'+inttostr(zaehler1)+'\'+inttostr(neue_nummer_integer)+'\html_' + neue_nummer + '.html',TEncoding.UTF8);
end;
if AnsiContainsStr(Msg.ContentType, 'application/pdf') then
begin
Msg.Body.SaveToFile(filename +'files\Mails\'+inttostr(zaehler1)+'\'+inttostr(neue_nummer_integer)+'\html_' + neue_nummer + '.html',TEncoding.UTF8);
end; }
//Mailteile
for i := 0
to Msg.MessageParts.Count-1
do
begin
if Msg.MessageParts.Items[i]
is tIdAttachment
then
begin
if KEIN_ANHANG = 0
then
begin
anzahl_anhang:=anzahl_anhang+1;
s := (Msg.MessageParts.Items[i]
as tIdAttachment).Filename;
extensio:= ExtractFileExt(s) ;
(Msg.MessageParts.Items[i]
as tIdAttachment).savetofile(filename +'
files\Mails\'+inttostr(zaehler1)+'
\'+inttostr(neue_nummer_integer)+'
\Anhang_' + neue_nummer +'
_'+inttostr(anzahl_anhang)+extensio);
{ CID --> }(Msg.MessageParts.Items[i]
as tIdAttachment).savetofile(filename +'
files\Mails\'+inttostr(zaehler1)+'
\'+inttostr(neue_nummer_integer)+'
\'+(Msg.MessageParts.Items[i]
as tIdAttachment).FileName);
MAILDATENBANK.Edit;
MAILDATENBANK.FieldByName('
ANHANG_ANZAHL').AsInteger:=anzahl_anhang;
MAILDATENBANK.Post;
Z_NUMMER.SQL.Clear;
Z_NUMMER.SQL.add('
SELECT GEN_ID( POOL, 1 ) AS IDR FROM RDB$DATABASE');
Z_NUMMER.open;
MAILANHANG.Append;
MAILANHANG.FieldByName('
INR').AsInteger:=Z_NUMMER.FieldByName('
IDR').AsInteger;
MAILANHANG.FieldByName('
PR').AsString:=neue_nummer;
MAILANHANG.FieldByName('
ORIGINALNAME').AsString:= (Msg.MessageParts.Items[i]
as tIdAttachment).FileName;
MAILANHANG.FieldByName('
FILE').AsString:= '
Anhang_' + neue_nummer +'
_'+inttostr(anzahl_anhang)+extensio;
MAILANHANG.Post;
end;
// if KEIN_ANHANG = 0 then
end
else
begin
//if Msg.MessageParts.Items[i] is TIdText then
if AnsiContainsStr(Msg.MessageParts.Items[i].ContentType, '
text/html')
then
TIdText(Msg.MessageParts.Items[i]).Body.SaveToFile(filename +'
files\Mails\'+inttostr(zaehler1)+'
\'+inttostr(neue_nummer_integer)+'
\html_' + neue_nummer + '
.html',TEncoding.UTF8);
if AnsiContainsStr(Msg.MessageParts.Items[i].ContentType, '
multipart/alternative')
then
TIdText(Msg.MessageParts.Items[i]).Body.SaveToFile(filename +'
files\Mails\'+inttostr(zaehler1)+'
\'+inttostr(neue_nummer_integer)+'
\html_' + neue_nummer + '
.html',TEncoding.UTF8);
if AnsiContainsStr(Msg.MessageParts.Items[i].ContentType, '
text/plain')
then
TIdText(Msg.MessageParts.Items[i]).Body.SaveToFile(filename +'
files\Mails\'+inttostr(zaehler1)+'
\'+inttostr(neue_nummer_integer)+'
\html_' + neue_nummer + '
.html',TEncoding.UTF8);
if AnsiContainsStr(Msg.MessageParts.Items[i].ContentType, '
multipart/mixed')
then
TIdText(Msg.MessageParts.Items[i]).Body.SaveToFile(filename +'
files\Mails\'+inttostr(zaehler1)+'
\'+inttostr(neue_nummer_integer)+'
\html_' + neue_nummer + '
.html',TEncoding.UTF8);
if AnsiContainsStr(Msg.MessageParts.Items[i].ContentType, '
multipart/related')
then
TIdText(Msg.MessageParts.Items[i]).Body.SaveToFile(filename +'
files\Mails\'+inttostr(zaehler1)+'
\'+inttostr(neue_nummer_integer)+'
\html_' + neue_nummer + '
.html',TEncoding.UTF8);
if AnsiContainsStr(Msg.MessageParts.Items[i].ContentType, '
application/pdf')
then
TIdText(Msg.MessageParts.Items[i]).Body.SaveToFile(filename +'
files\Mails\'+inttostr(zaehler1)+'
\'+inttostr(neue_nummer_integer)+'
\html_' + neue_nummer + '
.html',TEncoding.UTF8);
end;
end;
// for i := 0 to Msg.MessageParts.Count-1 do
if msg.MessageParts.Count = 0
then // keine Message Parts - Also eine reine Textmail
begin
Msg.Body.SaveToFile(filename +'
files\Mails\'+inttostr(zaehler1)+'
\'+inttostr(neue_nummer_integer)+'
\html_' + neue_nummer + '
.html',TEncoding.UTF8);
end;
end;
// Die Mail habe ich noch nicht
end;
// "NUR_WENN_ADRESSE"
///////
//Kopie am Server belassen: also NICHT löschen
//pop.Delete(intIndex); // Löscht die aktuelle Mail !!!!
if KOPIE_BELASSEN='
Nein'
then pop.Delete(intIndex);
end;
end;
// POP.Disconnect;
end;
//if NUR_SENDEN = 0 then
MAILKONTEN.Next;
end;
ZConnection1.Disconnect;
// timer1.Enabled:=true; <-- NICHT am anfange disablen .. da bei einem Fehler der timer nie wieder gestartet wird!
end;
// procedure
end.