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

Email服务器

开发平台:

Delphi

  1. unit UEmailFile;
  2. interface
  3. uses Classes,SysUtils,Windows,uEncrypt;
  4. type
  5.   TBaseEmailFile=class
  6.   protected
  7.     FFileName:string;
  8.     FCipher:ICipher;
  9.     function DecodeString(S:String):String;virtual;
  10.     function EncodeString(S:String):String;virtual;
  11.   public
  12.     constructor Create(AFilePath:string;ACipher:ICipher=nil);
  13.     destructor Destroy;override;
  14.     property  FilePath:string  read FFileName write FFileName;
  15.   end;
  16.   TEmailFile=class(TBaseEmailFile)
  17.   private
  18.     FSender,
  19.     FRecver:string;
  20.     FSubject:String;
  21.     FDate:string;
  22.     FSize:string;
  23.     FAttchs:TStrings;
  24.     FContent:string ;
  25.     procedure SetAttchs(const Value: TStrings);
  26.   public
  27.     Constructor Create(AFilePath:string);
  28.     destructor Destroy;override;
  29.     procedure SaveEmail;
  30.     procedure GetEmail;
  31.     property  Sender:string  read FSender write FSender;
  32.     property  Recver:string  read FRecver write FRecver;
  33.     property  Subject:String  read FSubject write FSubject;
  34.     property  Date:string  read FDate write FDate;
  35.     property  Size:string  read FSize write FSize;
  36.     property  Attchs:TStrings  read FAttchs write SetAttchs;
  37.     property  Content:string  read FContent write FContent;
  38.   end;
  39. implementation
  40. uses uCommon,Math;
  41. type
  42.     TEmailRec=packed record
  43.     Sender,
  44.     Recver:string[50];
  45.     Subject:String[255];
  46.     Date:string[50];
  47.     Size:string[20];
  48.     Attchs:array[0..2000]of AnsiChar;
  49.     Content:array[0..5000]of AnsiChar ;
  50.   end;
  51. { TBaseEmailFile }
  52. constructor TBaseEmailFile.Create(AFilePath: string; ACipher: ICipher);
  53. begin
  54.   inherited Create;
  55.   FFileName:=AFilePath;
  56.   FCipher:=ACipher;
  57. end;
  58. function TBaseEmailFile.DecodeString(S: String): String;
  59. begin
  60.   if FCipher=nil then
  61.     Result:=S
  62.   else
  63.     Result:=FCipher.DecodeString(S);
  64. end;
  65. destructor TBaseEmailFile.Destroy;
  66. begin
  67.   inherited;
  68. end;
  69. function TBaseEmailFile.EncodeString(S: String): string;
  70. begin
  71.   if FCipher=nil then
  72.     Result:=S
  73.   else
  74.     Result:=FCipher.EncodeString(S);
  75. end;
  76. { TEmailFile }
  77. constructor TEmailFile.Create(AFilePath: string);
  78. begin
  79.   inherited Create(AFilePath,nil);
  80.   FAttchs:=TStringList.Create;
  81. end;
  82. destructor TEmailFile.Destroy;
  83. begin
  84.   FAttchs.Free;
  85.   inherited;
  86. end;
  87. procedure TEmailFile.GetEmail;
  88. var
  89.   f:file of TEmailRec;
  90.   Ret:TEmailRec;
  91.   temp:string;
  92. begin
  93.   FillChar(Ret,sizeof(TEMailRec),0);
  94.   temp:=ChangeFileExt(FFileName,'.txt');
  95.   RenameFile(FFileName,temp);
  96.   AssignFile(f,temp);
  97.   try
  98.     Reset(f);
  99.     read(f,Ret);
  100.   finally
  101.     CloseFile(f);
  102.   end;
  103.   RenameFile(temp,FFileName);
  104.   FSender:=DecodeString(Ret.Sender);
  105.   FRecver:=DecodeString(Ret.Recver);
  106.   FSubject:=DecodeString(Ret.Subject);
  107.   FDate:=DecodeString(Ret.Date);
  108.   FSize:=DecodeString(Ret.Size);
  109.   FAttchs.Clear;
  110.   Split(';',DecodeString(StrPas(Ret.Attchs)),FAttchs);
  111.   FContent:=DecodeString(StrPas(Ret.Content));
  112. end;
  113. procedure TEmailFile.SaveEmail;
  114. var
  115.   f:file of TEmailRec;
  116.   Rec:TEmailRec;
  117.   temp:string;
  118.   S:string;
  119. begin
  120.   FillChar(Rec,sizeof(TEMailRec),0);
  121.   Rec.Sender:=EncodeString(FSender);
  122.   Rec.Recver:=EncodeString(FRecver);
  123.   Rec.Subject:=EncodeString(FSubject);
  124.   Rec.Date:=EncodeString(FDate);
  125.   Rec.Size:=EncodeString(FSize);
  126.   S:=EncodeString(Join(';',FAttchs));
  127.   ZeroMemory(@Rec.Attchs[0],Length(Rec.Attchs));
  128.   if Length(S)<>0 then
  129.     CopyMemory(@Rec.Attchs[0],@S[1],Min(Length(Rec.Attchs),Length(S)));
  130.   ZeroMemory(@Rec.Content[0],Length(Rec.Content));
  131.   if Length(FContent)<>0 then
  132.     CopyMemory(@Rec.Content[0],@FContent[1],Min(Length(Rec.Content),Length(FContent)));
  133.   temp:=ChangeFileExt(FFileName,'.txt');
  134.   AssignFile(f,temp);
  135.   try
  136.     Rewrite(f);
  137.     Write(f,Rec);
  138.   finally
  139.     CloseFile(f);
  140.   end;
  141.   RenameFile(temp,FFileName);
  142. end;
  143. procedure TEmailFile.SetAttchs(const Value: TStrings);
  144. begin
  145.   if (FAttchs <>Value) then
  146.     FAttchs.Assign(Value);
  147. end;
  148. end.