📄 idmessage.pas
字号:
const
// 2001-Oct-29 Don Siders
// TODO: Move to IdResourceStrings.pas
RSIdMessageCannotLoad = 'Cannot load message from file %s'; {Do not Localize}
const
MessageFlags : array [mfAnswered..mfRecent] of String =
( '\Answered', {Do not Localize} //Message has been answered.
'\Flagged', {Do not Localize} //Message is "flagged" for urgent/special attention.
'\Deleted', {Do not Localize} //Message is "deleted" for removal by later EXPUNGE.
'\Draft', {Do not Localize} //Message has not completed composition (marked as a draft).
'\Seen', {Do not Localize} //Message has been read.
'\Recent' ); {Do not Localize} //Message is "recently" arrived in this mailbox.
implementation
uses
IdMessageCoderMIME, // Here so the 'MIME' in create will always suceed
IdGlobal, IdMessageCoder, IdResourceStrings, IdStream,
IdMessageClient, IdIOHandlerStream, IdStrings;
{ TIdMIMEBoundary }
procedure TIdMIMEBoundary.Clear;
begin
FBoundaryList.Clear;
end;
constructor TIdMIMEBoundary.Create;
begin
FBoundaryList := TStringList.Create;
end;
destructor TIdMIMEBoundary.Destroy;
begin
FBoundaryList.Free;
inherited Destroy;
end;
class function TIdMIMEBoundary.FindBoundary(AContentType: string): string;
var
s: string;
begin
// Store in s and not Result because of Fetch semantics
s := UpperCase(AContentType);
Fetch(s, 'BOUNDARY='); {do not localize}
if (Length(s) > 0) and (s[1] = '"') then begin {do not localize}
Delete(s, 1, 1);
Result := Fetch(s, '"'); {do not localize}
// Should never occur, and if so bigger problems but just in case we'll try
end else begin
Result := s;
end;
end;
function TIdMIMEBoundary.GetBoundary: string;
begin
if FBoundaryList.Count > 0 then begin
Result := FBoundaryList.Strings[0];
end else begin
Result := '';
end;
end;
procedure TIdMIMEBoundary.Pop;
begin
FBoundaryList.Delete(0);
end;
procedure TIdMIMEBoundary.Push(ABoundary: string);
begin
if (FBoundaryList.Count > 0) and (AnsiSameText(ABoundary, FBoundaryList.Strings[0])) then begin
FNewBoundary := True;
end else begin
if Length(ABoundary) > 0 then begin
FBoundaryList.Insert(0, ABoundary);
FNewBoundary := False;
end;
end;
end;
{ TIdMessagePart }
procedure TIdMessagePart.Assign(Source: TPersistent);
var
mp: TIdMessagePart;
begin
if ClassType <> Source.ClassType then begin
inherited;
end else begin
mp := TIdMessagePart(Source);
ContentTransfer := mp.ContentTransfer;
ContentType := mp.ContentType;
ExtraHeaders.Assign(mp.ExtraHeaders);
end;
end;
constructor TIdMessagePart.Create(Collection: TCollection);
begin
if ClassType = TIdMessagePart then begin
raise EIdCanNotCreateMessagePart.Create(RSTIdMessagePartCreate);
end;
inherited;
FIsEncoded := False;
FHeaders := TIdHeaderList.Create;
FExtraHeaders := TIdHeaderList.Create;
end;
destructor TIdMessagePart.Destroy;
begin
FHeaders.Free;
FExtraHeaders.Free;
inherited;
end;
function TIdMessagePart.GetContentTransfer: string;
begin
Result := Headers.Values['Content-Transfer-Encoding']; {do not localize}
end;
function TIdMessagePart.GetContentType: string;
begin
Result := Headers.Values['Content-Type']; {do not localize}
end;
procedure TIdMessagePart.SetContentTransfer(const Value: string);
begin
Headers.Values['Content-Transfer-Encoding'] := Value; {do not localize}
end;
procedure TIdMessagePart.SetContentType(const Value: string);
begin
Headers.Values['Content-Type'] := Value; {do not localize}
end;
procedure TIdMessagePart.SetExtraHeaders(const Value: TIdHeaderList);
begin
FExtraHeaders.Assign(Value);
end;
{ TIdAttachment }
procedure TIdAttachment.Assign(Source: TPersistent);
var
mp: TIdAttachment;
begin
if ClassType <> Source.ClassType then begin
inherited;
end else begin
mp := TIdAttachment(Source);
ContentTransfer := mp.ContentTransfer;
ContentType := mp.ContentType;
ExtraHeaders.Assign(mp.ExtraHeaders);
ContentDisposition := mp.ContentDisposition;
FileName := mp.FileName;
end;
end;
constructor TIdAttachment.Create(Collection: TIdMessageParts; const AFileName: TFileName = '');
begin
inherited Create(Collection);
FStoredPathname := AFileName;
FFilename := ExtractFilename(AFilename);
end;
destructor TIdAttachment.Destroy;
begin
if FileIsTempFile then begin
DeleteFile(Filename);
end;
inherited;
end;
procedure TIdAttachment.Encode(ADest: TStream);
begin
with TIdMessageEncoderInfo(TIdMessageParts(Collection).MessageEncoderInfo).MessageEncoderClass
.Create(nil) do try
Filename := Self.Filename;
Encode(Self.StoredPathname, ADest);
finally Free; end;
end;
function TIdAttachment.GetContentDisposition: string;
begin
Result := Headers.Values['Content-Disposition']; {do not localize}
end;
function TIdAttachment.SaveToFile(const FileName: TFileName): Boolean;
begin
Result := CopyFileTo(StoredPathname, FileName);
if not Result then begin
raise EIdException.Create(RSTIdMessageErrorSavingAttachment);
end;
end;
procedure TIdAttachment.SetContentDisposition(const Value: string);
begin
Headers.Values['Content-Disposition'] := Value; {do not localize}
end;
{ TIdText }
procedure TIdText.Assign(Source: TPersistent);
var mp : TIdText;
begin
if ClassType <> Source.ClassType then
begin
inherited;
end
else
begin
mp := TIdText(Source);
ContentTransfer := mp.ContentTransfer;
ContentType := mp.ContentType;
ExtraHeaders.Assign(mp.ExtraHeaders);
Body.Assign(mp.Body);
end;
end;
constructor TIdText.Create(Collection: TIdMessageParts; ABody: TStrings = nil);
begin
inherited Create(Collection);
FBody := TStringList.Create;
if ABody <> nil then begin
FBody.Assign(ABody);
end;
end;
destructor TIdText.Destroy;
begin
FBody.Free;
inherited;
end;
procedure TIdText.SetBody(const AStrs: TStrings);
begin
FBody.Assign(AStrs);
end;
{ TMessageParts }
function TIdMessageParts.Add: TIdMessagePart;
begin
// This helps prevent TIdMessagePart from being added
Result := nil;
end;
procedure TIdMessageParts.CountParts;
//TODO: Make AttCount, etc maintained on the fly
var
i: integer;
begin
FAttachmentCount := 0;
FRelatedPartCount := 0;
FTextPartCount := 0;
for i := 0 to Count - 1 do begin
if Items[i] is TIdText then begin
Inc(FTextPartCount)
end else if Items[i] is TIdAttachment then begin
if Length(Items[i].ExtraHeaders.Values['Content-ID']) > 0 then begin
Inc(FRelatedPartCount);
end;
Inc(FAttachmentCount);
end;
end;
// if TextPartCount = 1 then begin
// raise EIdTextInvalidCount.Create(RSTIdTextInvalidCount);
// end;
end;
constructor TIdMessageParts.Create(AOwner: TPersistent);
begin
inherited Create(AOwner, TIdMessagePart);
// Must set prop and not variable so it will initialize it
AttachmentEncoding := 'MIME';
end;
function TIdMessageParts.GetItem(Index: Integer): TIdMessagePart;
begin
Result := TIdMessagePart(inherited GetItem(Index));
end;
procedure TIdMessageParts.SetAttachmentEncoding(const AValue: string);
begin
FMessageEncoderInfo := TIdMessageEncoderList.ByName(AValue);
FAttachmentEncoding := AValue;
end;
procedure TIdMessageParts.SetItem(Index: Integer; const Value: TIdMessagePart);
begin
inherited SetItem(Index, Value);
end;
{ TIdMessage }
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 := '';
ContentDisposition := '';
FSender.Text := '';
Headers.Clear;
ExtraHeaders.Clear;
FMIMEBoundary.Clear;
UseNowForDate := ID_MSG_USENOWFORDATE;
Flags := [];
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;
FMIMEBoundary := TIdMIMEBoundary.Create;
Clear;
FEncoding := meMIME;
end;
destructor TIdMessage.Destroy;
begin
FBody.Free;
FRecipients.Free;
FBccList.Free;
FCcList.Free;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -