📄 idmessage.pas
字号:
unit IdMessage;
{
2000-Jul-25 Hadi Hariri
- Added support for MBCS
2000-Jun-10 Pete Mee
- Fixed some minor but annoying bugs.
2000-May-06 Pete Mee
- Added coder support directly into TIdMessage.
}
{ TODO : Moved Decode/Encode out and will add later,. Maybe TIdMessageEncode, Decode?? }
{ TODO : Support any header in TMessagePart }
{ DESIGN NOTE: The TIdMessage has an fBody which should only ever be the
raw message. TIdMessage.fBody is only raw if TIdMessage.fIsEncoded = true
The component parts are thus possibly made up of the following
order of TMessagePart entries:
MP[0] : Possible prologue text (fBoundary is '')
MP[0 or 1 - depending on prologue existence] :
fBoundary = boundary parameter from Content-Type
MP[next...] : various parts with or without fBoundary = ''
MP[MP.Count - 1] : Possible epilogue text (fBoundary is '')
}
{ DESIGN NOTE: If TMessagePart.fIsEncoded = True, then TMessagePart.fBody
is the encoded raw message part. Otherwise, it is the (decoded) text.
}
interface
uses
Classes,
IdBaseComponent, IdException, IdEMailAddress, IdHeaderList,
SysUtils;
type
TIdMessagePriority = (mpHighest, mpHigh, mpNormal, mpLow, mpLowest);
const
ID_MSG_NODECODE = False;
ID_MSG_USENOWFORDATE = True;
ID_MSG_PRIORITY = mpNormal;
type
TOnGetMessagePartStream = procedure(AStream: TStream) of object;
TIdMessagePart = class(TCollectionItem)
protected
FBoundary: string;
FBoundaryBegin: Boolean;
FBoundaryEnd: Boolean;
FContentMD5: string;
FContentTransfer: string;
FContentType: string;
FEndBoundary: string;
FExtraHeaders: TIdHeaderList;
FHeaders: TIdHeaderList;
FIsEncoded: Boolean;
FOnGetMessagePartStream: TOnGetMessagePartStream;
FStoredPathName: TFileName;
//
function GetContentType: string;
function GetContentTransfer: string;
procedure SetContentType(const Value: string);
procedure SetContentTransfer(const Value: string);
procedure SetExtraHeaders(const Value: TIdHeaderList);
public
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
//
property Boundary : String read FBoundary write FBoundary;
property BoundaryBegin : Boolean read FBoundaryBegin write FBoundaryBegin;
property BoundaryEnd : Boolean read FBoundaryEnd write FBoundaryEnd;
property IsEncoded : Boolean read fIsEncoded;
property OnGetMessagePartStream: TOnGetMessagePartStream read FOnGetMessagePartStream
write FOnGetMessagePartStream;
property StoredPathName: TFileName read FStoredPathName write FStoredPathName;
property Headers: TIdHeaderList read FHeaders;
published
property ContentTransfer: string read GetContentTransfer write SetContentTransfer;
property ContentType: string read GetContentType write SetContentType;
property ExtraHeaders: TIdHeaderList read FExtraHeaders write SetExtraHeaders;
end;
TIdMessagePartClass = class of TIdMessagePart;
TIdMessageParts = class;
TIdAttachment = class(TIdMessagePart)
protected
FContentDisposition: string;
FFileIsTempFile: boolean;
FFileName: TFileName;
//
function GetContentDisposition: string;
procedure SetContentDisposition(const Value: string);
public
procedure Assign(Source: TPersistent); override;
constructor Create(Collection: TIdMessageParts; const AFileName: TFileName = ''); reintroduce;
destructor Destroy; override;
procedure Encode(ADest: TStream);
function SaveToFile(const FileName: TFileName): Boolean;
//
property ContentDisposition: string read GetContentDisposition write SetContentDisposition;
property FileIsTempFile: boolean read FFileIsTempFile write FFileIsTempFile;
property FileName: TFileName read FFileName write FFileName;
end;
TIdText = class(TIdMessagePart)
protected
FBody: TStrings;
procedure SetBody(const AStrs : TStrings);
public
constructor Create(Collection: TIdMessageParts; ABody: TStrings = nil); reintroduce;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
//
property Body: TStrings read FBody write SetBody;
end;
TIdMessageParts = class(TOwnedCollection)
protected
FAttachmentEncoding: string;
FAttachmentCount: integer;
FMessageEncoderInfo: TObject;
FRelatedPartCount: integer;
FTextPartCount: integer;
//
function GetItem(Index: Integer): TIdMessagePart;
procedure SetAttachmentEncoding(const AValue: string);
procedure SetItem(Index: Integer; const Value: TIdMessagePart);
public
function Add: TIdMessagePart;
procedure CountParts;
constructor Create(AOwner: TPersistent); reintroduce;
//
property AttachmentCount: integer read FAttachmentCount;
property AttachmentEncoding: string read FAttachmentEncoding write SetAttachmentEncoding;
property Items[Index: Integer]: TIdMessagePart read GetItem write SetItem; default;
property MessageEncoderInfo: TObject read FMessageEncoderInfo;
property RelatedPartCount: integer read FRelatedPartCount;
property TextPartCount: integer read FTextPartCount;
end;
TIdMessage = class(TIdBaseComponent)
protected
FBccList: TIdEmailAddressList;
FBody: TStrings;
FCharSet: string;
FCcList: TIdEmailAddressList;
FContentType: string;
FContentTransferEncoding: string;
FDate: TDateTime;
FMsgSize: longInt;
FIsEncoded : Boolean;
FExtraHeaders: TIdHeaderList;
FFrom: TIdEmailAddressItem;
FHeaders: TIdHeaderList;
FMessageParts: TIdMessageParts;
FMIMEBoundary: string;
FMsgId: string;
FNewsGroups: TStrings;
FNoDecode: Boolean;
FOrganization: string;
FPriority: TIdMessagePriority;
FSubject: string;
FReceiptRecipient: TIdEmailAddressItem;
FRecipients: TIdEmailAddressList;
FReferences: string;
FReplyTo: TIdEmailAddressList;
FSender: TIdEMailAddressItem;
FXProgram: string;
//
function GetAttachmentEncoding: string;
procedure SetAttachmentEncoding(const AValue: string);
public
procedure AddHeader(const Value: string);
procedure Clear; virtual;
procedure ClearBody;
procedure ClearHeader;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function GenerateHeader: TIdHeaderList;
function GetUseNowForDate: Boolean;
procedure ProcessHeaders;
procedure SetBody(const Value: TStrings);
procedure SetNewsGroups(const Value: TStrings);
procedure SetExtraHeaders(const Value: TIdHeaderList);
procedure SetUseNowForDate(const Value: Boolean);
//
property MsgSize: longint read FMsgSize write FMsgSize;
procedure SaveToText(fname: string);
procedure LoadFromText(fname: string);
//
property IsEncoded : Boolean read fIsEncoded write fIsEncoded;
property MsgId: string read FMsgId write FMsgId;
property Headers: TIdHeaderList read FHeaders;
property MessageParts: TIdMessageParts read FMessageParts;
published
//TODO: Make a property editor which drops down the registered coder types
property AttachmentEncoding: string read GetAttachmentEncoding write SetAttachmentEncoding;
property Body: TStrings read FBody write SetBody;
property BccList: TIdEmailAddressList read FBccList write FBccList;
property CharSet: string read FCharSet write FCharSet;
property CCList: TIdEmailAddressList read FCcList write FCcList;
property ContentType: string read FContentType write FContentType;
property ContentTransferEncoding: string read FContentTransferEncoding
write FContentTransferEncoding;
property Date: TDateTime read FDate write FDate;
//
property ExtraHeaders: TIdHeaderList read FExtraHeaders write SetExtraHeaders;
property From: TIdEmailAddressItem read FFrom write FFrom;
property MIMEBoundary: string read FMIMEBoundary write FMIMEBoundary;
property NewsGroups: TStrings read FNewsGroups write SetNewsGroups;
property NoDecode: Boolean read FNoDecode write FNoDecode default ID_MSG_NODECODE;
property Organization: string read FOrganization write FOrganization;
property Priority: TIdMessagePriority read FPriority write FPriority default ID_MSG_PRIORITY;
property ReceiptRecipient: TIdEmailAddressItem read FReceiptRecipient write FReceiptRecipient;
property Recipients: TIdEmailAddressList read FRecipients write FRecipients;
property References: string read FReferences write FReferences;
property ReplyTo: TIdEmailAddressList read FReplyTo write FReplyTo;
property Subject: string read FSubject write FSubject;
property Sender: TIdEmailAddressItem read FSender write FSender;
property UseNowForDate: Boolean read GetUseNowForDate write SetUseNowForDate default ID_MSG_USENOWFORDATE;
end;
TIdMessageEvent = procedure(ASender : TComponent; var AMsg : TIdMessage)
of object;
TIdStringMessageEvent = procedure(ASender : TComponent;
const AString : String; var AMsg : TIdMessage) of object;
EIdMessageException = class(EIdException);
EIdCanNotCreateMessagePart = class(EIdMessageException);
EIdTextInvalidCount = class(EIdMessageException);
implementation
uses
IdMessageCoderMime, // Here so the 'MIME' in create will always suceed
IdGlobal, IdCoderHeader, IdMessageCoder, IdResourceStrings, IdStream;
{ TIdMessage }
procedure TIdMessage.LoadFromText(fname: string);
var stLine: string;
f: textfile;
begin
if FileExists(fname) then
begin
Clear; // clear header and body
AssignFile(f, fname);
try
Reset(f);
repeat // read in headers
readln(f, stline);
if stline <> '' then
Headers.Add(stLine)
until eof(f) or (stLine = '');
repeat // read in body
readln(f, stline);
Body.Add(stLine)
until eof(f);
ProcessHeaders; // put header into variables
MsgSize := FileSize(f);
finally
closefile(f);
end;
end;
end;
procedure TIdMessage.SaveToText(fname: string);
var i: Integer;
f: textfile;
ofile : String;
begin
ofile := fname;
// Save the message into file
if FileExists(fname) then DeleteFile(fname);
AssignFile(f, fname); //save it all to a text file
try
Rewrite(f); //create new file
for i := 0 to Pred(Headers.count) do //save headers
writeln(f, Headers.strings[i]);
for i := 0 to Pred(Body.Count) do
writeln(f, Body.strings[i]); //save body text
finally
flush(f);
closefile(f);
end;
end;
procedure TIdMessage.AddHeader(const Value: string);
begin
FHeaders.Add(Value);
end;
procedure TIdMessage.Clear;
begin
ClearHeader;
ClearBody;
end;
procedure TIdMessage.ClearBody;
begin
MessageParts.Clear ;
Body.Clear;
end;
procedure TIdMessage.ClearHeader;
begin
CcList.Clear;
BccList.Clear;
Date := 0;
From.Text := '';
NewsGroups.Clear;
Organization := '';
References := '';
ReplyTo.Clear;
Subject := '';
Recipients.Clear;
Priority := ID_MSG_PRIORITY;
ReceiptRecipient.Text := '';
ContentType := '';
CharSet := '';
ContentTransferEncoding := '';
FSender.Text := '';
Headers.Clear;
ExtraHeaders.Clear;
UseNowForDate := ID_MSG_USENOWFORDATE;
end;
constructor TIdMessage.Create(AOwner: TComponent);
begin
inherited;
FBody := TStringList.Create;
FRecipients := TIdEmailAddressList.Create(Self);
FBccList := TIdEmailAddressList.Create(Self);
FCcList := TIdEmailAddressList.Create(Self);
FMessageParts := TIdMessageParts.Create(Self);
FNewsGroups := TStringList.Create;
FHeaders := TIdHeaderList.Create;
FFrom := TIdEmailAddressItem.Create(nil);
FReplyTo := TIdEmailAddressList.Create(Self);
FSender := TIdEmailAddressItem.Create(nil);
FExtraHeaders := TIdHeaderList.Create;
FReceiptRecipient := TIdEmailAddressItem.Create(nil);
NoDecode := ID_MSG_NODECODE;
Clear;
end;
destructor TIdMessage.Destroy;
begin
FBody.Free;
FRecipients.Free;
FBccList.Free;
FCcList.Free;
FMessageParts.Free;
FNewsGroups.Free;
FHeaders.Free;
FExtraHeaders.Free;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -