UEmailFile.pas
上传用户:yjb1804
上传日期:2021-01-30
资源大小:3105k
文件大小:4k
- unit UEmailFile;
- interface
- uses Classes,SysUtils,Windows,uEncrypt;
- type
- TBaseEmailFile=class
- protected
- FFileName:string;
- FCipher:ICipher;
- function DecodeString(S:String):String;virtual;
- function EncodeString(S:String):String;virtual;
- public
- constructor Create(AFilePath:string;ACipher:ICipher=nil);
- destructor Destroy;override;
- property FilePath:string read FFileName write FFileName;
- end;
- TEmailFile=class(TBaseEmailFile)
- private
- FSender,
- FRecver:string;
- FSubject:String;
- FDate:string;
- FSize:string;
- FAttchs:TStrings;
- FContent:string ;
- procedure SetAttchs(const Value: TStrings);
- public
- Constructor Create(AFilePath:string);
- destructor Destroy;override;
- procedure SaveEmail;
- procedure GetEmail;
- property Sender:string read FSender write FSender;
- property Recver:string read FRecver write FRecver;
- property Subject:String read FSubject write FSubject;
- property Date:string read FDate write FDate;
- property Size:string read FSize write FSize;
- property Attchs:TStrings read FAttchs write SetAttchs;
- property Content:string read FContent write FContent;
- end;
- implementation
- uses uCommon,Math;
- type
- TEmailRec=packed record
- Sender,
- Recver:string[50];
- Subject:String[255];
- Date:string[50];
- Size:string[20];
- Attchs:array[0..2000]of AnsiChar;
- Content:array[0..5000]of AnsiChar ;
- end;
- { TBaseEmailFile }
- constructor TBaseEmailFile.Create(AFilePath: string; ACipher: ICipher);
- begin
- inherited Create;
- FFileName:=AFilePath;
- FCipher:=ACipher;
- end;
- function TBaseEmailFile.DecodeString(S: String): String;
- begin
- if FCipher=nil then
- Result:=S
- else
- Result:=FCipher.DecodeString(S);
- end;
- destructor TBaseEmailFile.Destroy;
- begin
- inherited;
- end;
- function TBaseEmailFile.EncodeString(S: String): string;
- begin
- if FCipher=nil then
- Result:=S
- else
- Result:=FCipher.EncodeString(S);
- end;
- { TEmailFile }
- constructor TEmailFile.Create(AFilePath: string);
- begin
- inherited Create(AFilePath,nil);
- FAttchs:=TStringList.Create;
- end;
- destructor TEmailFile.Destroy;
- begin
- FAttchs.Free;
- inherited;
- end;
- procedure TEmailFile.GetEmail;
- var
- f:file of TEmailRec;
- Ret:TEmailRec;
- temp:string;
- begin
- FillChar(Ret,sizeof(TEMailRec),0);
- temp:=ChangeFileExt(FFileName,'.txt');
- RenameFile(FFileName,temp);
- AssignFile(f,temp);
- try
- Reset(f);
- read(f,Ret);
- finally
- CloseFile(f);
- end;
- RenameFile(temp,FFileName);
- FSender:=DecodeString(Ret.Sender);
- FRecver:=DecodeString(Ret.Recver);
- FSubject:=DecodeString(Ret.Subject);
- FDate:=DecodeString(Ret.Date);
- FSize:=DecodeString(Ret.Size);
- FAttchs.Clear;
- Split(';',DecodeString(StrPas(Ret.Attchs)),FAttchs);
- FContent:=DecodeString(StrPas(Ret.Content));
- end;
- procedure TEmailFile.SaveEmail;
- var
- f:file of TEmailRec;
- Rec:TEmailRec;
- temp:string;
- S:string;
- begin
- FillChar(Rec,sizeof(TEMailRec),0);
- Rec.Sender:=EncodeString(FSender);
- Rec.Recver:=EncodeString(FRecver);
- Rec.Subject:=EncodeString(FSubject);
- Rec.Date:=EncodeString(FDate);
- Rec.Size:=EncodeString(FSize);
- S:=EncodeString(Join(';',FAttchs));
- ZeroMemory(@Rec.Attchs[0],Length(Rec.Attchs));
- if Length(S)<>0 then
- CopyMemory(@Rec.Attchs[0],@S[1],Min(Length(Rec.Attchs),Length(S)));
- ZeroMemory(@Rec.Content[0],Length(Rec.Content));
- if Length(FContent)<>0 then
- CopyMemory(@Rec.Content[0],@FContent[1],Min(Length(Rec.Content),Length(FContent)));
- temp:=ChangeFileExt(FFileName,'.txt');
- AssignFile(f,temp);
- try
- Rewrite(f);
- Write(f,Rec);
- finally
- CloseFile(f);
- end;
- RenameFile(temp,FFileName);
- end;
- procedure TEmailFile.SetAttchs(const Value: TStrings);
- begin
- if (FAttchs <>Value) then
- FAttchs.Assign(Value);
- end;
- end.