///////////////////////////////////////////////////////////////////////////
//                                                                       //
// Copyright © 1997-1998, NetMasters, L.L.C                              //
//  - All rights reserved worldwide. -                                   //
//  Portions may be Copyright © Inprise.                                 //
//                                                                       //
// SMTP Demo Unit 1:  (UNIT1.PAS)                                        //
//                                                                       //
// DESCRIPTION:                                                          //
//                                                                       //
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY //
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE   //
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR //
// PURPOSE.                                                              //
//                                                                       //
///////////////////////////////////////////////////////////////////////////
//
// Revision History
//
//                                                                       //
///////////////////////////////////////////////////////////////////////////
{ 30.10.01 : FO : on ne se connecte plus par defaut, ca pose des problems si on a pas le reseau }
unit EmailUnit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, StdCtrls, Psock, NMsmtp, ExtCtrls, JvComponent, JvFormPlace;

type
  TSmtpForm = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    Edit1: TEdit;
    Label1: TLabel;
    Edit2: TEdit;
    Label2: TLabel;
    Button1: TButton;
    Button2: TButton;
    StatusBar1: TStatusBar;
    Edit4: TEdit;
    Label4: TLabel;
    Memo1: TMemo;
    Panel1: TPanel;
    GroupBox1: TGroupBox;
    Edit5: TEdit;
    Edit6: TEdit;
    Label5: TLabel;
    Label6: TLabel;
    Edit7: TEdit;
    Label7: TLabel;
    Edit8: TEdit;
    Label8: TLabel;
    ListBox1: TListBox;
    Label10: TLabel;
    Button4: TButton;
    Button5: TButton;
    Panel2: TPanel;
    Button6: TButton;
    Label11: TLabel;
    Edit10: TEdit;
    NMSMTP1: TNMSMTP;
    OpenDialog1: TOpenDialog;
    Label3: TLabel;
    JvFormPlace1: TJvFormPlace;
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure NMSMTP1Connect(Sender: TObject);
    procedure NMSMTP1Disconnect(Sender: TObject);
    procedure NMSMTP1Status(Sender: TComponent; Status: String);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure NMSMTP1EncodeStart(Filename: String);
    procedure NMSMTP1EncodeEnd(Filename: String);
    procedure NMSMTP1ConnectionFailed(Sender: TObject);
    procedure NMSMTP1ConnectionRequired(var handled: Boolean);
    procedure NMSMTP1Failure(Sender: TObject);
    procedure NMSMTP1HostResolved(Sender: TComponent);
    procedure NMSMTP1InvalidHost(var handled: Boolean);
    procedure NMSMTP1PacketSent(Sender: TObject);
    procedure NMSMTP1RecipientNotFound(Recipient: String);
    procedure NMSMTP1SendStart(Sender: TObject);
    procedure NMSMTP1Success(Sender: TObject);
    procedure NMSMTP1HeaderIncomplete(var handled: Boolean;
      hiType: Integer);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  SmtpForm: TSmtpForm;

implementation

{$R *.DFM}

procedure TSmtpForm.Button1Click(Sender: TObject);
begin
  NMSMTP1.Host := Edit1.Text;
  NMSMTP1.Port := StrToInt(Edit2.Text);
  NMSMTP1.UserID := Edit4.Text;
  NMSMTP1.Connect;
end;

procedure TSmtpForm.Button3Click(Sender: TObject);
var
  TmpStr: String;
begin
  If NMSMTP1.Verify(TmpStr) then
    ShowMessage(TmpStr+' verified')
  else
    ShowMessage(TmpStr+' not verified');
end;

procedure TSmtpForm.Button2Click(Sender: TObject);
begin
  NMSMTP1.Disconnect;
end;

procedure TSmtpForm.NMSMTP1Connect(Sender: TObject);
begin
  StatusBar1.SimpleText := 'Connected';
end;

procedure TSmtpForm.NMSMTP1Disconnect(Sender: TObject);
begin
  If StatusBar1 <> nil then
    StatusBar1.SimpleText := 'Disconnected';
end;

procedure TSmtpForm.NMSMTP1Status(Sender: TComponent; Status: String);
begin
  If StatusBar1 <> nil then
    StatusBar1.SimpleText := status;
end;

procedure TSmtpForm.Button4Click(Sender: TObject);
begin
  If OpenDialog1.Execute then
    ListBox1.Items.Add(OpenDialog1.FileName);
end;

procedure TSmtpForm.Button5Click(Sender: TObject);
begin
  ListBox1.Items.Delete(ListBox1.ItemIndex);
end;

procedure TSmtpForm.Button6Click(Sender: TObject);
begin
  NMSMTP1.PostMessage.FromAddress := Edit6.Text;
  NMSMTP1.PostMessage.FromName := Edit5.Text;
  NMSMTP1.PostMessage.Subject := Edit10.Text;
  NMSMTP1.PostMessage.ToAddress.Add(Edit7.Text);
  NMSMTP1.PostMessage.ToBlindCarbonCopy.Add(''); {Edit9.Text); }
  NMSMTP1.PostMessage.ToCarbonCopy.Add(Edit8.Text);
  NMSMTP1.PostMessage.Attachments.AddStrings(Listbox1.Items);
  NMSMTP1.PostMessage.Body.Assign(Memo1.Lines);
  NMSMTP1.SendMail;
end;

procedure TSmtpForm.NMSMTP1EncodeStart(Filename: String);
begin
  StatusBar1.SimpleText := 'Encoding '+Filename;
end;

procedure TSmtpForm.NMSMTP1EncodeEnd(Filename: String);
begin
  StatusBar1.SimpleText := 'Finished encoding '+Filename;
end;

procedure TSmtpForm.NMSMTP1ConnectionFailed(Sender: TObject);
begin
  ShowMessage('Connection Failed');
end;

procedure TSmtpForm.NMSMTP1ConnectionRequired(var handled: Boolean);
begin
  If MessageDlg('Connection Required. Connect?', mtConfirmation, mbOkCancel, 0) = mrOk then
  Begin
    Handled := TRUE;
    NMSMTP1.Connect;
  End;
end;

procedure TSmtpForm.NMSMTP1Failure(Sender: TObject);
begin
  StatusBar1.SimpleText := 'Failure';
end;

procedure TSmtpForm.NMSMTP1HostResolved(Sender: TComponent);
begin
  StatusBar1.SimpleText := 'Host Resolved';
end;

procedure TSmtpForm.NMSMTP1InvalidHost(var handled: Boolean);
var
  TmpStr: String;
begin
  If InputQuery('Invalid Host!', 'Specify a new host:', TmpStr) then
  Begin
    NMSMTP1.Host := TmpStr;
    Handled := TRUE;
  End;
end;

procedure TSmtpForm.NMSMTP1PacketSent(Sender: TObject);
begin
  StatusBar1.SimpleText := IntToStr(NMSMTP1.BytesSent)+' bytes of '+IntToStr(NMSMTP1.BytesTotal)+' sent';
end;

procedure TSmtpForm.NMSMTP1RecipientNotFound(Recipient: String);
begin
  ShowMessage('Recipient "'+Recipient+'" not found');
end;

procedure TSmtpForm.NMSMTP1SendStart(Sender: TObject);
begin
  StatusBar1.simpleText := 'Sending message';
end;

procedure TSmtpForm.NMSMTP1Success(Sender: TObject);
begin
  StatusBar1.SimpleText := 'Success';
end;

procedure TSmtpForm.NMSMTP1HeaderIncomplete(var handled: Boolean;
  hiType: Integer);
begin
  ShowMessage('Header Incomplete.');
end;


procedure TSmtpForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
 NMSMTP1.Abort;
end;

procedure TSmtpForm.FormCreate(Sender: TObject);
begin
  NMSMTP1.Host := Edit1.Text;
  NMSMTP1.Port := StrToInt(Edit2.Text);
  NMSMTP1.UserID := Edit4.Text;
{  NMSMTP1.Connect; }
{  WindowState:=wsMinimized; }
end;

end.
