unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, ExtCtrls, Menus, NMsmtp, Psock, NMpop3, ADODB, Db, StdCtrls,unit2,
AppEvnts;
type
TeMail = class(TForm)
sbSys: TStatusBar;
MainMenu1: TMainMenu;
File1: TMenuItem;
Exit1: TMenuItem;
Operate1: TMenuItem;
StartSmtpServer1: TMenuItem;
StopSmtpServer1: TMenuItem;
StartPop3Server1: TMenuItem;
StopPop3Server1: TMenuItem;
N1: TMenuItem;
PC1: TPageControl;
tsSMTP: TTabSheet;
tsPOP: TTabSheet;
adoEmail: TADOConnection;
atReceive: TADOTable;
aqReceive: TADOQuery;
aqSend: TADOQuery;
tmServer: TTimer;
tmWork: TTimer;
popReceive: TNMPOP3;
popCheck: TNMPOP3;
smtpSend: TNMSMTP;
sbSMTP: TStatusBar;
sbPOP: TStatusBar;
btnSMTP: TButton;
mmHistory: TMemo;
Label1: TLabel;
btnPOP: TButton;
lServer: TLabel;
lProgress: TLabel;
pbReceive: TProgressBar;
mmList: TMemo;
Label4: TLabel;
ApplicationEvents1: TApplicationEvents;
mmInfo: TMemo;
Label5: TLabel;
atHistory: TADOTable;
lSendMailNum: TLabel;
lCheck: TLabel;
N2: TMenuItem;
SetPOP3Server1: TMenuItem;
Help1: TMenuItem;
Abort1: TMenuItem;
lReceiveNum: TLabel;
lSendNum: TLabel;
procedure Exit1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Operate1Click(Sender: TObject);
procedure btnSMTPClick(Sender: TObject);
procedure StartSmtpServer1Click(Sender: TObject);
procedure StopSmtpServer1Click(Sender: TObject);
procedure btnPOPClick(Sender: TObject);
procedure StartPop3Server1Click(Sender: TObject);
procedure StopPop3Server1Click(Sender: TObject);
function GetServersInfo:Boolean;
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure tmServerTimer(Sender: TObject);
function CheckSendMail:Boolean;
procedure FormDestroy(Sender: TObject);
procedure ApplicationEvents1Exception(Sender: TObject; E: Exception);
procedure tmWorkTimer(Sender: TObject);
procedure popCheckConnect(Sender: TObject);
procedure popCheckDisconnect(Sender: TObject);
procedure popCheckStatus(Sender: TComponent; Status: String);
procedure popCheckSuccess(Sender: TObject);
procedure popCheckConnectionFailed(Sender: TObject);
procedure popCheckAuthenticationFailed(var Handled: Boolean);
procedure popCheckAuthenticationNeeded(var Handled: Boolean);
procedure popCheckConnectionRequired(var Handled: Boolean);
procedure popCheckDecodeEnd(Sender: TObject);
procedure popCheckDecodeStart(var FileName: String);
procedure popCheckFailure(Sender: TObject);
procedure popCheckHostResolved(Sender: TComponent);
procedure popCheckInvalidHost(var Handled: Boolean);
procedure popCheckList(Msg, Size: Integer);
procedure popCheckPacketRecvd(Sender: TObject);
procedure popCheckReset(Sender: TObject);
procedure popCheckRetrieveEnd(Sender: TObject);
procedure popCheckRetrieveStart(Sender: TObject);
procedure popReceiveConnect(Sender: TObject);
procedure popReceiveRetrieveEnd(Sender: TObject);
procedure popReceiveList(Msg, Size: Integer);
procedure popReceiveAuthenticationFailed(var Handled: Boolean);
procedure popReceiveAuthenticationNeeded(var Handled: Boolean);
procedure popReceiveConnectionFailed(Sender: TObject);
procedure popReceiveConnectionRequired(var Handled: Boolean);
procedure popReceiveDecodeEnd(Sender: TObject);
procedure popReceiveDecodeStart(var FileName: String);
procedure popReceiveDisconnect(Sender: TObject);
procedure popReceiveFailure(Sender: TObject);
procedure popReceiveHostResolved(Sender: TComponent);
procedure popReceiveInvalidHost(var Handled: Boolean);
procedure popReceivePacketRecvd(Sender: TObject);
procedure popReceiveReset(Sender: TObject);
procedure popReceiveRetrieveStart(Sender: TObject);
procedure popReceiveStatus(Sender: TComponent; Status: String);
procedure popReceiveSuccess(Sender: TObject);
procedure smtpSendAttachmentNotFound(Filename: String);
procedure smtpSendAuthenticationFailed(var Handled: Boolean);
procedure smtpSendConnect(Sender: TObject);
procedure smtpSendConnectionFailed(Sender: TObject);
procedure smtpSendConnectionRequired(var Handled: Boolean);
procedure smtpSendDisconnect(Sender: TObject);
procedure smtpSendEncodeEnd(Filename: String);
procedure smtpSendEncodeStart(Filename: String);
procedure smtpSendFailure(Sender: TObject);
procedure smtpSendHeaderIncomplete(var handled: Boolean;
hiType: Integer);
procedure smtpSendHostResolved(Sender: TComponent);
procedure smtpSendInvalidHost(var Handled: Boolean);
procedure smtpSendMailListReturn(MailAddress: String);
procedure smtpSendPacketSent(Sender: TObject);
procedure smtpSendRecipientNotFound(Recipient: String);
procedure smtpSendSendStart(Sender: TObject);
procedure smtpSendStatus(Sender: TComponent; Status: String);
procedure smtpSendSuccess(Sender: TObject);
procedure SetPOP3Server1Click(Sender: TObject);
procedure Abort1Click(Sender: TObject);
procedure mmHistoryKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
totSendNum,totreceiveNum:integer;
toStopPOP,toStopSMTP:Boolean;
CurReceiveCount:Integer;
POPServerReady:Boolean;
mvCheckMailNum:Integer;
DoCheckMail:boolean;
CurSystemStatus:ESysStatus;
EmailBoxMum:Integer;
HaveEmailBox:Boolean;
ReceiveMailDone:Boolean;
TotalReceiveMailNum:Integer;
CurReceiveMailNum:Integer;
ProcessCurMailDone:Boolean;
mHaveException:Boolean;
mTryCount:Integer;
PendStopSmtp:Boolean;
PendStopPop:Boolean;
RunSMTP:Boolean;
RunPOP:Boolean;
mPOPNum:Integer;
SomeThingToDo:Boolean;
TimerForWhich:ETask;
mPOPServer:array [0..9] of RPOPInfo;
mSMTPServer:RSMTPInfo;
mCSMTPServer:RSMTPInfo;
CurTryCount,CurTryNum:Integer;
DoneCurSendMail,CurSendMailSuccess:Boolean;
mLogFile:TLogFile;
SMTPConnect:Boolean;
POPCConnect:Boolean;
POPRConnect:Boolean;
{ Private declarations }
public
function CanStopSMTP:Boolean;
function CanStopPOP:Boolean;
function SendMail:boolean;
function CheckMail(var MailNumber:Integer):Boolean;
function ReceiveMail:Boolean;
function CheckMailOne(BoxNo:Integer;var HaveMail:Boolean):Boolean;
function fStartSMTP:Boolean;
function fStopSMTP:Boolean;
function fStartPOP:Boolean;
function fStopPOP:Boolean;
function HaveEmailToReceive:Boolean;
function sRE(ErrLevel:Integer;Sender:TObject;E:Exception;aStr:string=''):Boolean;overload;
function sRE(ErrLevel:Integer;E:Exception;aStr:string=''):Boolean;overload;
function sRE(ErrLevel:Integer;E:string):Boolean;overload;
function SendMailOne(MailID:Integer):Boolean;
procedure ClearSMTPServer;
procedure ClearPOPServer;
{ Public declarations }
end;
var
eMail: TeMail;
implementation
{$R *.DFM}
uses unit3;
procedure TeMail.ClearSMTPServer;
begin
lSendMailNum.Caption:=' ';
mmHistory.Clear;
sbSMTP.SimpleText:=' ';
btnSMTP.Enabled:=True;
end;
procedure TeMail.ClearPOPServer;
begin
lCheck.Caption:=' ';
lServer.Caption:=' ';
lProgress.Caption:=' ';
pbReceive.Max:=1;
pbReceive.Position:=0;
mmList.Clear;
mmInfo.Clear;
sbPOP.SimpleText:=' ';
btnPOP.Enabled:=True;
end;
function TeMail.sRE(ErrLevel:Integer;Sender:TObject;E:Exception;aStr:string=''):Boolean;
begin
sRE:=true;
sbSys.SimpleText:=TimeToStr(Time)+' -> '+'Exception('+ Sender.ClassName+') : '+E.Message+' ; '+aStr;
if InDebug then
mLogFile.WriteLog(ErrLevel,'Exception('+ Sender.ClassName+') : '+E.Message+' ; '+aStr);
end;
function TeMail.sRE(ErrLevel:Integer;E:Exception;aStr:string=''):Boolean;
begin
sRE:=true;
sbSys.SimpleText:=TimeToStr(Time)+' -> '+'Exception('+ E.ClassName+') : '+E.Message+' ; '+aStr;
if InDebug then
mLogFile.WriteLog(ErrLevel,'Exception('+ E.ClassName+') : '+E.Message+' ; '+aStr);
end;
function TeMail.sRE(ErrLevel:Integer;E:string):Boolean;
begin
sRE:=True;
sbSys.SimpleText:=TimeToStr(Time)+' -> '+E;
if InDebug then
mLogFile.WriteLog(ErrLevel,E);
end;
function TeMail.HaveEmailToReceive:Boolean;
begin
HaveEmailToReceive:=HaveEmailBox;
end;
function TeMail.fStartSMTP:Boolean;
begin
fStartSMTP:=False;
if not RunSMTP then
begin
btnSMTP.Caption :='Stop SMTP Server';
sbSys.SimpleText:='Start SMTP Server at ' + TimeToStr(Time);
if not tmServer.Enabled then
tmServer.Enabled:=True;
RunSMTP:=True;
if CurSystemStatus=sNotReady then
CurSystemStatus:=sIdle;
ClearSMTPServer;
fStartSMTP:=True;
totSendNum:=0;
end;
PendStopSmtp:=False;
toStopSMTP:=False;
end;
function TeMail.fStopSMTP:Boolean;
begin
fstopSMTP:=False;
if RunSMTP then
if CanStopSMTP and toStopSMTP then
begin
btnSMTP.Caption :='Start SMTP Server';
sbSys.SimpleText:='Stop SMTP Server at ' + TimeToStr(Time);
RunSmtp:=False;
if (not (RunSmtp or RunPop)) and (tmServer.Enabled) then
tmServer.Enabled:=False;
PendStopSmtp:=False;
toStopSMTP:=False;
ClearSMTPServer;
fStopSMTP:=True;
if (not RunPOP) and (not RunSMTP) then
TimerForWhich:=tNotReady;
totSendNum:=0;
lSendNum.Visible:=False;
end
else if not CanStopSMTP then
begin
btnSMTP.Enabled:=False;
PendStopSmtp:=True;
toStopSMTP:=True;
end
else
begin
btnSMTP.Enabled:=False;
toStopSmtp:=True;
end;
end;
function TeMail.fStartPOP:Boolean;
begin
fStartPOP:=False;
if not RunPOP then
begin
btnPOP.Caption:='Stop POP3 Server';
sbSys.SimpleText:='Start POP3 Server at ' + TimeToStr(Time);
if not tmServer.Enabled then
tmServer.Enabled:=True;
RunPOP:=True;
if CurSystemStatus=sNotReady then
CurSystemStatus:=sIdle;
ClearPOPServer;
fStartPOP:=True;
if TimerForWhich=tNotReady then
TimerForWhich:=tIdle;
totReceiveNum:=0;
end;
PendStopPop:=False;
toStopPOP:=False;
end;
function TeMail.fStopPOP:Boolean;
begin
fStopPOP:=False;
if RunPOP then
if CanStopPOP and toStopPOP then
begin
btnPOP.Caption:='Start POP3 Server';
sbSys.SimpleText:='Stop POP3 Server at ' + TimeToStr(Time);
RunPOP:=False;
if (not (RunSmtp or RunPop)) and (tmServer.Enabled) then
tmServer.Enabled:=False;
ClearPOPServer;
fStopPOP:=True;
PendStopPop:=False;
toStopPOP:=False;
if (not RunPOP) and (not RunSMTP) then
TimerForWhich:=tNotReady;
totReceiveNum:=0;
lReceiveNum.Visible:=False;
end
else if not CanStopPOP then
begin
btnPOP.Enabled:=False;
PendStopPop:=True;
toStopPOP:=True;
end
else
begin
btnPOP.Enabled:=False;
toStopPop:=True;
end;
end;
function TeMail.CheckMailOne(BoxNo:Integer;var HaveMail:Boolean):Boolean;
begin
CheckMailOne:=False;
try
lCheck.Caption:='Check Mail in Server : '+mPOPServer[BoxNo].mServer;
try
if popCheck.Connected then
popCheck.Disconnect;
popCheck.Host:=mPOPServer[BoxNo].mServer;
popCheck.UserID:=mPOPServer[BoxNo].mUser;
popCheck.Password:=mPOPServer[BoxNo].mPassWord;
popCheck.AttachFilePath:=mPOPServer[BoxNo].mAttchPath;
popCheck.Port:=mPOPServer[BoxNo].mPort;
popCheck.DeleteOnRead:=False;
DoCheckMail:=True;
mvCheckMailNum:=-1;
popCheck.Connect;
while DoCheckMail do
begin
Application.ProcessMessages;
end;
except
on E:Exception do
sRE(gErr1,E,'In CheckMail:'+mPOPServer[BoxNo].mServer);
end;
if (mvCheckMailNum>-1) or (not doCheckmail) then
begin
if mvCheckMailNum>0 then
HaveMail:=True
else
HaveMail:=False;
CheckMailOne:=True;
end;
finally
if popCheck.Connected then
popCheck.Disconnect;
lCheck.Caption:=' ';
end;
end;
function TeMail.ReceiveMail:Boolean;
var
i:Integer;
mOldSystemStatus:ESysStatus;
begin
mOldSystemStatus:=CurSystemStatus;
CurSystemStatus:=sReceiveMail;
ReceiveMail:=False;
try
if HaveEmailBox then
begin
i:=EmailBoxMum;
lServer.Caption:='Server : '+mPOPServer
.mServer;
pbReceive.Max:=1;
pbReceive.Position:=0;
try
if popReceive.Connected then
popReceive.Disconnect;
popReceive.Host:=mPOPServer.mServer;
popReceive.UserID:=mPOPServer.mUser;
popReceive.Password:=mPOPServer.mPassWord;
popReceive.AttachFilePath:=mPOPServer.mAttchPath;
popReceive.Port:=mPOPServer.mPort;
popReceive.DeleteOnRead:=True;
ReceiveMailDone:=False;
popReceive.Connect;
while not ReceiveMailDone do
begin
Application.ProcessMessages;
end;
except
on E:Exception Do
sRE(gErr1,E,'Receive Mail :'+mPOPServer.mServer);
end;
ReceiveMail:=True;
end;
finally
mmList.Clear;
mmInfo.Clear;
lCheck.Caption:='';
lServer.Caption:='';
lProGress.Caption:='';
sbPOP.SimpleText:='';
sbSys.SimpleText:='';
pbReceive.Max:=1;
pbReceive.Position:=0;
HaveEmailBox:=False;
CurSystemStatus:=mOldSystemStatus;
end;
end;
function TeMail.SendMailOne(MailID:Integer):Boolean;
var
AttNum:integer;
AttPath,AttName:string;
tStrs:TStringList;
begin
SendMailOne:=False;
tStrs:=TStringlist.Create;
try
if not adoEmail.Connected then
adoEmail.Open;
if adoEmail.Connected then
begin
if aqSend.Active then
aqSend.Close;
aqSend.SQL.Clear;
aqSend.SQL.Add('SELECT * FROM tblSendMail WHERE ID='+IntToStr(MailID));
aqSend.Open;
CurTryCount:=aqSend.FieldByName('Counter').AsInteger;
if aqSend.RecordCount<>1 then
sRE(gErr1,'DataBase Err ! ')
else
begin
try
CurSendMailSuccess:=False;
CurTryNum:=0;
while (not CurSendMailSuccess) and (CurTryNum<4) do
begin
try
if smtpSend.Connected then
smtpSend.Disconnect;
while smtpSend.Connected do
begin
Application.ProcessMessages;
end;
try
smtpSend.ClearParameters;
smtpSend.PostMessage.Body.Clear;
mCSMTPServer.mServer:=aqSend.FieldByName('SMTPServerName').AsString;
mCSMTPServer.mUser:=aqSend.FieldByName('UserName').AsString;
mCSMTPServer.mPassword:=aqSend.FieldByName('Password').AsString;
mCSMTPServer.mMailBox:=aqSend.FieldByName('FromEmail').AsString;
mCSMTPServer.mPort:=25;
smtpSend.ClearParams:=True;
smtpSend.Host:=aqSend.FieldByName('SMTPServerName').AsString;
smtpSend.UserID:=aqSend.FieldByName('UserName').AsString;
smtpSend.Port:=25;
smtpSend.PostMessage.FromAddress:=aqSend.FieldByName('FromEmail').AsString;
smtpSend.PostMessage.FromName:=aqSend.FieldByName('UserName').AsString;
smtpSend.PostMessage.Subject:=aqSend.FieldByName('Subject').AsString;
smtpSend.PostMessage.Body.Text:=aqSend.FieldByName('MailBody').AsString;
StrToStrS(aqSend.FieldByName('ToEmail').AsString,';',tStrs);
smtpSend.PostMessage.ToAddress.Assign(tStrs);
StrToStrS(aqSend.FieldByName('CCEmail').AsString,';',tStrs);
smtpSend.PostMessage.ToCarbonCopy.Assign(tStrs);
StrToStrS(aqSend.FieldByName('BCCEmail').AsString,';',tStrs);
smtpSend.PostMessage.ToBlindCarbonCopy.Assign(tStrs);
AttPath:=aqSend.FieldByName('AttachBackupPath').AsString;
AttNum:=aqSend.FieldByName('AttachCounter').AsInteger;
AttName:=aqSend.FieldByName('AttachName').AsString;
if AttNum>0 then
begin
if not ExtAtt(AttName,AttPath,';',AttNum,tStrs) then
sRE(gErr1,'Attachment Number Err ! ')
else
smtpSend.PostMessage.Attachments.Assign(tStrs);
end
else
smtpSend.PostMessage.Attachments.Clear;
except
on E:Exception do
sRE(gErr1,E,'SendMail(Set Params):'+IntToStr(MailID));
end;
DoneCurSendMail:=False;
Inc(CurTryNum);
smtpSend.Connect;
while not DoneCurSendMail do
begin
Application.ProcessMessages;
end;
except
on E:Exception do
sRE(gErr1,E,'Sendmail(Connect And Send):'+IntToStr(MailID));
end;
end;
if (not CurSendMailSuccess) and (CurTryCount>-100) then
begin
if CurTryCount>99 then
CurTryCount:=99;
if CurTryCount>0 then
CurTryCount:=-101+CurTryCount
else
Dec(CurTryCount);
aqSend.Edit;
aqSend.FieldByName('Counter').AsInteger:=CurTryCount;
aqSend.Post;
end
else
begin
if atHistory.Active then
atHistory.Close;
atHistory.Active:=true;
try
try
atHistory.Append;
if CurSendMailSuccess then
begin
SendMailOne:=true;
atHistory.FieldByName('isOK').AsString:='1'
end
else
atHistory.FieldByName('isOK').AsString:='0';
atHistory.FieldByName('ID').AsInteger:=aqSend.FieldByName('ID').AsInteger;
atHistory.FieldByName('AttachCounter').AsInteger:=aqSend.FieldByName('AttachCounter').AsInteger;
atHistory.FieldByName('Counter').AsInteger:=aqSend.FieldByName('Counter').AsInteger;
atHistory.FieldByName('ToEmail').AsString:=aqSend.FieldByName('ToEmail').AsString;
atHistory.FieldByName('CCEmail').AsString:=aqSend.FieldByName('CCEmail').AsString;
atHistory.FieldByName('BCCEmail').AsString:=aqSend.FieldByName('BCCEmail').AsString;
atHistory.FieldByName('FromEmail').AsString:=aqSend.FieldByName('FromEmail').AsString;
atHistory.FieldByName('Subject').AsString:=aqSend.FieldByName('Subject').AsString;
atHistory.FieldByName('SMTPServerName').AsString:=aqSend.FieldByName('SMTPServerName').AsString;
atHistory.FieldByName('AttachBackupPath').AsString:=aqSend.FieldByName('AttachBackupPath').AsString;
atHistory.FieldByName('AttachName').AsString:=aqSend.FieldByName('AttachName').AsString;
atHistory.FieldByName('UserName').AsString:=aqSend.FieldByName('UserName').AsString;
atHistory.FieldByName('Password').AsString:=aqSend.FieldByName('Password').AsString;
atHistory.FieldByName('MailBody').AsString:=aqSend.FieldByName('MailBody').AsString;
atHistory.FieldByName('DateTime').AsString:=aqSend.FieldByName('DateTime').AsString;
atHistory.FieldByName('Priority').AsString:=aqSend.FieldByName('Priority').AsString;
atHistory.Post;
except
on E:Exception do
sRE(gErr1,E,'In Append History Table');
end;
finally
atHistory.Active:=False;
end;
if mmHistory.Lines.Count>1000 then
mmHistory.Clear;
mmHistory.Lines.Add(DateToStr(Date)+' -> '+TimeToStr(Time)+' # Send mail : '+ aqSend.FieldByName('ID').AsString);
mmHistory.Lines.Add(' From: '+aqSend.FieldByName('FromEmail').AsString);
mmHistory.Lines.Add(' To: '+aqSend.FieldByName('ToEmail').AsString);
mmHistory.Lines.Add(' Subject: '+aqSend.FieldByName('Subject').AsString);
mmHistory.Lines.Add(' Attachmemt Count: '+aqSend.FieldByName('AttachCounter').AsString);
if CurSendMailSuccess then
mmHistory.Lines.Add(' ^_^ OK ^_^ ')
else
mmHistory.Lines.Add(' ~_~ Err ~_~ ');
mmHistory.Lines.Add(' ');
try
aqSend.Close;
aqSend.SQL.Clear;
aqSend.SQL.Add('DELETE FROM tblSendMail Where ID='+IntToStr(MailID));
aqSend.ExecSQL;
finally
aqSend.Close;
end;
end;
except
on E:Exception do sRE(gErr1,E,'SendMailOne:'+InttoStr(MailID));
end;
end;
end
else
sRE(gErr1,'Database Err ! ');
finally
tStrs.Free;
end;
end;
function TeMail.SendMail:Boolean;
var
MailID:Integer;
MailCount:Integer;
SendMailOK:Boolean;
mOldSystemStatus:ESysStatus;
begin
mOldSystemStatus:=CurSystemStatus;
CurSystemStatus:=sSendMail;
SendMailOK:=False;
try
try
while not SendMailOK do
begin
adoEmail.Open;
if adoEmail.Connected then
begin
try
if aqSend.Active then
aqSend.Close;
aqSend.SQL.Clear;
aqSend.SQL.Add('SELECT count(*) FROM tblSendMail');
aqSend.Open;
MailCount:=aqSend.Fields[0].AsInteger;
aqSend.Close;
if MailCount<1 then
SendMailOK:=true
else
begin
lSendMailNum.Caption:=IntToStr(MailCount)+' E_Mail need to be sent';
aqSend.SQL.Clear;
aqSend.SQL.Add('SELECT ID FROM tblSendMail ORDER BY Priority DESC, Counter DESC');
aqSend.Open;
if not aqSend.Eof then
begin
MailID:=aqSend.FieldByName('ID').AsInteger;
if SendMailOne(MailID) then
begin
Inc(totSendNum);
lSendNum.Caption:=IntToStr(totSendNum)+' Mails sent';
if totSendNum>0 then
lSendNum.Visible:=true;
end;
end;
aqSend.Close;
end;
except
on E:Exception do sRE(gErr1,E,'In SendMail(Open And Send)');
end;
adoEmail.Close;
end
else
begin
sRE(gErr1,'Cannot connect SQL Server ! ');
SendMailOK:=True;
end;
end;
except
on E:Exception do sRE(gErr1,E,'In SendMail(Connect DataBase)');
end;
finally
lSendMailNum.Caption:='';
sbSMTP.SimpleText:='';
sbSys.SimpleText:='';
SendMail:=SendMailOK;
lSendMailNum.Caption:=' ';
CurSystemStatus:=mOldSystemStatus;
end;
end;
function TeMail.CheckMail(var MailNumber:Integer):Boolean;
var
i,j:Integer;
mHaveMail:Boolean;
CheckDone:Boolean;
mOldSystemStatus:ESysStatus;
begin
mOldSystemStatus:=CurSystemStatus;
CurSystemStatus:=sCheckMail;
CheckMail:=False;
CheckDone:=False;
MailNumber:=0;
try
for i:=0 to mPOPNum-1 do
begin
if not CheckDone then
begin
mHaveMail:=False;
try
if CheckMailOne(i,mHaveMail) then
begin
if mHaveMail then
begin
MailNumber:=i;
CheckMail:=true;
CheckDone:=True;
end;
end
else
begin
for j:=0 to gTryCheckNumber do
begin
mHaveMail:=False;
if CheckMailOne(i,mHaveMail) then
begin
if mHaveMail then
begin
MailNumber:=i;
CheckMail:=true;
CheckDone:=True;
end;
end;
end;
end;
except
on E:Exception do
sRE(gErr1,E,'CheckMail:'+IntToStr(i));
end;
end
else
Break;
end;
finally
lCheck.Caption:='';
sbPOP.SimpleText:='';
sbSys.SimpleText:='';
CurSystemStatus:=mOldSystemStatus;
end;
end;
function TeMail.CanStopSMTP:Boolean;
begin
CanStopSMTP:=not SMTPConnect;
end;
function TeMail.CanStopPOP:Boolean;
begin
CanStopPOP:=not (POPCConnect or POPRConnect);
end;
function TeMail.CheckSendMail:Boolean;
begin
CheckSendMail:=False;
try
adoEmail.Open;
if adoEmail.Connected then
begin
aqSend.SQL.Clear;
aqSend.SQL.Add('SELECT COUNT(*) FROM tblSendMail');
aqSend.Open;
if not aqSend.Eof then
if aqSend.Fields[0].AsInteger>0 then
CheckSendMail:=True;
aqSend.Close;
adoEmail.Close;
end
else
sRE(gErr1,'Cannot connect SQL Server ! ');
except
on E:Exception do sRE(gErr1,E,'In CheckSendMail(Connect And Count)');
end;
end;
function TeMail.GetServersInfo:Boolean;
var
i:Integer;
GetOK:Boolean;
POPOffice:TPOPOffice;
SMTPOffice:TSMTPOffice;
begin
POPOffice:=TPOPOffice.Create;
try
if POPOffice.mIsOK then
begin
mPOPNum:=POPOffice.mPOPONum;
for i:=0 to mPOPNum-1 do
begin
mPOPServer.mServer:=POPOffice.mPOPOffice.mServer;
mPOPServer.mUser:=POPOffice.mPOPOffice.mUser;
mPOPServer.mPassWord:=POPOffice.mPOPOffice.mPassWord;
mPOPServer.mAttchPath:=POPOffice.mPOPOffice.mAttchPath;
mPOPServer.mPort:=POPOffice.mPOPOffice.mPort;
end;
GetOK:=True;
end
else
GetOK:=false;
finally
POPOffice.Free;
end;
if GetOk then
begin
GetServersInfo:=True;
SMTPOffice:=TSMTPOffice.Create;
try
if SMTPOffice.mIsOK then
begin
mSMTPServer.mServer:=SMTPOffice.mSMTPOffice.mServer;
mSMTPServer.mMailBox:=SMTPOffice.mSMTPOffice.mMailBox;
mSMTPServer.mUser:=SMTPOffice.mSMTPOffice.mUser;
mSMTPServer.mPassword:=SMTPOffice.mSMTPOffice.mPassword;
mSMTPServer.mPort:=SMTPOffice.mSMTPOffice.mPort;
GetServersInfo:=True;
end
finally
SMTPOffice.Free;
end;
end
else
GetServersInfo:=False;
end;
procedure TeMail.Exit1Click(Sender: TObject);
begin
Close;
end;
procedure TeMail.FormCreate(Sender: TObject);
begin
toStopPOP:=False;
toStopSMTP:=False;
POPCConnect:=False;
POPRConnect:=False;
SMTPConnect:=False;
CurSystemStatus:=sNotReady;
RunSMTP:=False;
RUNPOP:=False;
mTryCount:=0;
TimerForWhich:=tIdle;
SomeThingToDo:=False;
HaveEmailBox:=False;
gAppPath:=ExtractFilePath(ParamStr(0));
TimerForWhich:=tIdle;
ClearSMTPServer;
ClearPOPServer;
if InDebug then
mLogFile:=TLogFile.Create;
if not GetServersInfo then
POPServerReady:=False
else
POPServerReady:=True;
{
if inDebug then
begin
smtpSend.CaptureFile(gAppPath+'smtpSend.Log');
popCheck.CaptureFile(gAppPath+'popCheck.Log');
popReceive.CaptureFile(gAppPath+'popReceive.Log');
end;
}
PC1.ActivePage:=tsSmtp;
end;
procedure TeMail.Operate1Click(Sender: TObject);
begin
StartSmtpServer1.Enabled := (not RunSMTP) and ((not PendStopSMTP) and (not toStopSMTP)) ;
StopSmtpServer1.Enabled :=(RunSMTP {and CanStopSMTP}) and ((not PendStopSMTP) and (not toStopSMTP));
StartPOP3Server1.Enabled :=POPServerReady and (not RunPOP) and ((not PendStopPOP) and (not toStopPOP));
StopPOP3Server1.Enabled :=POPServerReady and(RunPOP {and CanStopPOP}) and ((not PendStopPOP) and (not toStopPOP));
SetPOP3Server1.Enabled:=(not RunPOP);
end;
procedure TeMail.btnSMTPClick(Sender: TObject);
begin
if not fStartSMTP then
fStopSMTP;
end;
procedure TeMail.StartSmtpServer1Click(Sender: TObject);
begin
fStartSMTP;
end;
procedure TeMail.StopSmtpServer1Click(Sender: TObject);
begin
fStopSMTP;
end;
procedure TeMail.btnPOPClick(Sender: TObject);
begin
if POPServerReady then
begin
if not fStartPOP then
fStopPOP;
end;
end;
procedure TeMail.StartPop3Server1Click(Sender: TObject);
begin
if POPServerReady then
fStartPOP;
end;
procedure TeMail.StopPop3Server1Click(Sender: TObject);
begin
if POPServerReady then
fStopPOP;
end;
procedure TeMail.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
if not (RunSmtp or RunPOP) then
CanClose:=True
else
begin
CanClose:=False;
ShowMessage(' Please stop servers first ! ');
end;
end;
procedure TeMail.tmServerTimer(Sender: TObject);
begin
tmServer.Enabled:=False;
try
if mHaveException then
begin
try
if SMTPConnect then
smtpSend.Disconnect;
if POPCConnect then
popCheck.Disconnect;
if POPRConnect then
popReceive.Disconnect;
finally
SMTPConnect:=False;
POPCConnect:=False;
POPRConnect:=False;
CurSystemStatus:=sIdle;
mHaveException:=False;
end;
end;
if TimerForWhich=tIdle then
begin
if PendStopSmtp or toStopSMTP then
fStopSMTP;
if PendStopPop or toStopPOP then
fStopPop;
if RunSmtp or RunPOP then
begin
SomeThingToDo:=False;
try
if RunSmtp then
begin
if CheckSendMail then
begin
SomeThingToDo:=True;
TimerForWhich:=tSend;
end;
end;
if RunPop and (not SomeThingToDo) then
begin
if HaveEmailToReceive then
begin
SomethingToDo:=True;
TimerForWhich:=tReceive;
end
else
begin
SomeThingToDo:=True;
TimerForWhich:=tCheck;
end;
end;
finally
if SomeThingToDo then
tmWork.Enabled:=True
else
tmServer.Enabled:=True;
end;
case TimerForWhich of
tSend:
sbSys.SimpleText:='Server is going to send mail ... ';
tReceive:
sbSys.SimpleText:='Server is going to receive mail ... ';
tCheck:
sbSys.SimpleText:='Server is going to check mail ... ';
else
sbSys.SimpleText:='Server is idle ... ';
end;
end;
end
else
tmServer.Enabled:=True;
except
on E:Exception do
begin
sRE(gErr1,E,'On tmServer.OnTimer');
tmServer.Enabled:=True;
end;
end;
end;
procedure TeMail.FormDestroy(Sender: TObject);
begin
if InDebug then
mLogFile.Free;
end;
procedure TeMail.ApplicationEvents1Exception(Sender: TObject;
E: Exception);
begin
sRE(gErr1,Sender,E,'In ApplicationEvents');
mHaveException:=True;
try
if (Sender.ClassName ='TTimer') and (E.Message ='Connection Failed') then
begin
mHaveException:=False;
end;
finally
tmServer.Enabled:=True;
end;
end;
procedure TeMail.tmWorkTimer(Sender: TObject);
begin
tmWork.Enabled:=False;
try
case TimerForWhich of
tSend:
begin
sbSys.SimpleText:='Server is sending mail ... ';
SendMail;
sbSys.SimpleText:='Server has sent mail ! ';
end;
tReceive:
begin
sbSys.SimpleText:='Server is receiving mail ... ';
ReceiveMail;
sbSys.SimpleText:='Server has received mail ! ';
end;
tCheck:
begin
sbSys.SimpleText:='Server is checking mail ... ';
HaveEmailBox:=CheckMail(EmailBoxMum);
sbSys.SimpleText:='Server has checked mail ! ';
end;
else
sbSys.SimpleText:='Server is idle ! ';
end;
finally
TimerForWhich:=tIdle;
tmServer.Enabled:=true;
end;
end;
procedure TeMail.popCheckConnect(Sender: TObject);
begin
POPCConnect:=True;
mvCheckMailNum:=popCheck.MailCount;
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Connect ! ';
popCheck.Disconnect;
end;
procedure TeMail.popCheckDisconnect(Sender: TObject);
begin
POPCConnect:=False;
DoCheckMail:=False;
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Disconnect ! ';
end;
procedure TeMail.popCheckStatus(Sender: TComponent; Status: String);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+Status;
end;
procedure TeMail.popCheckSuccess(Sender: TObject);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Success ! ';
end;
procedure TeMail.popCheckConnectionFailed(Sender: TObject);
begin
DoCheckMail:=False;
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Connection failed ! ';
sRE(gErr1,'CheckMail : Connection failed ! ');
end;
procedure TeMail.popCheckAuthenticationFailed(var Handled: Boolean);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Authentication failed ! ';
sRE(gErr1,'CheckMail : Authentication failed ! ');
end;
procedure TeMail.popCheckAuthenticationNeeded(var Handled: Boolean);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Authentication needed ! ';
end;
procedure TeMail.popCheckConnectionRequired(var Handled: Boolean);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Connection required ! ';
end;
procedure TeMail.popCheckDecodeEnd(Sender: TObject);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Decode end ! ';
end;
procedure TeMail.popCheckDecodeStart(var FileName: String);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Decode start ! ';
end;
procedure TeMail.popCheckFailure(Sender: TObject);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Failure ! ';
sRE(gErr1,'CheckMail : Failure ! ');
end;
procedure TeMail.popCheckHostResolved(Sender: TComponent);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Host resolved ! ';
end;
procedure TeMail.popCheckInvalidHost(var Handled: Boolean);
begin
POPCConnect:=False;
DoCheckMail:=False;
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Invalid host ! ';
sRE(gErr1,'CheckMail : Invalid host ! ');
end;
procedure TeMail.popCheckList(Msg, Size: Integer);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'List ! ';
end;
procedure TeMail.popCheckPacketRecvd(Sender: TObject);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Packet received ! ';
end;
procedure TeMail.popCheckReset(Sender: TObject);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Reset ! ';
end;
procedure TeMail.popCheckRetrieveEnd(Sender: TObject);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Retrieve end ! ';
end;
procedure TeMail.popCheckRetrieveStart(Sender: TObject);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Retrieve start ! ';
end;
procedure TeMail.popReceiveConnect(Sender: TObject);
var
i:Integer;
eMsg:string;
begin
POPRConnect:=True;
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Connect ! ';
CurReceiveMailNum:=-1;
CurReceiveCount:=0;
try
CurReceiveCount:=popReceive.MailCount;
TotalReceiveMailNum:=CurReceiveCount;
if CurReceiveCount>0 then
begin
mmList.Clear;
popReceive.List;
CurReceiveMailNum:=0;
ProcessCurMailDone:=False;
for i:=CurReceiveCount downto 1 do
begin
try
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'To Receive Mail: '+IntToStr(i);
CurReceiveMailNum:=i;
ProcessCurMailDone:=False;
popReceive.Parse:=True;
popReceive.GetMailMessage(i);
while not ProcessCurMailDone do
begin
Application.ProcessMessages;
end;
except
on E:Exception do
begin
SetLength(eMsg,200);
eMsg:=E.Message;
if Pos('List index out of bounds',eMsg)>0 then
begin
popReceive.DeleteMailMessage(i);
end
else
SRE(gErr1,Sender,E,'ReceiveConnect:GetMail:'+IntToStr(i));
end;
end
end;
end;
finally
ReceiveMailDone:=True;
popreceive.Disconnect;
CurReceiveMailNum:=-1;
TotalReceiveMailNum:=-1;
end;
end;
procedure TeMail.popReceiveRetrieveEnd(Sender: TObject);
var
toEmail:string;
fromEmail:string;
Subject:string;
pop3ServerName:string;
attchBackUp:string;
AttachName:string;
UserName:string;
Password:string;
MailBody:string;
szDateTime:string;
isCheck:string;
isOK:string;
nAttachCount:Integer;
nMailID:Integer;
IsStatus:string;
szMailDate:string;
maPath:string;
i:Integer;
OldFN,NewFN:string;
AccessPath:Boolean;
begin
lProgress.Caption:='Progress ('+IntToStr(TotalReceiveMailNum-CurReceiveMailNum+1)+'/'+IntToStr(TotalReceiveMailNum)+') : ';
pbReceive.Max:=TotalReceiveMailNum;
pbReceive.Position:=TotalReceiveMailNum-CurReceiveMailNum;
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Process Email: '+IntToStr(CurReceiveMailNum);
ProcessCurMailDone:=False;
toEmail:='';
fromEmail:=popReceive.MailMessage.From;
Subject:=popReceive.MailMessage.Subject;
pop3ServerName:=mPOPServer[EmailBoxMum].mServer;
attchBackUp:=mPOPServer[EmailBoxMum].mAttchPath;
nAttachCount:=popReceive.MailMessage.Attachments.Count;
userName:='';
Password:='';
MailBody:=popReceive.MailMessage.Body.Text;
szDateTime:=FormatDateTime('yyyymmdd',Date)+FormatDateTime('hhnnss',Time);
isCheck:='0';
isOK:='0';
isStatus:='0';
mmInfo.Clear;
mmInfo.Lines.Add(' POP3 Server : '+pop3ServerName);
mmInfo.Lines.Add(' User ID :'+mPOPServer[EmailBoxMum].mUser);
mmInfo.Lines.Add(' From Mail : '+fromEmail);
mmInfo.Lines.Add(' Subject : '+Subject);
mmInfo.Lines.Add(' attachment Count : '+IntToStr(nAttachCount));
mmInfo.Lines.Add(' CurMailID : '+IntToStr(CurReceiveMailNum));
adoEmail.Open;
if adoEmail.Connected then
begin
if atReceive.Active then
atReceive.Active:=False;
atReceive.Open;
try
atReceive.Append;
atReceive.FieldByName('ToEmail').AsString:=toEmail;
atReceive.FieldByName('FromEmail').AsString:=fromEmail;
atReceive.FieldByName('Subject').AsString:=Subject;
atReceive.FieldByName('POP3ServerName').AsString:=POP3ServerName;
if nattachCount=0 then
begin
atReceive.FieldByName('AttachBackupPath').AsString:='';
atREceive.FieldByName('AttachName').AsString:='';
end
else
begin
atReceive.FieldByName('AttachBackupPath').AsString:=attchBackUp;
StrSToStr(popReceive.MailMessage.Attachments,';',AttachName);
atReceive.FieldByName('AttachName').AsString:=AttachName;
end;
atReceive.FieldByName('AttachCounter').AsInteger:=nAttachCount;
atReceive.FieldByName('UserName').AsString:='';
atReceive.FieldByName('Password').AsString:='';
atReceive.FieldByName('MailBody').AsString:=MailBody;
atReceive.FieldByName('DateTime').AsString:=szDateTime;
atREceive.FieldByName('IsCheck').AsString:=isCheck;
atReceive.FieldByName('IsOK').AsString:=isOK;
atReceive.FieldByName('IsStatus').AsString:=isStatus;
atReceive.FieldByName('PendingStartTime').AsString:='';
atReceive.FieldByName('PendingEndTime').AsString:='';
atReceive.Post;
finally
atReceive.Active:=False;
end;
if nAttachcount>0 then
begin
AccessPath:=True;
if atReceive.Active then
atReceive.Close;
atReceive.Active:=True;
atReceive.FindLast;
nMailID:=atReceive.FieldByName('ID').AsInteger;
szMailDate:=copy(atReceive.FieldByName('DateTime').AsString,1,8);
atReceive.Close;
maPath:=AttchBackUp;
while maPath[Length(maPath)]='/' do
begin
Delete(maPath,Length(maPath),1);
end;
if not DirectoryExists(maPath) then
begin
if not CreateDir(maPath) then
begin
sRE(gErr1,'Cannot access '+maPath+', Create subdirectory'+' in program file directory ! ');
maPath:=gAppPath;
end;
end;
maPath:=maPath+'/'+szMailDate;
if not DirectoryExists(maPath) then
begin
if not CreateDir(maPath) then
begin
sRE(gErr1,'Cannot access '+maPath+', Create subdirectory'+' in program file directory ! ');
AccessPath:=False;
end;
end;
if AccessPath then
begin
maPath:=maPath+'/'+IntToStr(nMailID);
if not DirectoryExists(maPath) then
begin
if not CreateDir(maPath) then
begin
sRE(gErr1,'Cannot access '+maPath+', Create subdirectory'+' in program file directory ! ');
AccessPath:=False;
end;
end;
end;
if AccessPath then
begin
maPath:=maPath+'/';
if AttchBackUp[length(AttchBackUp)]<>'/' then
AttchBackUp:=AttchBackUp+'/';
for i:=0 to nAttachCount-1 do
begin
OldFN:=AttchBackUp+popReceive.MailMessage.Attachments.Strings;
NewFN:=maPath+popReceive.MailMessage.Attachments.Strings;
CopyFile(PChar(OldFN),PChar(NewFN),False);
DeleteFile(OldFN);
end;
end;
end;
end;
Inc(totReceiveNum);
lReceiveNum.Caption:=IntToStr(totReceiveNum)+' Mails received';
if totReceiveNum>0 then
lReceivenum.Visible:=True;
ProcessCurMailDone:=true;
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Done Process Email: '+IntToStr(CurReceiveMailNum);
end;
procedure TeMail.popReceiveList(Msg, Size: Integer);
begin
mmList.Lines.Add('Message '+IntToStr(Msg)+': '+IntToStr(Size)+' bytes');
end;
procedure TeMail.popReceiveAuthenticationFailed(var Handled: Boolean);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Authentication failed ! ';
sRE(gErr1,'ReceiveMail : Authentication failed ! ');
end;
procedure TeMail.popReceiveAuthenticationNeeded(var Handled: Boolean);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Authentication needed ! ';
end;
procedure TeMail.popReceiveConnectionFailed(Sender: TObject);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Connection failed ! ';
sRE(gErr1,'ReceiveMail : Connection failed ! ');
end;
procedure TeMail.popReceiveConnectionRequired(var Handled: Boolean);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Connection required ! ';
popReceive.Connect;
end;
procedure TeMail.popReceiveDecodeEnd(Sender: TObject);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Decode end';
end;
procedure TeMail.popReceiveDecodeStart(var FileName: String);
var
strS:string;
strF:string;
begin
SetLength(strS,200);
SetLength(strF,200);
strS:=FileName;
if pos('?B?',Uppercase(strS))>0 then
begin
Delete(strS,1,pos('?B?',Uppercase(strS))+2);
Delete(strS,pos('?=',strS),2);
strF:=DecodeBase64(strS);
FileName:=strF;
end
else if pos('?Q?',Uppercase(FileName))>0 then
begin
Delete(strS,1,pos('?Q?',Uppercase(strS))+2);
Delete(strS,pos('?=',strS),2);
strF:=DecodeQuotedP(strS);
FileName:=strF;
end;
if Pos('?',FileName)>0 then
FileName:=Format('%f',[Time])+'.Tmp';
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Decode start ! ';
end;
procedure TeMail.popReceiveDisconnect(Sender: TObject);
begin
POPRConnect:=False;
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Disconnect ! ';
DeleteTempFile(popReceive.AttachFilePath);
end;
procedure TeMail.popReceiveFailure(Sender: TObject);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Failure ! ';
sRE(gErr1,'ReceiveMail : Failure ! ');
end;
procedure TeMail.popReceiveHostResolved(Sender: TComponent);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Host resolved ! ';
end;
procedure TeMail.popReceiveInvalidHost(var Handled: Boolean);
begin
POPRConnect:=False;
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Invalid host ! ';
sRE(gErr1,'ReceiveMail : Invalid host ! ');
end;
procedure TeMail.popReceivePacketRecvd(Sender: TObject);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Packet received ! ';
end;
procedure TeMail.popReceiveReset(Sender: TObject);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Reset ! ';
end;
procedure TeMail.popReceiveRetrieveStart(Sender: TObject);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Retrieve start ! ';
end;
procedure TeMail.popReceiveStatus(Sender: TComponent; Status: String);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+Status;
end;
procedure TeMail.popReceiveSuccess(Sender: TObject);
begin
sbPOP.SimpleText:=TimeToStr(Time)+' : '+'Success ! ';
end;
procedure TeMail.smtpSendAttachmentNotFound(Filename: String);
begin
sbSMTP.SimpleText:=TimeToStr(Time)+' : '+'Attachment('+FileName+') not found ! ';
sRE(gErr1,'SendMail : Attachment('+FileName+') not found ! ');
end;
procedure TeMail.smtpSendAuthenticationFailed(var Handled: Boolean);
begin
sbSMTP.SimpleText:=TimeToStr(Time)+' : '+'Authentication failed ! ';
sRE(gErr1,'SendMail : Authentication failed ! ');
end;
procedure TeMail.smtpSendConnect(Sender: TObject);
begin
SMTPConnect:=True;
sbSMTP.SimpleText:=TimeToStr(Time)+' : '+'Connect ! ';
if smtpSend.ReplyNumber = 250 then
smtpSend.Transaction('auth login');
if smtpSend.ReplyNumber = 334 then
begin
smtpSend.Transaction(EncodeBase64(mCSMTPServer.mUser));
smtpSend.Transaction(EncodeBase64(mCSMTPServer.mPassword));
end;
smtpSend.SendMail;
sbSMTP.SimpleText:=TimeToStr(Time)+' : '+'Start send mail ! ';
end;
procedure TeMail.smtpSendConnectionFailed(Sender: TObject);
begin
sbSMTP.SimpleText:=TimeToStr(Time)+' : '+'Connection failed ! ';
sRE(gErr1,'SendMail : Connection failed ! ');
end;
procedure TeMail.smtpSendConnectionRequired(var Handled: Boolean);
begin
sbSMTP.SimpleText:=TimeToStr(Time)+' : '+'Connection required ! ';
smtpSend.Connect;
end;
procedure TeMail.smtpSendDisconnect(Sender: TObject);
begin
SMTPConnect:=False;
sbSMTP.SimpleText:=TimeToStr(Time)+' : '+'Disconnect ! ';
DoneCurSendMail:=True;
end;
procedure TeMail.smtpSendEncodeEnd(Filename: String);
begin
sbSMTP.SimpleText:=TimeToStr(Time)+' : '+'Encode end ! ';
end;
procedure TeMail.smtpSendEncodeStart(Filename: String);
begin
sbSMTP.SimpleText:=TimeToStr(Time)+' : '+'Encode start ! ';
end;
procedure TeMail.smtpSendFailure(Sender: TObject);
begin
DoneCurSendMail:=True;
CurSendMailSuccess:=False;
sbSMTP.SimpleText:=TimeToStr(Time)+' : '+'Send failure ! ';
sRE(gErr1,'SendMail : Send failure ! ');
end;
procedure TeMail.smtpSendHeaderIncomplete(var handled: Boolean;
hiType: Integer);
begin
sbSMTP.SimpleText:=TimeToStr(Time)+' : '+'Header incomplete ! ';
sRE(gErr1,'SendMail : Header incomplete ! ');
end;
procedure TeMail.smtpSendHostResolved(Sender: TComponent);
begin
sbSMTP.SimpleText:=TimeToStr(Time)+' : '+'Host resolved ! ';
end;
procedure TeMail.smtpSendInvalidHost(var Handled: Boolean);
begin
sbSMTP.SimpleText:=TimeToStr(Time)+' : '+' Invalid host ! ';
sRE(gErr1,'SendMail : Invalid host !');
end;
procedure TeMail.smtpSendMailListReturn(MailAddress: String);
begin
sbSMTP.SimpleText:=TimeToStr(Time)+' : '+'MailList '+MailAddress+' return ! ';
end;
procedure TeMail.smtpSendPacketSent(Sender: TObject);
begin
sbSMTP.SimpleText:=TimeToStr(Time)+' : '+'Packet sent ! ';
end;
procedure TeMail.smtpSendRecipientNotFound(Recipient: String);
begin
sbSMTP.SimpleText:=TimeToStr(Time)+' : '+Recipient+' not found ! ';
sRE(gErr1,'SendMail : '+Recipient+' not found ! ');
end;
procedure TeMail.smtpSendSendStart(Sender: TObject);
begin
sbSMTP.SimpleText:=TimeToStr(Time)+' : '+'Send start ! ';
end;
procedure TeMail.smtpSendStatus(Sender: TComponent; Status: String);
begin
sbSMTP.SimpleText:=TimeToStr(Time)+' : '+Status;
end;
procedure TeMail.smtpSendSuccess(Sender: TObject);
begin
DoneCurSendMail:=True;
CurSendMailSuccess:=True;
sbSMTP.SimpleText:=TimeToStr(Time)+' : '+'Send success ! ';
smtpSend.Disconnect;
end;
procedure TeMail.SetPOP3Server1Click(Sender: TObject);
var
frmIni:TfrmIni;
begin
frmIni:=TfrmIni.Create(Application);
frmIni.ShowModal;
frmIni.Free;
if GetServersInfo then
POPServerReady:=true
else
POPServerReady:=False;
end;
procedure TeMail.Abort1Click(Sender: TObject);
var
DllName,szEN,szCO,szIDChar;
LibHandle:THandle;
ShowAboutrocedure(szExeName,szCorp,szProductIDChar);stdcall;
begin
szEN:=StrNew(PChar(ExtractFileName(Application.ExeName)));
szCo:=StrNew(PChar('AAA'));
szID:=StrNew(PChar('BBB'));
DllName:=StrNew(PChar('cr.dll'));
try
LibHandle := LoadLibrary(DllName);
if LibHandle = 0 then
ShowMessage('Cannot find CR.DLL ! ')
else
begin
try
@ShowAbout:=GetProcAddress(LibHandle, 'ShowAbout');
if not (@ShowAbout = nil) then
ShowAbout(szEN,szCO,szID)
else
ShowMessage('Call procedure Err ! ');
finally
FreeLibrary(LibHandle);
end;
end;
finally
StrDispose(szEN);
StrDispose(szCO);
StrDispose(szID);
StrDispose(DllName);
end;
end;
procedure TeMail.mmHistoryKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
NewString: string;
ShowStr:string;
tStr:string;
MS:TMemoryStatus;
begin
try
if ((ssShift in Shift) and (ssCtrl in Shift)) and (Key=VK_F12) then
begin
SetLength(NewString,20);
newString:='';
if InputQuery('Enter Password','Password:',NewString) then
begin
if NewString='DebugStatus' then
begin
SetLength(ShowStr,250);
SetLength(tStr,50);
case TimerForWhich of
tIdle:
tStr:='tIdle';
tSend:
tStr:='tSend';
tCheck:
tStr:='tCheck';
tReceive:
tStr:='tReceive';
else
tStr:='Unknown';
end;
ShowStr:='TimerForWhich:'+tStr;
case CurSystemStatus of
sNotReady:
tStr:='sNotReady';
sIdle:
tStr:='sIdle';
sCheckMail:
tStr:='sCheckMail';
sSendMail:
tStr:='sSendMail';
sReceiveMail:
tStr:='sReceiveMail';
else
tStr:='Unknown';
end;
ShowStr:=ShowStr+#13+'CurSystemStatus:'+tStr;
if SMTPConnect then
ShowStr:=ShowStr+#13+'SMTP:'+'Connect';
if POPCConnect then
ShowStr:=ShowStr+#13+'POP(C):'+'Connect';
if POPRConnect then
ShowStr:=ShowStr+#13+'POP(R):'+'Connect';
ShowStr:=ShowStr+#13#13+'Memory Status:';
MS.dwLength:=SizeOf(TMemorystatus);
FillChar(MS,SizeOf(TMemorystatus),0);
GlobalMemoryStatus(MS);
ShowStr:=ShowStr+#13+Format('Total:%u',[MS.dwTotalPhys]);
ShowStr:=ShowStr+#13+Format('Avail:%u',[MS.dwAvailPhys]);
ShowStr:=ShowStr+#13+Format('InUse:%u%%',[MS.dwMemoryLoad]);
ShowStr:=ShowStr+#13+Format('TotPage:%u',[MS.dwTotalPageFile]);
ShowStr:=ShowStr+#13+Format('AvlPage:%u',[MS.dwAvailPageFile]);
ShowStr:=ShowStr+#13+Format('TotVirtual:%u',[MS.dwTotalVirtual]);
ShowStr:=ShowStr+#13+Format('AvlVritual:%u',[MS.dwAvailVirtual]);
ShowMessage(ShowStr);
end;
end;
end;
finally
;
end;
end;
end.