unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdMessage, IdBaseComponent, IdComponent, IdTCPServer, IdCmdTCPServer,
  IdExplicitTLSClientServerBase, IdSMTPServer, StdCtrls, IdTCPConnection,
  IdTCPClient, IdMessageClient, IdSMTPBase, IdSMTP, IdAttachment, IdAttachmentFile;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Memo1: TMemo;
    GroupBox1: TGroupBox;
    Edit5: TEdit;
    Memo2: TMemo;
    ListBox1: TListBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Button1: TButton;
    Button2: TButton;
    Label6: TLabel;
    IdMessage1: TIdMessage;
    IdSMTP1: TIdSMTP;
    OpenDialog1: TOpenDialog;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var i: integer;
zalocznik: TIdAttachmentFile;
begin

idSMTP1.Host:=Edit5.Text;
idMessage1.From.Address:=Edit1.Text;
idMessage1.Recipients.EMailAddresses:=Edit2.Text+','+Edit3.Text;
idMessage1.Subject:=Edit4.Text;
idMessage1.Body.Text:=Memo1.Text;

for i:=0 to ListBox1.Items.Count-1 do
zalocznik:=TIdAttachmentFIle.Create(idMessage1.MessageParts,listbox1.Items[i]);

try
  idSMTP1.Connect;
  idSMTP1.Send(idMessage1);
  Memo2.Lines.Add('Wiadomość wysłano');
except on E:Exception do
  Memo2.Lines.Insert(0,'Błąd: '+E.Message);
end;

if idSMTP1.Connected then
idSMTP1.Disconnect;
zalocznik.Free;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
 ListBox1.Items.Add(OpenDialog1.FileName)
end;

end.
