1. The most known MIME headers are stored in corresponding component properties TclMailMessage. For the TclMailMessage component: TclMailMessage.Subject, TclMailMessage.ContentType, etc.
Custom headers can be specified/obtained using the TclMailMessage.ExtraFields property. This property contains header names together with their values using the MIME format: "Custom-Field: Value".
You can extract or add new headers using the TclHeaderFieldList class. Both known and custom header fields for message bodies are stored similar to the TclMaiMessage headers: TclMailMessage.Bodies[0].ContentType and TclMailMessage.Bodies[0].ExtraFields.
//Add a new custom field
procedure TForm1.Button1Click(Sender: TObject);
var
fieldList: TclHeaderFieldList;
begin
clMailMessage1.ExtraFields.Add('Custom-Field: Value');
//or
fieldList := TclHeaderFieldList.Create();
try
fieldList.Parse(0, clMailMessage1.ExtraFields);
fieldList.AddFieldIfNotExist('Custom-Field', 'Value');
finally
fieldList.Free();
end;
end;
//Get a custom field value
procedure TForm1.Button1Click(Sender: TObject);
var
fieldList: TclHeaderFieldList;
value: string;
begin
fieldList := TclHeaderFieldList.Create();
try
fieldList.Parse(0, clMailMessage1.ExtraFields);
value := fieldList.GetFieldValue('Custom-Field');
finally
fieldList.Free();
end;
end;
2. If you want to set up request headers for the TclHttp component, you need first assign an instance of the TclHttpRequest component for the TclHttp.Request property. The standard Request headers can be accessed using the TclHttpRequest.Header property: TclHttpRequest.Header.Accept, TclHttpRequest.Header.Referer, TclHttpRequest.Header.ContentType. Response headers can be obtained using the TclHttp.ResponseHeader property: TclHttp.ResponseHeader.ContentType, TclHttp.ResponseHeader.ETag, etc.
Custom headers can be read/modified using the TclHttpRequest.Header.ExtraFields and TclHttp.ResponseHeader.ExtraFields properties. The format for these properties is the same as described above: "Custom-Field: Value".
//Add a new custom request field
procedure TForm1.Button1Click(Sender: TObject);
var
fieldList: TclHeaderFieldList;
begin
fieldList := TclHeaderFieldList.Create();
try
clHttp1.Request := clHttpRequest1;
clHttpRequest1.Header.ExtraFields.Add('Custom-Field: Value');
//or
clHttp1.Request := clHttpRequest1;
fieldList.Parse(0, clHttpRequest1.Header.ExtraFields);
fieldList.AddFieldIfNotExist('Custom-Field', 'Value');
finally
fieldList.Free();
end;
end;
//Get a response header field
procedure TForm1.Button1Click(Sender: TObject);
var
fieldList: TclHeaderFieldList;
value: string;
begin
fieldList := TclHeaderFieldList.Create();
try
fieldList.Parse(0, clHttp1.ResponseHeader.ExtraFields);
value := fieldList.GetFieldValue('Custom-Field');
finally
fieldList.Free();
end;
end;
The examples below describe how to use the TclHeaderFieldList class to get/add/modify/delete header fields.
3. Clever Internet Suite v 9.0 and higher
procedure TForm1.Button1Click(Sender: TObject);
var
rawHeader: TStrings;
fieldList: TclHeaderFieldList;
s: string;
begin
rawHeader := TStringList.Create();
fieldList := TclHeaderFieldList.Create();
try
//you need to call the Parse method first
fieldList.Parse(0, rawHeader);
//you can specify own formatting options, if you need
fieldList.CharsPerLine := 50;
fieldList.ItemDelimiter := ',';
//adding fields
fieldList.AddField('Name1', 'val1');
fieldList.AddField('Name2', 'val2');
fieldList.AddField('Name3', 'val3');
//adding a field item to the Name2 header field
fieldList.AddQuotedFieldItem('Name2', 'item1', 'item-value');
//you can easily remove a field, or an item
fieldList.RemoveFieldItem('Name2', 'item1');
fieldList.RemoveField('Name2');
//you can add raw headers fields from a string list
//fieldList.AddFields(strings);
//you can get a field value by field name or by index
s := fieldList.GetFieldValue('Name1');
s := fieldList.GetFieldValue(0);
//a list of parsed fields is available at
//fieldList.FieldList;
//you can even get a field source
//fieldList.GetFieldSource('Name1', strings);
//simple adds an empty line to the raw header source
fieldList.AddEndOfHeader();
//see the rawHeader content
finally
fieldList.Free();
rawHeader.Free();
end;
end;
4. Previous versions of Clever Internet Suite
procedure TForm1.Button2Click(Sender: TObject);
var
rawHeader: TStrings;
fieldList: TclHeaderFieldList;
begin
//v8.3 and lower
//AddHeaderField(rawHeader, 'FieldName', 'Value');
//v8.4 and higher
fieldList := TclHeaderFieldList.Create();
try
fieldList.Parse(0, rawHeader);
fieldList.AddField('FieldName', 'Value');
finally
fieldList.Free();
end;
end;