📄 idmessage.pas
字号:
FFrom.Free;
FReplyTo.Free;
FSender.Free;
FReceiptRecipient.Free;
inherited destroy;
end;
procedure TIdMessage.SetBody(const Value: TStrings);
begin
FBody.Assign(Value);
end;
procedure TIdMessage.SetNewsGroups(const Value: TStrings);
begin
FNewsgroups.Assign(Value);
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 {do not localize}
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;
procedure TIdMessage.ProcessHeaders;
var
s: string;
// Some mailers send priority as text, number or combination of both
function GetMsgPriority(Priority:string): TIdMessagePriority;
var
s: string;
Num: integer;
begin
// This is for Pegasus.
if IndyPos('urgent', LowerCase(Priority)) <> 0 then begin {do not localize}
Result := mpHigh;
end else if IndyPos('non-priority', LowerCase(Priority)) <> 0 then begin {do not localize}
Result := mpLow;
end else begin
s := Trim(Priority);
s := Trim(Fetch(s, ' '));
Num := StrToIntDef(s, 3);
Result := TIdMessagePriority(Num-1);
end;
end;
begin
//TODO: Addresses: should be decodeaddress and not decodeheader
// Fill the properties
ContentType := Headers.Values['Content-Type']; {do not localize}
ContentTransferEncoding := Headers.Values['Content-Transfer-Encoding']; {do not localize}
Subject := DecodeHeader(Headers.Values['Subject']); {do not localize}
From.Text := DecodeHeader(Headers.Values['From']); {do not localize}
MsgId := Headers.Values['Message-Id']; {do not localize}
CommaSeperatedToStringList(Newsgroups, Headers.Values['Newsgroups']); {do not localize}
Recipients.EMailAddresses := DecodeHeader(Headers.Values['To']); {do not localize}
CCList.EMailAddresses := DecodeHeader(Headers.Values['Cc']); {do not localize}
Organization := Headers.Values['Organization']; {do not localize}
ReceiptRecipient.Text := Headers.Values['Disposition-Notification-To']; {do not localize}
if Length(ReceiptRecipient.Text) = 0 then begin
ReceiptRecipient.Text := Headers.Values['Return-Receipt-To']; {do not localize}
end;
References := Headers.Values['References']; {do not localize}
ReplyTo.EmailAddresses := Headers.Values['Reply-To']; {do not localize}
Date := GMTToLocalDateTime(Headers.Values['Date']); {do not localize}
Sender.Text := Headers.Values['Sender']; {do not localize}
if Length(Headers.Values['Priority']) = 0 then begin {do not localize}
Priority := GetMsgPriority(Headers.Values['X-Priority']) {do not localize}
end else begin
Priority := GetMsgPriority(Headers.Values['Priority']); {do not localize}
end;
// Get MIME Boundary
// TODO: Improve this parsing of MimeBoundary
s := ContentType;
Fetch(s, 'boundary='); {do not localize}
Delete(s, 1, 1); // Delete "
FMIMEBoundary := Fetch(s, '"');
end;
procedure TIdMessage.SetExtraHeaders(const Value: TIdHeaderList);
begin
FExtraHeaders.Assign(Value);
end;
function TIdMessage.GetUseNowForDate: Boolean;
begin
Result := FDate = 0;
end;
procedure TIdMessage.SetUseNowForDate(const Value: Boolean);
begin
Date := 0;
end;
procedure TIdMessage.SetAttachmentEncoding(const AValue: string);
begin
MessageParts.AttachmentEncoding := AValue;
end;
function TIdMessage.GetAttachmentEncoding: string;
begin
Result := MessageParts.AttachmentEncoding;
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 TIdMessage.GenerateHeader: TIdHeaderList;
var
MimeCharset: string;
HeaderEncoding: Char;
TransferHeader: TTransfer;
begin
MessageParts.CountParts;
TIdMessageEncoderInfo(MessageParts.MessageEncoderInfo).InitializeHeaders(Self);
if Length(ContentType) = 0 then begin
ContentType := CharSet;
end else begin
ContentType := ContentType + ';' + CharSet;
end;
InitializeMime(TransferHeader, HeaderEncoding, MimeCharSet);
Result := TIdHeaderList.Create; try
with Result do begin
Values['From'] := EncodeAddressItem(From, HeaderEncoding, TransferHeader, MimeCharSet); {do not localize}
Values['Subject'] := EncodeHeader(Subject, [], HeaderEncoding, TransferHeader, {do not localize}
MimeCharSet);
Values['To'] := EncodeAddress(Recipients, HeaderEncoding, TransferHeader, MimeCharSet); {do not localize}
Values['Cc'] := EncodeAddress(CCList, HeaderEncoding, TransferHeader, MimeCharSet); {do not localize}
Values['Newsgroups'] := NewsGroups.CommaText; {do not localize}
Values['Content-Type'] := ContentType; {do not localize}
if MessageParts.Count > 0 then begin
Values['MIME-Version'] := '1.0';
end;
{ TODO : Add charset? }
Values['Content-Transfer-Encoding'] := ContentTransferEncoding; {do not localize}
Values['Sender'] := Sender.Text; {do not localize}
Values['Reply-To'] := EncodeAddress(ReplyTo, HeaderEncoding, TransferHeader, {do not localize}
MimeCharSet);
Values['Organization'] := EncodeHeader(Organization, [], HeaderEncoding, {do not localize}
TransferHeader, MimeCharSet);
Values['Disposition-Notification-To'] := EncodeAddressItem(ReceiptRecipient, {do not localize}
HeaderEncoding, TransferHeader, MimeCharSet);
Values['References'] := References; {do not localize}
if UseNowForDate then begin
Values['Date'] := DateTimeToInternetStr(Now); {do not localize}
end else begin
Values['Date'] := DateTimeToInternetStr(Self.Date); {do not localize}
end;
Values['X-Priority'] := IntToStr(Ord(Priority) + 1); {do not localize}
Values['X-Library'] := gsIdProductName + ' ' + gsIdVersion ; {do not localize}
// Add the extra-headers
AddStrings(ExtraHeaders);
end;
except
FreeAndNil(Result);
raise;
end;
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;
initialization
RegisterClasses([TIdAttachment, TIdText]);
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -