home *** CD-ROM | disk | FTP | other *** search
/ Prima Shareware 3 / DuCom_Prima-Shareware-3_cd1.bin / PROGRAMO / delphi / TSMTP / MMAIL.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-04-26  |  6.2 KB  |  250 lines

  1. unit Mmail;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, Windows, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, Buttons, ExtCtrls, StdCtrls, Smtp,
  8.   SMTPSu,IniFiles, ComCtrls, MailUtil, mailbase;
  9.  
  10. type
  11.   TMailForm = class(TForm)
  12.     ToolBar: TPanel;
  13.     SendButton: TSpeedButton;
  14.     SetupButton: TSpeedButton;
  15.     ExitButton: TSpeedButton;
  16.     SendInfoPanel: TPanel;
  17.     ToEdit: TEdit;
  18.     Label1: TLabel;
  19.     Label3: TLabel;
  20.     SubjectEdit: TEdit;
  21.     CancelButton: TSpeedButton;
  22.     Label2: TLabel;
  23.     BodyMemo: TMemo;
  24.     AttachButton: TSpeedButton;
  25.     AttachmentsComboBox: TComboBox;
  26.     Mailer: TSMTP;
  27.     Panel1: TPanel;
  28.     StatusBar1: TStatusBar;
  29.     ProgressBar1: TProgressBar;
  30.     procedure ExitButtonClick(Sender: TObject);
  31.     procedure SendButtonClick(Sender: TObject);
  32.     procedure CancelButtonClick(Sender: TObject);
  33.     procedure SetupButtonClick(Sender: TObject);
  34.     procedure FormCreate(Sender: TObject);
  35.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  36.     procedure AttachButtonClick(Sender: TObject);
  37.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  38.     procedure MailerProgress(Sender: TObject);
  39.     procedure MailerStatusChange(Sender: TObject);
  40.   private
  41.     { Private declarations }
  42.     IniName : string;
  43.     procedure EnableControls;
  44.     procedure DisableControls;
  45.   public
  46.     { Public declarations }
  47.   end;
  48.  
  49. var
  50.   MailForm: TMailForm;
  51.  
  52. implementation
  53.  
  54. {$R *.DFM}
  55.  
  56. procedure TMailForm.EnableControls;
  57. var
  58.   i : Integer;
  59.   SB : TSpeedButton;
  60. begin
  61.   for i:=0 to ToolBar.ControlCount-1 do
  62.   if ToolBar.Controls[i] is TSpeedButton then
  63.   begin
  64.     SB:=ToolBar.Controls[i] as TSpeedButton;
  65.     SB.Enabled:=SB.Tag<>1;
  66.   end;
  67.   SendInfoPanel.Enabled:=true;
  68.   ProgressBar1.Visible:=false;
  69.   BodyMemo.Enabled:=true;
  70.   Cursor:=crDefault;
  71. end;
  72.  
  73. procedure TMailForm.DisableControls;
  74. var
  75.   i : Integer;
  76.   SB : TSpeedButton;
  77. begin
  78.   for i:=0 to ToolBar.ControlCount-1 do
  79.   if ToolBar.Controls[i] is TSpeedButton then
  80.   begin
  81.     SB:=ToolBar.Controls[i] as TSpeedButton;
  82.     SB.Enabled:=SB.Tag=1;
  83.   end;
  84.   SendInfoPanel.Enabled:=false;
  85.   ProgressBar1.Visible:=true;
  86.   BodyMemo.Enabled:=false;
  87.   Cursor:=crHourGlass;
  88. end;
  89.  
  90. procedure TMailForm.ExitButtonClick(Sender: TObject);
  91. begin
  92.   Close;
  93. end;
  94.  
  95. procedure TMailForm.SendButtonClick(Sender: TObject);
  96. begin
  97.   if (Mailer.Server='') or (Mailer.From='') then
  98.     MessageDlg('You might want to enter the information'^M^J+
  99.                'in the Setup dialog box...',mtError,[mbOk],0)
  100.   else
  101.   if ToEdit.Text='' then
  102.     MessageDlg('You might want to enter the recipient''s address first...'
  103.                ,mtError,[mbOk],0)
  104.   else
  105.   with Mailer do
  106.   begin
  107.     Recipient:=ToEdit.Text;
  108.     Subject:=SubjectEdit.Text;
  109.     Body:=BodyMemo.Lines;
  110.     Attachments:=AttachmentsComboBox.Items;
  111.     TimeOut:=60;
  112.     try
  113.       DisableControls;
  114.       Open;
  115.       LogIn;
  116.       Send;
  117.       LogOut;
  118.     finally
  119.       Close;
  120.       EnableControls;
  121.     end;
  122.   end;
  123. end;
  124.  
  125. procedure TMailForm.CancelButtonClick(Sender: TObject);
  126. begin
  127.   Mailer.Cancel;
  128. end;
  129.  
  130. procedure TMailForm.SetupButtonClick(Sender: TObject);
  131. var
  132.   A,N : string;
  133. begin
  134.   with TSetupDlg.Create(Self) do
  135.   try
  136.     ServerEdit.Text:=Mailer.Server;
  137.     BreakLine(Mailer.From,A,N);
  138.     UserAddressEdit.Text:=A;
  139.     UserNameEdit.Text:=N;
  140.     LogFileNameEdit.Text:=Mailer.LogFileName;
  141.     EncodeComboBox.ItemIndex:=Ord(Mailer.Encoding);
  142.     ShowModal;
  143.     if ModalResult=mrOk then
  144.     begin
  145.       Mailer.Server:=ServerEdit.Text;
  146.       Mailer.From:=UserAddressEdit.Text;
  147.       Mailer.From:=JoinLines(UserAddressEdit.Text,UserNameEdit.Text);
  148.       Mailer.LogFileName:=LogFileNameEdit.Text;
  149.       Mailer.Encoding:=TEncoding(EncodeComboBox.ItemIndex);
  150.     end;
  151.   finally
  152.     free;
  153.   end;
  154. end;
  155.  
  156. procedure TMailForm.FormCreate(Sender: TObject);
  157. var
  158.   A,N : string;
  159.   i : Integer;
  160. begin
  161.   IniName:=ChangeFileExt(Application.ExeName,'.ini');
  162.   with TIniFile.Create(IniName) do
  163.   try
  164.     Mailer.Server:=ReadString('Setup','Server','');
  165.     A:=ReadString('Setup','EMail Address','');
  166.     N:=ReadString('Setup','Name','');
  167.     Mailer.From:=JoinLines(A,N);
  168.     Mailer.LogFileName:=ReadString('Setup','Log File','');
  169.     Mailer.Encoding:=TEncoding(ReadInteger('Setup','Encoding',0));
  170.   finally
  171.     free;
  172.   end;
  173.   for i:=1 to ParamCount do
  174.     AttachmentsComboBox.Items.Add(ParamStr(i));
  175.   AttachmentsComboBox.ItemIndex:=0;
  176. end;
  177.  
  178. procedure TMailForm.FormClose(Sender: TObject; var Action: TCloseAction);
  179. var
  180.   A,N : string;
  181. begin
  182.   with TIniFile.Create(IniName) do
  183.   try
  184.     WriteString('Setup','Server',Mailer.Server);
  185.     BreakLine(Mailer.From,A,N);
  186.     WriteString('Setup','EMail Address',A);
  187.     WriteString('Setup','Name',N);
  188.     WriteString('Setup','Log File',Mailer.LogFileName);
  189.     WriteInteger('Setup','Encoding',Ord(Mailer.Encoding));
  190.   finally
  191.     free;
  192.   end;
  193. end;
  194.  
  195. procedure TMailForm.AttachButtonClick(Sender: TObject);
  196. begin
  197.   with TOpenDialog.Create(Self) do
  198.   try
  199.     Filter:='All Files (*.*)|*.*';
  200.     Options:=[ofHideReadOnly,ofAllowMultiSelect];
  201.     if Execute then
  202.     begin
  203.       AttachmentsComboBox.Items.Assign(Files);
  204.       AttachmentsComboBox.ItemIndex:=0;
  205.     end
  206.     else
  207.       AttachmentsComboBox.Items.Clear;
  208.   finally
  209.     Free;
  210.   end;
  211. end;
  212.  
  213. procedure TMailForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  214. begin
  215.   if not SendInfoPanel.Enabled then
  216.   begin
  217.     MessageDlg('Transfer in Progress.  Click Cancel button first.',
  218.                mtError,[mbOk],0);
  219.     CanClose:=false;
  220.   end
  221.   else
  222.     CanClose:=true;
  223. end;
  224.  
  225. procedure TMailForm.MailerProgress(Sender: TObject);
  226. begin
  227.   ProgressBar1.Position:=Mailer.Progress;
  228. end;
  229.  
  230. procedure TMailForm.MailerStatusChange(Sender : TObject);
  231. var
  232.   s : string;
  233. begin
  234.   case Mailer.Status of
  235.    msIdle : s:='';
  236.    msLogIn : s:='Logging In';
  237.    msLogOut : s:='Logging Out';
  238.    msHeaders : s:='Sending headers';
  239.    msEnvelope : s:='Sending commands';
  240.    msBody : s:='Sending message body';
  241.    msAttachment : s:='Sending attachment(s)';
  242.    msCancel : s:='Canceled';
  243.    msEnCode : s:='Encoding the attachment(s)';
  244.    msConnecting : s:='Connecting to Server';
  245.   end;
  246.   StatusBar1.Panels[0].Text:=s;
  247. end;
  248.  
  249. end.
  250.