ivan
  • ivan
  • 100% (Exalted)
  • Administration Topic Starter
14 years ago
Delphi Example
After you received the emails from email server to the local folder, we can use the following code to parse the email now. The following code demonstrates how to parse from, to, cc, subject, body text, attachments of specified EML file.


// The following example codes demonstrate parsing email (*.EML) file
// To get full sample projects, please download and install EAGetMail on your machine.
// To run it correctly, please change email server, user, password, folder, file name value to yours

Unit Unit1; 

Interface 

Uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls, EAGetMailObjLib_TLB; 

Type 
  TForm1 = Class(TForm) 
    Button1: TButton; 
    Procedure Button1Click(Sender: TObject); 
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  End; 

Const 
  MailServerPop3 = 0; 
  MailServerImap4 = 1; 

Var 
  Form1: TForm1; 

Implementation 

{$R *.dfm} 

Procedure TForm1.ParseEmail( fileName: WideString ); 
Var 
  oMail: TMail; 
  i, UBound: Integer; 
  addrs, atts: OleVariant; 
  addr: IMailAddress; 
  att: IAttachment; 
Begin 
  oMail := TMail.Create(Application); 
  oMail.LicenseCode := 'TryIt'; 
  oMail.LoadFile(fileName, false); 

  // Parse email sender
  ShowMessage( 'From: ' + oMail.From.Address ); 

  // Parse email to recipients
  addrs := oMail.ToAddr; 
  UBound := VarArrayHighBound( addrs, 1 ); 
  For i := 0 To UBound Do 
  Begin 
    addr := IDispatch(VarArrayGet(addrs, i)) As IMailAddress; 
    ShowMessage( 'To: ' + addr.Address ); 
  End; 

  // Parse email cc recipients
  addrs := oMail.Cc; 
  UBound := VarArrayHighBound( addrs, 1 ); 
  For i := 0 To UBound Do 
  Begin 
    addr := IDispatch(VarArrayGet(addrs, i)) As IMailAddress; 
    ShowMessage( 'Cc: ' + addr.Address ); 
  End; 

  // Parse email subject
  ShowMessage( 'Subject: ' + oMail.Subject ); 

  // Parse email text body
  ShowMessage( 'Text body: ' + oMail.TextBody ); 

  // Parse email HTML body
  ShowMessage( 'HTML body: ' + oMail.HtmlBody ); 

  // Parse attachment
  atts := oMail.Attachments; 
  UBound := VarArrayHighBound( atts, 1 ); 
  For i := 0 To UBound Do 
  Begin 
    att := IDispatch(VarArrayGet(atts,i)) As IAttachment; 
    ShowMessage( att.Name ); 
  End; 

End; 

Procedure TForm1.Button1Click(Sender: TObject); 
Begin 
  Try 
      ParseEmail('c:\my folder\test.eml'); 
  Except 
    On ep:Exception Do 
      ShowMessage( 'Error: ' + ep.Message ); 
  End; 

End; 

End. 
Click here to read original topic - full version ... 

If you have any comments or questions about above example codes, please add your comments here.

EXPLORE TUTORIALS

© All Rights Reserved, AIFEI Software Limited & AdminSystem Software Limited.