How to modify a subject line of the email within the raw .eml source

 
 
The code below loads an email message from an .eml file, parses the MIME message header, extracts and replaces the subject line, and finally, sends the message using the SMTP component.
The other MIME header fields can be modified using the introduced approach.
 
See also:
 
var
  fieldList: TclMailHeaderFieldList;
  ind: Integer;
  subject: string;
begin
  //load a message in a raw format.
  clSmtp1.MailData.LoadFromFile('mailmessage.eml');

  //set up the TclSmtp component
  clSmtp1.Server := 'mail.example.com';
  clSmtp1.UserName := 'user';
  clSmtp1.Password := 'secret';

  //specify the desired character set and MIME encoding for the Subject header field
  fieldList := TclMailHeaderFieldList.Create('utf-8', cmQuotedPrintable, DefaultCharsPerLine);
  try
    //parse the mail header
    fieldList.Parse(0, clSmtp1.MailData);

    //get an index of the Subject field
    ind := fieldList.GetFieldIndex('Subject');

    //get the Subject field value
    subject := fieldList.GetDecodedFieldValue(ind);

    //modify the subject line
    subject := subject + ' modified...';

    //remove the subject field from the clSmtp1.MailData string list
    fieldList.RemoveField(ind);

    //add an updated subject line
    fieldList.AddEncodedField('Subject', subject);

    //starting from now, the clSmtp1.MailData property contains the updated Subject field
  finally
    fieldList.Free();
  end;

  //send the message
  clSmtp1.Send();
end;

Add Feedback