📄 mailsnd1.pas
字号:
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Author: Fran鏾is PIETTE
Object: How to use TSmtpCli component
Creation: 09 october 1997
Version: 2.08
EMail: http://www.overbyte.be http://www.rtfm.be/fpiette
francois.piette@overbyte.be francois.piette@rtfm.be
Support: Use the mailing list twsocket@elists.org
Follow "support" link at http://www.overbyte.be for subscription.
Legal issues: Copyright (C) 1997-2005 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
Apr 08, 2003 V2.03 Arno Garrels <arno.garrels@gmx.de> made some useful
changes:
Search for 04/06/2003 Property AuthComboBox.ItemIndex removed
from dfm, caused error message with older Delphi versions.
AuthComboBox.Items "smtpAuthAutoSelect" added.
Apr 19, 2003 V2.04 Replaced BuildRcptList by the new RcptNameAdd component
method.
May 05, 2003 V2.06 Changed the way data is saved to INI file to allow bigger
messages.
Aug 23, 2004 V2.07 Removed unused units
Mar 12, 2005 V2.08 Added CinfirmCheckbox
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
unit MailSnd1;
{$B-} { Enable partial boolean evaluation }
{$T-} { Untyped pointers }
{$X+} { Enable extended syntax }
interface
uses
WinTypes, WinProcs, Messages, SysUtils, Classes, Controls, Forms,
StdCtrls, ExtCtrls, SmtpProt, IniFiles;
const
SmtpTestVersion = 2.08;
CopyRight : String = ' MailSnd (c) 1997-2005 F. Piette V2.08 ';
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;
PriorityComboBox: TComboBox;
Label11: TLabel;
EhloButton: TButton;
AuthButton: TButton;
Label12: TLabel;
CcEdit: TEdit;
Label13: TLabel;
BccEdit: TEdit;
AllInOneButton: TButton;
ConfirmCheckBox: TCheckBox;
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 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';
KeyPriority = 'Priority';
KeyConfirm = 'Confirm';
SectionWindow = 'Window';
KeyTop = 'Top';
KeyLeft = 'Left';
KeyWidth = 'Width';
KeyHeight = 'Height';
SectionFileAttach = 'Files';
KeyFileAttach = 'File';
SectionMsgMemo = 'Message';
KeyMsgMemo = 'Msg';
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure SaveStringsToIniFile(
const IniFileName : String;
const IniSection : String;
const IniKey : String;
Strings : TStrings);
var
IniFile : TIniFile;
nItem : Integer;
begin
if (IniFileName = '') or (IniSection = '') or (IniKey = '') or
(not Assigned(Strings)) then
Exit;
IniFile := TIniFile.Create(IniFileName);
IniFile.EraseSection(IniSection);
if Strings.Count <= 0 then
IniFile.WriteString(IniSection, IniKey + 'EmptyFlag', 'Empty')
else
for nItem := 0 to Strings.Count - 1 do
IniFile.WriteString(IniSection,
IniKey + IntToStr(nItem),
Strings.Strings[nItem]);
IniFile.Free;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ Return FALSE if non existant in IniFile }
function LoadStringsFromIniFile(
const IniFileName : String;
const IniSection : String;
const IniKey : String;
Strings : TStrings) : Boolean;
var
IniFile : TIniFile;
nItem : Integer;
I : Integer;
Buf : String;
begin
Result := TRUE;
if (IniFileName = '') or (IniSection = '') or (IniKey = '') or
(not Assigned(Strings)) then
Exit;
Strings.Clear;
IniFile := TIniFile.Create(IniFileName);
try
if IniFile.ReadString(IniSection, IniKey + 'EmptyFlag', '') <> '' then
Exit;
IniFile.ReadSectionValues(IniSection, Strings);
finally
IniFile.Free;
end;
nItem := Strings.Count - 1;
while nItem >= 0 do begin
Buf := Strings.Strings[nItem];
if CompareText(IniKey, Copy(Buf, 1, Length(IniKey))) <> 0 then
Strings.Delete(nItem)
else begin
if not (Buf[Length(IniKey) + 1] in ['0'..'9']) then
Strings.Delete(nItem)
else begin
I := Pos('=', Buf);
Strings.Strings[nItem] := Copy(Buf, I + 1, Length(Buf));
end;
end;
Dec(nItem);
end;
Result := (Strings.Count <> 0);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ 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);
PriorityComboBox.ItemIndex := IniFile.ReadInteger(SectionData, KeyPriority, 2);
ConfirmCheckBox.Checked := Boolean(IniFile.ReadInteger(SectionData, KeyConfirm, 0));
if not LoadStringsFromIniFile(FIniFileName, SectionFileAttach,
KeyFileAttach, FileAttachMemo.Lines) then
FileAttachMemo.Text := 'ics_logo.gif' + #13#10 + 'fp_small.gif';
if not LoadStringsFromIniFile(FIniFileName, SectionMsgMemo,
KeyMsgMemo, MsgMemo.Lines) then
MsgMemo.Text :=
'This is the first line' + #13#10 +
'Then the second one' + #13#10 +
'The next one is empty' + #13#10 +
'' + #13#10 +
'The next one has only a single dot' + #13#10 +
'.' + #13#10 +
'Finally the last one' + #13#10;
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -