Parsing an Email header

Hello, I want to Parse the Date from a header, so I need a parser which looks for ";" in the header and Save the Value after ";" as a String.

Here is a header;:
Return-Path: <>
Received: from ( [])
      by (8.12.8/8.12.8) with ESMTP id h9OFOduM018338
      for <>; Fri, 24 Oct 2003 17:24:39 +0200
Received: from (localhost.localdomain [])
      by (8.12.8/8.12.8) with ESMTP id h9OFOcag028455
      for <>; Fri, 24 Oct 2003 17:24:38 +0200
Received: from ([])
      by (MailMonitor for SMTP v1.2.2 ) ;
      Fri, 24 Oct 2003 17:24:38 +0200 (CEST)
Received: from ( [])
      by (8.12.8/8.12.8) with ESMTP id h9OFOcAO010270
      (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO)
      for <>; Fri, 24 Oct 2003 17:24:38 +0200
Received: (from wwwrun@localhost)
      by (8.11.3/8.11.3/SuSE Linux 8.11.1-0.5) id h9OFOce24381;
      Fri, 24 Oct 2003 17:24:38 +0200
Date: Sat, 30 Dec 1899 01:00:00 +0100
Message-Id: <>
Subject: test
Content-Type: text/html; charset=windows-1254
From: "Arash Mahini" <>
X-Mailer: System33r
X-Spam-Status: No, hits=4.2 required=5.0 tests=FORGED_YAHOO_RCVD,HTML_MESSAGE,
      autolearn=no version=2.60
X-Spam-Level: ****
X-Spam-Checker-Version: SpamAssassin 2.60 (1.212-2003-09-23-exp) on
In-Reply-To: test
I want to get "Fri, 24 Oct 2003 17:24:39 +0200" in a String (Or TDateTime).

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wim ten BrinkSelf-employed developerCommented:
The Indy components have a component called TidMessage. If you load this source into such a component then the properties allow you access to most of these fields.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
My problem is the TIdMessage, I cannot get the right Date of message with TIdmessage, it changes the date to "Now"!!
Wim ten BrinkSelf-employed developerCommented:
And what if you use TIdMessage.Headers.Values['Date'] instead of the Date property? The Date property is not the same as the Date value in the header.
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Eddie ShipmanAll-around developerCommented:
The problem with the appraach you are taking in searching for a ';' is that other headers may and do have that character in them.

What you are trying to parse, from what I can see, is the Received headers.

Even TidMessage doesn't parse the headers of an email like we would like. It just places them into a stringlist.

What you can do is to write a handler to take the headers, look for the word "Received:" . Something a little like this using
the NameValue Pairs on the headers:

        idPop31.Retrieve(i, msg);
        msg.Headers.NameValueSeparator := ':';
        if Length(msg.Headers.Text) > 0 then
          for x := 0 to msg.Headers.Count-1 do
            s := msg.Headers.Names[x];
            if s = 'Received' then
              s := msg.Headers.Values[s];
              iPos := Pos(';', s);
              s := Trim(Copy(s, iPos+1, Length(s)));
               // s now contains the date from the received header

Its too bad that there isn't a standard that these haders should be formatted. That is one beef I have with W3C because it makes
fighting spam a lot more difficult.

Eddie ShipmanAll-around developerCommented:
But, your header may have multiple Received haeders and you would get a date for each one.
If you really are looking for just the Date header, then Workshop_Alex had the right answer

But then again, you asked to get the "Fri, 24 Oct 2003 17:24:39 +0200" datetime and it is in
your Received header....

k4hvd66Author Commented:
what do you mean to this:
procedure TForm1.Parse(S : String; var List : TStringList);
L : TstringList;
I : Integer;
 L := TstringList.Create; // use another TStringList to parse source string on subelements
 L.Text :=S;

 If L.Count > 0 Then
    If List = Nil Then List := TStringList.Create Else List.Clear;
    For I := 0 To L.Count-1 Do
      List.Values[Copy(Trim(L[I]),1,Pos(';',Trim(L[I]))-1)] := Trim(Copy(Trim(L[I]),Pos(';',Trim(L[I]))+1,Length(Trim(L[I]))));


procedure TForm1.Button1Click(Sender: TObject);
VAR S:STring; L1:TStringList;
L1 :=TStringList.Create;
Parse (Memo1.Lines.Text, L1);
ShowMessage('Date: '+L1.Values['']);


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.