How to extract/add MIME headers in E-mail messages and HTTP requests

1. 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;
 
2. 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;

Add Feedback