📄 mailsnd1.pas
字号:
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Author: Fran鏾is PIETTE
Object: How to use TSmtpCli component
Creation: 09 october 1997
Version: 2.03
EMail: http://www.overbyte.be http://www.rtfm.be/fpiette
francois.piette@overbyte.be francois.piette@rtfm.be
francois.piette@pophost.eunet.be
Support: Use the mailing list twsocket@elists.org
Follow "support" link at http://www.overbyte.be for subscription.
Legal issues: Copyright (C) 1997-2002 by Fran鏾is PIETTE
Rue de Grady 24, 4053 Embourg, Belgium. Fax: +32-4-365.74.56
<francois.piette@overbyte.be>
This software is provided 'as-is', without any express or
implied warranty. In no event will the author be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following
restrictions:
1. The origin of this software must not be misrepresented,
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
Updates:
Oct 26, 1997 V1.00 Released
Jan 10, 1998 V1.01 Added a Port property
Feb 15, 1998 V1.02 Added file attachement support
Mar 06, 1998 V1.03 Check for DisplayMemo overflow (100 lines allowed)
Aug 03, 1998 V2.00 Revised for new asynchronous SMTP component version
Jul 26, 2001 V2.01 Added authentification
Sep 07, 2002 V2.02 Added Cc and Bcc fields.
Added AllInOne demo to show how to "chain" several operations
using OnRequest done, avoiding any wait loop. This is how event
driven operation has to be done.
Sep 15, 2002 V2.02 Corrected typo error in BuildRcptList where CcEdi was used
where ToEdit should.
Thanks to konstantinos.Kokkorogiannis@diala.greenpeace.org
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
unit MailSnd1;
{$B-} { Enable partial boolean evaluation }
{$T-} { Untyped pointers }
{$X+} { Enable extended syntax }
interface
uses
WinTypes, WinProcs, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, SmtpProt, StdCtrls, ExtCtrls, IniFiles;
const
SmtpTestVersion = 2.03;
CopyRight : String = ' MailSnd (c) 1997-2002 F. Piette V2.03 ';
type
TSmtpTestForm = class(TForm)
MsgMemo: TMemo;
DisplayMemo: TMemo;
ToolsPanel: TPanel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Subject: TLabel;
Label4: TLabel;
HostEdit: TEdit;
FromEdit: TEdit;
ToEdit: TEdit;
SubjectEdit: TEdit;
SignOnEdit: TEdit;
PortEdit: TEdit;
Label5: TLabel;
AttachPanel: TPanel;
Label6: TLabel;
FileAttachMemo: TMemo;
InfoPanel: TPanel;
Label7: TLabel;
ClearDisplayButton: TButton;
ConnectButton: TButton;
HeloButton: TButton;
MailFromButton: TButton;
RcptToButton: TButton;
DataButton: TButton;
AbortButton: TButton;
QuitButton: TButton;
MailButton: TButton;
OpenButton: TButton;
Label8: TLabel;
SmtpClient: TSmtpCli;
Label9: TLabel;
Label10: TLabel;
UsernameEdit: TEdit;
PasswordEdit: TEdit;
AuthComboBox: TComboBox;
Label11: TLabel;
EhloButton: TButton;
AuthButton: TButton;
Label12: TLabel;
CcEdit: TEdit;
Label13: TLabel;
BccEdit: TEdit;
AllInOneButton: TButton;
procedure FormCreate(Sender: TObject);
procedure ClearDisplayButtonClick(Sender: TObject);
procedure ConnectButtonClick(Sender: TObject);
procedure SmtpClientRequestDone(Sender: TObject; RqType: TSmtpRequest;
Error: Word);
procedure HeloButtonClick(Sender: TObject);
procedure MailFromButtonClick(Sender: TObject);
procedure RcptToButtonClick(Sender: TObject);
procedure DataButtonClick(Sender: TObject);
procedure AbortButtonClick(Sender: TObject);
procedure QuitButtonClick(Sender: TObject);
procedure MailButtonClick(Sender: TObject);
procedure OpenButtonClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure EhloButtonClick(Sender: TObject);
procedure AuthButtonClick(Sender: TObject);
procedure AllInOneButtonClick(Sender: TObject);
procedure SmtpClientDisplay(Sender: TObject; Msg: String);
procedure SmtpClientGetData(Sender: TObject; LineNum: Integer;
MsgLine: Pointer; MaxLen: Integer; var More: Boolean);
procedure SmtpClientHeaderLine(Sender: TObject; Msg: Pointer;
Size: Integer);
private
FIniFileName : String;
FInitialized : Boolean;
FAllInOneFlag : Boolean;
procedure Display(const Msg : String);
procedure BuildRcptList;
procedure ExceptionHandler(Sender: TObject; E: Exception);
end;
var
SmtpTestForm: TSmtpTestForm;
implementation
{$R *.DFM}
const
SectionData = 'Data';
KeyHost = 'HostName';
KeyPort = 'Port';
KeyFrom = 'From';
KeyTo = 'To';
KeyCc = 'Cc';
KeyBcc = 'Bcc';
KeySubject = 'Subject';
KeySignOn = 'SignOn';
KeyUser = 'UserName';
KeyPass = 'Password';
KeyAuth = 'Authentification';
SectionWindow = 'Window';
KeyTop = 'Top';
KeyLeft = 'Left';
KeyWidth = 'Width';
KeyHeight = 'Height';
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ Display a message in display memo box, making sure we don't overflow it. }
procedure TSmtpTestForm.Display(const Msg : String);
begin
DisplayMemo.Lines.BeginUpdate;
try
if DisplayMemo.Lines.Count > 200 then begin
{ We preserve only 200 lines }
while DisplayMemo.Lines.Count > 200 do
DisplayMemo.Lines.Delete(0);
end;
DisplayMemo.Lines.Add(Msg);
finally
DisplayMemo.Lines.EndUpdate;
{ Makes last line visible }
{$IFNDEF VER80}
SendMessage(DisplayMemo.Handle, EM_SCROLLCARET, 0, 0);
{$ENDIF}
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSmtpTestForm.FormCreate(Sender: TObject);
begin
Application.OnException := ExceptionHandler;
DisplayMemo.Clear;
FIniFileName := LowerCase(ExtractFileName(Application.ExeName));
FIniFileName := Copy(FIniFileName, 1, Length(FIniFileName) - 3) + 'ini';
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSmtpTestForm.FormShow(Sender: TObject);
var
IniFile : TIniFile;
begin
if not FInitialized then begin
FInitialized := TRUE;
IniFile := TIniFile.Create(FIniFileName);
HostEdit.Text := IniFile.ReadString(SectionData, KeyHost,
'localhost');
PortEdit.Text := IniFile.ReadString(SectionData, KeyPort,
'smtp');
FromEdit.Text := IniFile.ReadString(SectionData, KeyFrom,
'first.last@company.com');
ToEdit.Text := IniFile.ReadString(SectionData, KeyTo,
'john.doe@acme;tartempion@brol.fr');
CcEdit.Text := IniFile.ReadString(SectionData, KeyCc,
'');
BccEdit.Text := IniFile.ReadString(SectionData, KeyBcc,
'francois.piette@swing.be');
SubjectEdit.Text := IniFile.ReadString(SectionData, KeySubject,
'This is the message subject');
SignOnEdit.Text := IniFile.ReadString(SectionData, KeySignOn,
'your name');
UsernameEdit.Text := IniFile.ReadString(SectionData, KeyUser,
'account name');
PasswordEdit.Text := IniFile.ReadString(SectionData, KeyPass,
'account password');
AuthComboBox.ItemIndex := IniFile.ReadInteger(SectionData, KeyAuth,
0);
Top := IniFile.ReadInteger(SectionWindow, KeyTop, (Screen.Height - Height) div 2);
Left := IniFile.ReadInteger(SectionWindow, KeyLeft, (Screen.Width - Width) div 2);
Width := IniFile.ReadInteger(SectionWindow, KeyWidth, Width);
Height := IniFile.ReadInteger(SectionWindow, KeyHeight, Height);
IniFile.Free;
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSmtpTestForm.FormClose(Sender: TObject;
var Action: TCloseAction);
var
IniFile : TIniFile;
begin
IniFile := TIniFile.Create(FIniFileName);
IniFile.WriteString(SectionData, KeyHost, HostEdit.Text);
IniFile.WriteString(SectionData, KeyPort, PortEdit.Text);
IniFile.WriteString(SectionData, KeyFrom, FromEdit.Text);
IniFile.WriteString(SectionData, KeyTo, ToEdit.Text);
IniFile.WriteString(SectionData, KeyCc, CcEdit.Text);
IniFile.WriteString(SectionData, KeyBcc, BccEdit.Text);
IniFile.WriteString(SectionData, KeySubject, SubjectEdit.Text);
IniFile.WriteString(SectionData, KeySignOn, SignOnEdit.Text);
IniFile.WriteString(SectionData, KeyUser, UsernameEdit.Text);
IniFile.WriteString(SectionData, KeyPass, PasswordEdit.Text);
IniFile.WriteInteger(SectionData, KeyAuth, AuthComboBox.ItemIndex);
IniFile.WriteInteger(SectionWindow, KeyTop, Top);
IniFile.WriteInteger(SectionWindow, KeyLeft, Left);
IniFile.WriteInteger(SectionWindow, KeyWidth, Width);
IniFile.WriteInteger(SectionWindow, KeyHeight, Height);
IniFile.Free;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{$IFDEF VER80}
function TrimRight(Str : String) : String;
var
i : Integer;
begin
i := Length(Str);
while (i > 0) and (Str[i] = ' ') do
i := i - 1;
Result := Copy(Str, 1, i);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function TrimLeft(Str : String) : String;
var
i : Integer;
begin
if Str[1] <> ' ' then
Result := Str
else begin
i := 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -