Topic: [script] Read email messages (POP3)

Read email messages (POP3) (version 3.5 and above)


Example:

procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
var
  i, MsgCount: integer;
  k, AttachCount: integer;
  IdPOP3: TIdPOP3;
  Msg: TIdMessage;
begin
  IdPOP3 := TIdPOP3.Create(Form1);

  IdPOP3.Host := '';
  IdPOP3.Port := 110;
  IdPOP3.Username := '';
  IdPOP3.Password := '';

  IdPOP3.Connect;
  if IdPOP3.Connected then
  begin
    Msg := TIdMessage.Create(Form1);
    MsgCount:=IdPOP3.CheckMessages;

    // emails
    for i:=1 to MsgCount do
    begin
          Msg.Clear;
          IdPOP3.Retrieve(i, Msg);
          //IdPOP3.RetrieveHeader(i, Msg);
          //IdPOP3.Delete(i)
    
          Form1.Memo1.Lines.Add(IntToStr(i)+'--- Size: ' + IntToStr(IdPOP3.RetrieveMsgSize(i)));
          Form1.Memo1.Lines.Add('Subject: ' +Msg.Subject);
          Form1.Memo1.Lines.Add('===============');
          Form1.Memo1.Lines.Add('ContentType: '+Msg.ContentType);
          Form1.Memo1.Lines.Add('Date: '+DateToStr(Msg.Date));
          Form1.Memo1.Lines.Add('From: '+Msg.From.Address);
          Form1.Memo1.Lines.Add('Body:');
          Form1.Memo1.Lines.Add(Msg.Body.Text);
    
          // Attachments
          AttachCount := Msg.MessageParts.Count-1;
          for k := 0 to AttachCount do
          begin
                if (Msg.MessageParts.Items[k] is TIdAttachment) then // Save file
                begin
                  if FileExists(TIdAttachment(Msg.MessageParts.Items[k]).Filename) then
                    DeleteFile(TIdAttachment(Msg.MessageParts.Items[k]).Filename);
        
                  // Save attachment to the current folder
                  Form1.Memo1.Lines.Add('    -- File: '+TIdAttachment(Msg.MessageParts.Items[k]).Filename);
                  TIdAttachment(Msg.MessageParts.Items[k]).SaveToFile(TIdAttachment(Msg.MessageParts.Items[k]).Filename);
                end else
                if (Msg.MessageParts.Items[k] is TIdText) then // Text attachment
                begin
                    Form1.Memo1.Lines.Add('    -- Begin attachment '+IntToStr(k));
                    Form1.Memo1.Lines.Add('    '+ TIdText(Msg.MessageParts.Items[k]).Body.Text );
                    Form1.Memo1.Lines.Add('    -- End attachment '+IntToStr(k));
                end;
          end;
    
          Form1.Memo1.Lines.Add('===============');
    end;

    Msg.Free;
    IdPOP3.Disconnect;
  end;

  IdPOP3.Free;
end;


Project example:

Post's attachments

Attachment icon POP3.zip 5.55 kb, 175 downloads since 2017-07-06 

Dmitry.

Re: [script] Read email messages (POP3)

Hi mr.Dimitry,


This work only on port = 110?
With SSL/TLS on port 995 not working ?

Thx,

Brgds,

Krokodil

Re: [script] Read email messages (POP3)

Hello.


It's not support yet.

Dmitry.

Re: [script] Read email messages (POP3)

Thx for your answer,

Is this option planed?
or
Can I map external .dll for this purpose?

Thx,

Krokodil,

Re: [script] Read email messages (POP3)

Unfortunately no.

Dmitry.