Topic: [Скрипт] Получение почты (POP3)

Получение почты (POP3) (начиная с версии 3.5)


Пример:

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;


Проект с примером: http://myvisualdatabase.com/forum/misc. … download=1

Dmitry.