faqts : Computers : Programming : Languages : Delphi

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

39 of 48 people (81%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

Delphi: Internet: E-mail: Pop3: How to possibly get your e-mail message information using Delphi?

Jul 13th, 2003 09:19
Knud van Eeden,


Internet: E-mail: POP3: Delphi: How to possibly get your e-mail 
message information using Delphi?

---

see also: http://www.howstuffworks.com/email.htm

---

This has been tested and worked OK (counts e-mails and downloads
some information) on Delphi 7 

---

Steps: Overview:

On a form put:

 1. POP3 client component (Indy tab palette)

 2. IdMessage component (Indy miscellaneous palette)

 3. Button component (standard palette)

 4. Memo component (standard palette)

 5. Double click on the 'POP3 client' component and after that the 
button
    component, and copy/paste inside the following text, as further 
shown below

 6. Then change in that source code your POP3 information

     IdPOP31.Host := '<your POP3 mail server at your Internet service 
provider>'; // for example: 'pop.vip.sc5.yahoo.com' (if you are using 
Yahoo)

     IdPOP31.Username := '<your POP3 username at your Internet service 
provider>'; // for example: 'myname'

     IdPOP31.Password := '<your POP3 password at your Internet service 
provider>'; // for example: 'mypassword'

    for one or more service providers you want to download from

 7. If you then compile this program and click on the button, it will 
    download e-mail information (e.g. count the total of e-mail    
present and get their
    header, body, attachment (not implemented further) information)

--- cut here ----------------------------------------------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, 
Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdMessageClient, IdPOP3, StdCtrls, IdMessage;

type
  TForm1 = class(TForm)
    IdPOP31: TIdPOP3;
    Button1: TButton;
    IdMessage1: TIdMessage;
    Memo1: TMemo;
    procedure IdPOP31Connected(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.IdPOP31Connected(Sender: TObject);
begin
 IdPOP31.Host := 'mail.myprovider1.com';
 IdPOP31.Port := 110;
 IdPOP31.Username := 'john_doe';
 IdPOP31.Password := 'mypop3passwordatmyprovider1'; // type your 
password (but remember, this is a security breach)
//
 IdPOP31.Host := 'mail.myprovider2.com';
 IdPOP31.Port := 110;
 IdPOP31.Username := 'john.doe';
 IdPOP31.Password := 'mypop3passwordatmyprovider2';
//
 IdPOP31.Host := 'pop.vip.sc5.yahoo.com';
 IdPOP31.Port := 110;
 IdPOP31.Username := 'john_doe';
 IdPOP31.Password := 'mypop3passwordforyahoo';
end;

procedure TForm1.Button1Click(Sender: TObject);
var pop3mailcounttotalI : integer;
var pop3mailboxsizetotalI : integer;
var I : integer;
var J : integer;
var K : integer;
var bodyS : TStringList;
var s : string;
var Msg: TIdMessage;
begin
 bodyS := TStringList.Create;
 IdPOP31.Connect;
 pop3mailcounttotalI := IdPOP31.CheckMessages;
 pop3mailboxsizetotalI := IdPOP31.RetrieveMailBoxSize div 1024;
 ShowMessage( 'Mail total =' + ' ' + IntToStr( pop3mailcounttotalI ) );
 ShowMessage( 'Mailbox size total =' + ' ' + IntToStr( 
pop3mailboxsizetotalI ) + ' ' + 'Kilobytes' );
 for I := 1 to pop3mailcounttotalI do begin
  ShowMessage( 'I =' + ' ' + IntToStr( I ) );
  Msg :=  TIdMessage.Create( Self );
  if IdPOP31.Retrieve( I, Msg ) then begin // add an IDMessage 
component from the Indy Misc tab, to initialize IdMessage1
   // BodyS.Clear;
   // ShowMessage( IdMessage1.MsgId );
   // ShowMessage( IdMessage1.Subject );
   {
   for J := 0 to Pred( IdMessage1.MessageParts.Count ) do begin
    if ( IdMessage1.MessageParts.Items[ J ] is TIdAttachment) then 
begin
     BodyS.Add( TIdAttachment(IdMessage1.MessageParts.Items[ 
J ] ).Filename );
    end;
    if ( IdMessage1.MessageParts.Items[ J ] is TIdText ) then begin
     ShowMessage( 'I am here' );
     BodyS.AddStrings( TIdText( IdMessage1.MessageParts.Items[ 
J ]).Body );
     // s := s + ' ' +  TIdText( IdMessage1.MessageParts.Items[ 
J ]).Body.Text;
    end;
   end;
   // .Add(TmpStr + '|' + StringReplace(BodyS.Text, #13#10, '^', 
[rfReplaceAll]));
   //  MsgList.Add(TmpStr);
   // ShowMessage( s );
   ShowMessage( BodyS.Text );
   }
    Msg.NoDecode := false;
    //
    // Msg.NoDecode := true; // this does certainly not work
    //
    BodyS.Clear;
    // Memo1.Clear;
    ShowMessage( Msg.ContentType );
    for J := 0 to Pred( Msg.MessageParts.Count) do
    begin
      //general attachments
      if ( Msg.MessageParts.Items[J] is TIdAttachment ) then
      begin
        // lstAttachments.Add(TIdAttachment( Msg.MessageParts.Items
[J]).Filename);
      end
      //body text
      else
      // begin
       // for K := 0 to 1 do begin
        if Msg.MessageParts.Items[J] is TIdText then begin
         Memo1.Lines.AddStrings(TIdText( Msg.MessageParts.Items
[J]).Body);
         BodyS.AddStrings( TIdText( Msg.MessageParts.Items[ 
J ]).Body );
        end;
       //end;
      // end;
    end;
   ShowMessage( BodyS.Text );
  end
  else begin
   ShowMessage( 'Internet: E-mail: Header: Could not be retrieved' );
  end;
 end;
 IdPOP31.Disconnect;
end;

end.

--- cut here ----------------------------------------------------------

Language: Computer: Delphi: Indy: Demo

[file: source: c:\progra~1\borland\delphi7\demos\indy\readme~1.txt]

Delphi 7
Indy V9 Demos

===========================

Indy V9 Demos

Demo applications for Indy V9 are available from the
downloads section on the Indy site at:

http://www.nevrona.com/indy

As new demos are constantly being built and tested,
this is the most up to date location for demo
downloads.

If you have any problems with demos please join the
"Indy Demos" group at yahoo groups, and report them
there:

http://groups.yahoo.com/group/Indy-Demos/

---