unit MainFrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, IdMessage, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP, StdCtrls, Buttons;
type
TMainForm = class(TForm)
SMTP: TIdSMTP;
Message: TIdMessage;
StatusBar: TStatusBar;
GroupBox1: TGroupBox;
lblFrom: TLabel;
edtFrom: TEdit;
lblTo: TLabel;
edtTo: TEdit;
lblSubject: TLabel;
edtSubject: TEdit;
lblBody: TLabel;
memBody: TMemo;
btnSend: TSpeedButton;
GroupBox2: TGroupBox;
lblHost: TLabel;
edtHost: TEdit;
lblPort: TLabel;
edtPort: TEdit;
lblLogin: TLabel;
lblPassword: TLabel;
edtLogin: TEdit;
edtPassword: TEdit;
procedure SMTPConnected(Sender: TObject);
procedure SMTPDisconnected(Sender: TObject);
procedure SMTPStatus(ASender: TObject; const AStatus: TIdStatus;
const AStatusText: String);
procedure btnSendClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
procedure TMainForm.SMTPConnected(Sender: TObject);
begin
GroupBox1.Enabled := False;
GroupBox2.Enabled := False;
end;
procedure TMainForm.SMTPDisconnected(Sender: TObject);
begin
GroupBox1.Enabled := True;
GroupBox2.Enabled := True;
end;
procedure TMainForm.SMTPStatus(ASender: TObject; const AStatus: TIdStatus;
const AStatusText: String);
begin
case AStatus of
hsResolving: StatusBar.SimpleText := 'Wyszukiwanie hosta...';
hsConnecting: StatusBar.SimpleText := 'Łączenie z serwerem ' + SMTP.Host;
hsConnected: StatusBar.SimpleText := 'Połączony z serwerem';
hsDisconnecting: StatusBar.SimpleText := 'Trwa rozłączanie...';
hsDisconnected: StatusBar.SimpleText := 'Rozłączono';
end;
end;
procedure TMainForm.btnSendClick(Sender: TObject);
begin
if Length(edtLogin.Text) > 0 then // Jeżeli użytkownik wpisał login...
begin
SMTP.AuthenticationType := atLogin; //...znaczy to, że serwer wymaga autoryzacji
{ przypisanie właściwości Username (użytkownik) oraz Password (hasło) }
SMTP.Username := edtLogin.Text;
SMTP.Password := edtPassword.Text;
end;
SMTP.Host := edtHost.Text; // przypisanie adresu
SMTP.Port := StrToIntDef(edtPort.Text, 25); // przypisanie IP
try
try
SMTP.Connect; // próba połączenia się z serwerem
btnSend.Enabled := False; // dezaktywacja przycisku
Message.Subject := edtSubject.Text; // temat wiadomości
Message.From.Address := edtFrom.Text; // adres nadawcy
Message.From.Text := edtFrom.Text;
Message.From.Name := edtFrom.Text;
Message.Recipients.Add;
Message.Recipients.Items[0].Address := edtTo.Text; // adres odbiorcy
Message.Recipients.Items[0].Text := edtTo.Text;
Message.Recipients.Items[0].Name := edtTo.Text;
Message.Body.Assign(memBody.Lines); // pobieranie treści wiadomości
SMTP.Send(Message); // wysyłanie e-maila
except
{ w razie wystąpienia błędu - wyświetl komunikat }
raise Exception.Create('Błąd! Nie można połączyć się z serwerem!');
end;
finally
{ te instrukcje będa wykonywane ZAWSZE bez względu na ew. wystąpienie błędu }
btnSend.Enabled := True;
SMTP.Disconnect;
end;
end;
end.