uCheckEmail.pas
上传用户:yjb1804
上传日期:2021-01-30
资源大小:3105k
文件大小:7k
源码类别:

Email服务器

开发平台:

Delphi

  1. unit uCheckEmail;
  2. interface
  3. uses Classes,SysUtils,Windows,uCheckEmailThread,ExtCtrls,Forms;
  4. const DEF_DETECT_INTERVAL=1000*60*15; //15分钟检查一次 
  5. type
  6.     {自动检测新邮件,仅当最小化时检测新邮件,仅启动时检测新邮件,从不检测新邮件}
  7.   TEmailDetectOption=(edoAuto,edoAppMin,edoAppRun,edoNerverDetect);
  8.   TAppState=(asInit,asRestore,asMin,asClose);
  9.   PEmailDetect=^TEmailDetect;
  10.   TEmailDetect=record
  11.     EmailAccount:TAccount;
  12.     EmailDetectOption:TEmailDetectOption;
  13.   end;
  14.  //---------------------------检查某一个邮箱----------------------------
  15.   TCheckOneEmail=class   
  16.   private
  17.     FEmailDetect:TEmailDetect;
  18.     FTimer:TTimer;
  19.     FDetectCount:Integer; //检测了多少次
  20.     FExpectedCount:Integer; //想要检测多少次
  21.     FOldUIDLs:TStrings;
  22.     FOnNewEmailArrive:TNewEmailArrive;
  23.     FChecker:TCheckEmailThread;
  24.     FDetecting:Boolean; //是否正在检查
  25.     procedure SetOldUIDLs(const Value: TStrings);
  26.     procedure ThreadOnTerminate(Sender:TObject);
  27.   protected
  28.     procedure  OnTimer(Sender:TObject);
  29.   public
  30.     constructor Create;
  31.     destructor Destroy;override;
  32.     procedure DetectOnce; //只检测一次
  33.     procedure DetectRepeat;//重复检测
  34.     procedure StopDetect;//停止检测
  35.     property  OldUIDLs:TStrings  read FOldUIDLs write SetOldUIDLs;
  36.     property  OnNewEmailArrive:TNewEmailArrive  read FOnNewEmailArrive write FOnNewEmailArrive;
  37.     property  EmailDetect:TEmailDetect  read FEmailDetect write FEmailDetect;
  38.     property  Detecting:Boolean  read FDetecting write FDetecting;
  39.   end;
  40.   //------------------------------------------------------------------------
  41.   TEmailDetects=array of TEmailDetect;
  42.   TOnEnterState=procedure (Sender:TObject;Runing:Boolean)of Object;
  43.   TCheckEmail=class
  44.   private
  45.     FEmailDetects:TEmailDetects;
  46.     FEmailCheckList:TThreadList;
  47.     FOnNewEmailArrive:TNewEmailArrive;
  48.     FOnEnterState:TOnEnterState;
  49.     FOldUIDLs:TStrings;
  50.     procedure SetOldUIDLs(const Value: TStrings);
  51.   protected
  52.     procedure ReadEmailDetects;
  53.     
  54.     procedure StopDetect;
  55.     function FindChecker(EmailAccount:string):TCheckOneEmail;
  56.   public
  57.     constructor Create;
  58.     destructor Destroy;override;
  59.     procedure CreateChecker;
  60.     procedure EnterState(AppState:TAppState);
  61.     property  OldUIDLs:TStrings  read FOldUIDLs write SetOldUIDLs;
  62.     property  OnNewEmailArrive:TNewEmailArrive  read FOnNewEmailArrive write FOnNewEmailArrive;
  63.     property OnEnterState:TOnEnterState read FOnEnterState write FOnEnterState;
  64.   end;
  65. implementation
  66. uses uCommon, uMyXml;
  67. { TCheckEmail }
  68. procedure TCheckEmail.EnterState(AppState: TAppState);
  69. var
  70.   C:TCheckOneEmail;
  71.   I:Integer;
  72.   HadRun:Boolean;
  73. begin
  74.   HadRun:=False;
  75.   for I:=0 to Length(FEmailDetects)-1 do
  76.   begin
  77.     C:=FindChecker(FEmailDetects[I].EmailAccount.EmailAccount);
  78.     if  C=nil then Continue;
  79.     if C.Detecting then   C.StopDetect;
  80.     case C.EmailDetect.EmailDetectOption of //邮箱的“检查新邮件”属性
  81.       edoAuto: //自动检查
  82.         C.DetectRepeat;
  83.       edoAppMin:  //窗口最小化时检查
  84.       begin
  85.         if AppState=asMin then
  86.           C.DetectRepeat
  87.         else
  88.           C.StopDetect;
  89.       end;
  90.       edoAppRun:    //启动程序时检查
  91.       begin
  92.         if AppState=asInit then
  93.           C.DetectOnce
  94.         else
  95.           C.StopDetect;
  96.       end;
  97.       edoNerverDetect:;  //从不检查
  98.     else ;
  99.     end;
  100.     if not HadRun then HadRun:=C.Detecting;
  101.   end;
  102.   if Assigned(FOnEnterState) then  FOnEnterState(self,HadRun);
  103. end;
  104. constructor TCheckEmail.Create;
  105. begin
  106.   FOldUIDLs:=TStringList.Create;
  107.   FEmailCheckList:=TThreadList.Create;
  108.   ReadEmailDetects;
  109. end;
  110. destructor TCheckEmail.Destroy;
  111. begin
  112.   StopDetect;
  113.   FEmailCheckList.Free;
  114.   FOldUIDLs.Free;
  115.   WriteLog(#$D#$A'===DetectEmails End===');
  116.   inherited;
  117. end;
  118. procedure TCheckEmail.CreateChecker;
  119. var
  120.   I:Integer;
  121.   checkOne:TCheckOneEmail;
  122. begin
  123.   WriteLog(#$D#$A'===DetectEmails Begin=====');
  124.   for I:=0 to Length(FEmailDetects)-1 do
  125.   begin
  126.     checkOne:=TCheckOneEmail.Create;
  127.     checkOne.EmailDetect:=FEmailDetects[I];
  128.     checkOne.OnNewEmailArrive:=Self.OnNewEmailArrive; //////////
  129.     checkOne.OldUIDLs:=Self.FOldUIDLs;
  130.     FEmailCheckList.Add(checkOne);
  131.   end;
  132. end;
  133. function TCheckEmail.FindChecker(EmailAccount: string): TCheckOneEmail;
  134. var
  135.   L:TList;
  136.   I:Integer;
  137. begin
  138.   Result:=nil;
  139.   L:=FEmailCheckList.LockList;
  140.   try
  141.     for i:=0 to L.Count-1 do
  142.       if CompareText(TCheckOneEmail(L[I]).EmailDetect.EmailAccount.EmailAccount,EmailAccount)=0 then
  143.       begin
  144.         Result:=TCheckOneEmail(L[I]);
  145.         Break;
  146.       end;
  147.   finally
  148.     FEmailCheckList.UnlockList;
  149.   end;
  150. end;
  151. procedure TCheckEmail.ReadEmailDetects;
  152. var
  153.   xml:TAppXml;
  154.   L:TList;
  155.   I:Integer;
  156. begin
  157.   xml:=TAppXml.Create;
  158.   try
  159.     L:=TList.Create;
  160.     try
  161.       xml.GetAllEmailDetectOptions(L);
  162.       SetLength(FEmailDetects,L.Count);
  163.       for I:=0 to L.Count-1 do
  164.       begin
  165.         FEmailDetects[I]:=PEmailDetect(L[I])^;
  166.         Dispose(PEmailDetect(L[I]));
  167.       end;
  168.     finally
  169.       L.Free;
  170.     end;
  171.   finally
  172.     xml.Free;
  173.   end;
  174. end;
  175. procedure TCheckEmail.SetOldUIDLs(const Value: TStrings);
  176. begin
  177.   if FOldUIDLs<>Value then
  178.     FOldUIDLs.Assign(Value);
  179. end;
  180. procedure TCheckEmail.StopDetect;
  181. var
  182.   I:Integer;
  183.   L:TList;
  184. begin
  185.   L:= FEmailCheckList.LockList;
  186.   try
  187.     for I:=L.Count-1 downto 0 do
  188.       TCheckOneEmail(L[I]).Free;
  189.   finally
  190.     FEmailCheckList.UnlockList;
  191.   end;
  192. end;
  193. { TCheckOneEmail }
  194. constructor TCheckOneEmail.Create;
  195. begin
  196.   Ftimer:=TTimer.Create(nil);
  197.   FTimer.Enabled:=False;
  198.   FTimer.OnTimer:=OnTimer;
  199.   FTimer.Interval:=DEF_DETECT_INTERVAL;
  200.   FOldUIDLs:=TStringList.Create;
  201.   FChecker:=nil;
  202.   FDetecting:=False;
  203.   FDetectCount:=0;
  204.   FExpectedCount:=0;
  205. end;
  206. destructor TCheckOneEmail.Destroy;
  207. begin
  208.   if FChecker<>nil then FChecker.Terminate;
  209.   FOldUIDLs.Free;
  210.   FTimer.Enabled:=False;
  211.   FTimer.Free;
  212.   inherited;
  213. end;
  214. procedure TCheckOneEmail.DetectOnce;
  215. begin
  216.   FExpectedCount:=1;
  217.   FTimer.Enabled:=True;
  218. end;
  219. procedure TCheckOneEmail.DetectRepeat;
  220. begin
  221.   FExpectedCount:=-1;
  222.   FTimer.Enabled:=True;
  223. end;
  224. procedure TCheckOneEmail.OnTimer(Sender: TObject);
  225. var
  226.   arr:TAccounts;
  227. begin
  228.   Detecting :=True ;
  229.   if FChecker<>nil then FChecker.Terminate;
  230.   SetLength(arr,1);
  231.   arr[0]:=FEmailDetect.EmailAccount;
  232.   FChecker:=TCheckEmailThread.Create(arr,OldUIDLs);
  233.   FChecker.OnNewEmailArrive:=OnNewEmailArrive;
  234.   FChecker.OnTerminate:=ThreadOnTerminate;
  235.   FChecker.Resume;
  236.   Inc(FDetectCount);
  237.   if FExpectedCount=-1 then Exit;   //重复执行检查
  238.   if FExpectedCount<=FDetectCount then FTimer.Enabled:=False; //执行次数已到
  239. end;
  240. procedure TCheckOneEmail.SetOldUIDLs(const Value: TStrings);
  241. begin
  242.   if FOldUIDLs<> Value then
  243.     FOldUIDLs.Assign(Value);
  244. end;
  245. procedure TCheckOneEmail.StopDetect;
  246. begin
  247.   FExpectedCount:=0;
  248.   FDetectCount:=0;
  249.   FTimer.Enabled:=False;
  250.   if FChecker<>nil then FChecker.Terminate;
  251. end;
  252. procedure TCheckOneEmail.ThreadOnTerminate(Sender: TObject);
  253. begin
  254.   FChecker:=nil;
  255.   FDetecting:=False;
  256. end;
  257. end.