📄 smtpprot.pas
字号:
{*_* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Author: Fran鏾is PIETTE
Object: TSmtpCli class implements the SMTP protocol (RFC-821)
Support file attachement using MIME format (RFC-1521, RFC-2045)
Support authentification (RFC-2104)
Support HTML mail with embedded images.
Creation: 09 october 1997
Version: 2.48
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.
4. You must register this software by sending a picture postcard
to the author. Use a nice stamp and mention your name, street
address, EMail address and any comment you like to say.
How to use the HTML feature:
An HTML mail message is composed of 3 parts: an HTML formatted message,
a plain text message and a collection of document files which may be
reference in the HTML (for example images).
The HTML mail message is formatted by the THtmlSmtpCli component using the
MIME multipart format. There are two level of parts. An external level
with the message as part one and the documents in one or more parts.
The first part is itself a multipart message. The first part in the first
part is the plain text message, the second part in the first part is the
HTML message.
So to build a correct HTML mail message, you have to supply an ascii text
for the plain text part (PlainText property), a HTML message (HtmlText
property) and zero or more document filenames (EmailFiles property).
In the HTML part, you may reference the files (for example images) using
the special URL beginning with 'cid:'. For example to include an image,
you use: <IMG SRC="cid:IMAGE1">. And the image file has to be the first
file listed in EmailFiles property. The second image would be referenced
<IMG SRC="cid:IMAGE2">, and so on. It is always "cid:IMAGEn" with n
replaced by the position in EmailFiles property.
If you wants to have nromal attached files, just put them and the end
of EmailFiles list. They will be shown as attached files.
Updates:
Oct 25, 1997 Added the OnHeaderLine event to allow modification/deletion of
header lines.
Oct 26, 1997 V1.00 Released
Changed the OnGetData event arguments to have code compatible
between 16 and 32 bit versions (replaced string with PChar).
Jan 10, 1998 V1.01 Added a Port property
Feb 14, 1998 V1.02 Added an intermeditae TCustomSmtpClient in order to
support MIME in the TSmtpCli. I implemented MIME with the
help of code donated by Brad Choate <choate@delphiexchange.com>
Mime is used for file attachement.
Added a SetRcptName to copy values from a string list in place
of copying the string list reference.
Feb 15, 1998 V1.03 Added a CharSet property, defaulting to iso-8859-1
Mar 02, 1998 V1.04 Corrected result for QUIT command.
Marcus Schmutz <schmutz@kwsoft.de>
Mar 06, 1998 V1.05 Use OnDataSent event to prenvent over-buffering
Mar 15, 1998 V1.06 Implemented the Date header line
Apr 01, 1998 V1.07 Adapted for BCB V3
Apr 10, 1998 V1.08 Corrected DayNames: sunday is day 1, saturday is day 7.
Changed UUEncode procedures to virtual methods to ease component
inheritance.
Apr 26, 1998 V1.09 Ignore any empty file name (a very common error !)
Check if file exists and raise an exception if not.
Made Rfc822DateTime public.
Added Rset method from Victor Garcia Aprea <vga@overnet.com.ar>
Added Abort procedure to close the socket and abort any operation
Made the underlaying TWSocket accessible using a property.
Apr 28, 1998 V1.10 Reset FTimeOutFlag in the mail procedure.
May 05, 1998 V1.11 Handled correctly lines beginning with a dot.
May 21, 1998 V1.12 Check for nil argument in SetEMailFiles
Added OnCommand and OnResponse events.
Added SendDataLine procedure (same as SendCommand, but do not
trigger OnCommand event) used for header and message lines.
Jul 29, 1998 V2.00 Asynchronous functions and new TSyncSmtpCli component
to be a placer holder for synchronous version.
Renamed source file from SmtpCli to SmtpProt.
Aug 06, 1998 V2.01 Made HighLevelAsync public and added smtpCustom to be used
for custom calls to HighLevelAsync.
Sep 22, 1998 V2.02 Removed useless Wait unit from the uses clause.
Oct 04, 1998 V2.03 Checked for Error in TriggerRequestDone.
Oct 11, 1998 V2.04 Removed -1 in DataNext. Thanks to Dennis V. Turov
<chip@quorum.ru> for finding this bug.
Nov 22, 1998 V2.05 Implemented VRFY command with code proposed by
DZ-Jay <dz@caribe.net> but use HdrTo property as name to verify.
Nov 29, 1998 V2.06 Added SetErrorMessage in WSocketSessionConnected when an
error occured. Thanks to DZ-Jay.
Changed FMimeBoundary format to use numbered month instead of
month names. Thanks to Dmitry Kislov <kislov@tekom.odessa.ua> who
found that some foreign charsets are invalid in mime boundaries.
Dec 22, 1998 V2.07 Handle exception when connecting (will be triggered when
an invalid port has been given).
Force readonly when reading attached files.
Added ContentType property as suggested by Henri Fournier
<hfournier@home.com>
Feb 13, 1999 V2.08 Published the state property and OnSessionConnected,
OnSessionClosed events.
Feb 27, 1999 V2.09 Added Connected property.
Added code from Larry Pesyna <ldpesyna@aep.com> to handle time
zone bias.
Added OnAttachContentType event. Thanks to Vladimir M.
Zakharychev <zak@dzbjaro.bertelsmann.de> for his suggestion.
Added ReplyTo and ReturnPath properties. Thanks to Eric Bullen
<eric@thedeepsky.com> for his code.
Mar 06, 1999 V2.10 Conditional compile to remove timezone code unsupported by
Delphi 1.
Mar 09, 1999 V2.11 Made state property [really] published.
Mar 27, 1999 V2.12 Published OnProcessHeader
Changed sign for time zone bias (thanks to Larry Pesyna).
May 10, 1999 V2.13 'daylight' functionality for timezonebias function.
Thanks to Bernhard Goebel <Bernhard.Goebel@t-online.de>
Do not set FRequestType in Connect when called from HighLevel
function. Thanks to Eugene V. Krapivin <evk@tagil.ru>.
May 18, 1999 V2.14 Added Sender field. If ommited, the sender is becomes
HdrFrom. Jon Glazer <jglazer@adconn.com>
Jul 30, 1999 V2.15 Added MailMessage property by Thomas Kvamme
<thokvamm@online.no>. MailMessage property can be used with
OnGetData event. If both are used, MailMessages lines appears
before lines got by OnGetData.
Oct 02, 1999 V2.16 Added OnAttachHeader event as suggested by Vladimir M.
Zakharychev <zak@dzbjaro.bertelsmann.de>
Accept friendly EMail addresses. Thanks to Thierry De Leeuw
<thierry.deleeuw@proxis.be> for his code.
Nov 01, 1999 V2.17 Made all fields protected to easy component inheritance.
Oct 15, 2000 V2.18 Check for too long lines in TriggerGetData.
Thanks to Steve Williams <stevewilliams@kromestudios.com>
Jun 18, 2001 V2.19 Use AllocateHWnd and DeallocateHWnd from wsocket.
Renamed property WSocket to CtrlSocket (this require code change
in user application too).
Jul 26, 2001 V2.20 Angus Robertson <angus@magsys.co.uk> found a problem when
using the MailSync method that it's not possible to send a body
that takes longer than the timeout in WaitUntilReady. Timeout has
to be reevaluated in TriggerGetData.
Jake Traynham <jake@comm-unity.net> added authentification and
EHLO code. Well done job.
Aug 18, 2001 V2.21 Angus V2.21 added OwnHeaders property flag which allows
mail relaying where the body includes all headers and none are
added by the component
Sep 09, 2001 V2.22 Beat Boegli <leeloo999@bluewin.ch> added LocalAddr property
for multihomed hosts.
Dec 24, 2001 V2.23 Added support for NOFORMS (console mode without Forms unit).
Added X-Mailer header line.
Jan 09, 2001 V2.24 Corrected WSocketDnsLookupDone where FRequestResult was not
properly set with errorcode. Corrected DoHighLevelAsync to set
RequestResult to 426 when aborting. Found by "SJF" <bcb@daqing.net>.
Mar 17, 2002 V2.25 Check for FRequestType = smtpQuit in NextExecAsync to avoid
calling OnRequestDone before remote server has closed connection.
And in WSocketSessionClosed, check if last command was smtpQuit
to select proper error code for OnRequestDone event.
Lot of peoples helped find this one (Alphabetical order):
David Colliver <david.colliver@revilloc.com>
DZ-Jay <dz@caribe.net>
Max Terentiev <support@nexus6.ru>
Roger Morton <roger@chez-morton.com>
Wilfried Mestdagh <wilfried@mestdagh.biz>
Apr 01, 2002 V2.26 TriggerRequestDone with correct winsock error in
WSocketDnsLookupDone. Thanks to DZ [dz@caribe.net] and
Roger Morton [roger@chez-morton.com] for fixing this bug.
Apr 20, 2002 V2.27 Enhance NOFORMS mode.
Apr 24, 2002 V2.28 Return real error code in case of error in
WSocketDnsLookupDone. Thanks to DZ-Jay <dz@caribe.net>.
Sep 07, 2002 V2.29 Added HdrCc property to send mail to CC.
Oct 26, 2002 V2.30 Revised Rfc822DateTime function.
Nov 01, 2002 V2.31 Changed arguments for event from PChar to Pointer to avoid
Delphi 7 bug with PCHar <-> AnsiChar. This will require small
changes in your application: change PChar to Pointer in your
event handler and probably add a PChar cast when using the args.
Nov 11, 2002 V2.32 Revised for Delphi 1
Apr 08, 2003 V2.33 Arno Garrels <arno.garrels@gmx.de> made some useful
changes:
ThreadDetach/ThreadAttach added.
Fixed: A possibly open file handle was not closed on abnormal
termination (I'm not quite happy with this fix).
AUTH AutoSelection added.
Global var FileMode is not thread-safe.
Modified UUEncoding in a way that it uses a TFileStream.
New property ShareMode was added.
Made AuthTypesSupported public.
Apr 18, 2003 V2.34 added THtmlSmtpCli to send HTML formatted mail with
embedded images or documents.
Apr 19, 2003 V2.35 Arno Garrels <arno.garrels@gmx.de> made some useful changes:
As Jake Traynham <jake@comm-unity.net> suggested, in case of
AuthType is set to smtpAuthAutoSelect the component tries each
of the AuthTypes one after the other until one works (or not).
That should make it closer to RFC2554. Added X-Priority header,
proc FoldHdrLine (Reply-To, To, CC headers are *folded* now,
see TCustomSmtpClient.Data, accoording to RFC822), modified
Base64 encoded line length from 60 to 72 chars.
Apr 19, 2003 V2.36 Added RcptNameAdd procedure.
Added ParseEmail function.
Added Priority (RFC1327) and X-MSMail-Priority header line.
Added smtpPriorityNone.
Replaced all spaces in FSignon by underscore before sending.
May 03, 2003 V2.37 Moved MIME routines to MimeUtil unit.
Renamed UUEncode to EncFileBase64 because it was really base64
encoding. The routines are now in MimeUtil.
Break quoted-printable at column 76 as stated in RFC2045
May 09, 2003 V2.38 Revised FoldHdrLine and RcptNameAdd to accept both coma
and semicolon as email addresses separators.
Thanks to Arno Garrels <arno.garrels@gmx.de> for his help.
Sep 07, 2003 V2.39 In WSocketDnsLookupDone trigger OnSessionConnected and
OnSessionClosed events when an error occurs in DNS resolution.
Jan 10, 2004 V2.40 Added CramSHA1 authentication code from
"Piotr Hellrayzer Dalek" <enigmatical@interia.pl>
May 02, 2004 V2.41 Renamed uint Sha1 to IcsSha1 to avoi conflict with other
products. Allowed it for Delphi 3 and up.
May 31, 2004 V2.42 Used ICSDEFS.INC the same way as in other units
Jul 18, 2004 V2.43 Adpoted code from eric launay <eric.launay@numericable.fr>
to have a separate list of images and attached files in the
THtmlSmtpCli component and send images as inline attachement.
This solve display problem with some mail client such as HotMail.
Aug 23, 2004 V2.44 Use MsgWaitForMultipleObjects in WaitUntilReady to avoid
consumming 100% CPU while waiting.
Sep 08, 2004 V2.45 MD5 has been renamed to IcsMD5
Jan 13, 2005 V2.46 In WaitUntilReady, moved MsgWaitForMultipleObjects before
the call to the message pump and added a few flags for
MsgWaitForMultipleObjects. This make the loop much efficient.
Mar 01, 2005 V2.47 Changes by Arno Garrels <arno.garrels@gmx.de>. Added a new
event OnRcptToError. Fixed: When RcptName had multiple
recipients request results of all but the last call to
RcptTo were silently ignored.
Mar 12, 2005 V2.48 Arno Garrels added new property ConfirmReceipt which means
"request confirmation of receipt".
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
unit SmtpProt;
interface
{$B-} { Enable partial boolean evaluation }
{$T-} { Untyped pointers }
{$X+} { Enable extended syntax }
{$I ICSDEFS.INC}
{$IFDEF DELPHI6_UP}
{$WARN SYMBOL_PLATFORM OFF}
{$WARN SYMBOL_LIBRARY OFF}
{$WARN SYMBOL_DEPRECATED OFF}
{$ENDIF}
{$IFNDEF VER80} { Not for Delphi 1 }
{$H+} { Use long strings }
{$J+} { Allow typed constant to be modified }
{$ENDIF}
{$IFDEF BCB3_UP}
{$ObjExportAll On}
{$ENDIF}
uses
Messages,
{$IFDEF USEWINDOWS}
Windows,
{$ELSE}
WinTypes, WinProcs,
{$ENDIF}
SysUtils, Classes,
{$IFNDEF NOFORMS}
Forms, Controls,
{$ENDIF}
WSocket, WinSock, IcsMD5,
{$IFDEF DELPHI3_UP}
IcsSha1, { SHA1 unit compatible with D3 and UP }
{$ENDIF}
MimeUtil;
const
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -