📄 pjnsmtp.cpp
字号:
/*
Module : PJNSMTP.CPP
Purpose: Implementation for a MFC class encapsulation of the SMTP protocol
Created: PJN / 22-05-1998
History: PJN / 15-06-1998 1) Fixed the case where a single dot occurs on its own
in the body of a message
2) Class now supports Reply-To Header Field
3) Class now supports file attachments
PJN / 18-06-1998 1) Fixed a memory overwrite problem which was occurring
with the buffer used for encoding base64 attachments
PJN / 27-06-1998 1) The case where a line begins with a "." but contains
other text is now also catered for. See RFC821, Section 4.5.2
for further details.
2) m_sBody in CPJNSMTPMessage has now been made protected.
Client applications now should call AddBody instead. This
ensures that FixSingleDot is only called once even if the
same message is sent a number of times.
3) Fixed a number of problems with how the MIME boundaries
were defined and sent.
4) Got rid of an unreferenced formal parameter
compiler warning when doing a release build
PJN / 11-09-1998 1) VC 5 project file is now provided
2) Attachment array which the message class contains now uses
references instead of pointers.
3) Now uses Sleep(0) to yield our time slice instead of Sleep(100),
this is the preferred way of writting polling style code in Win32
without serverly impacting performance.
4) All Trace statements now display the value as returned from
GetLastError
5) A number of extra asserts have been added
6) A AddMultipleRecipients function has been added which supports added a
number of recipients at one time from a single string
7) Extra trace statements have been added to help in debugging
PJN / 12-09-98 1) Removed a couple of unreferenced variable compiler warnings when code
was compiled with Visual C++ 6.0
2) Fixed a major bug which was causing an ASSERT when the CSMTPAttachment
destructor was being called in the InitInstance of the sample app.
This was inadvertingly introduced for the 1.2 release. The fix is to revert
fix 2) as done on 11-09-1998. This will also help to reduce the number of
attachment images kept in memory at one time.
PJN / 18-01-99 1) Full CC & BCC support has been added to the classes
PJN / 22-02-99 1) Addition of a Get and SetTitle function which allows a files attachment
title to be different that the original filename
2) AddMultipleRecipients now ignores addresses if they are empty.
3) Improved the reading of responses back from the server by implementing
a growable receive buffer
4) timeout is now 60 seconds when building for debug
PJN / 25-03-99 1) Now sleeps for 250 ms instead of yielding the time slice. This helps
reduce CPU usage when waiting for data to arrive in the socket
PJN / 14-05-99 1) Fixed a bug with the way the code generates time zone fields in the Date headers.
PJN / 10-09-99 1) Improved CPJNSMTPMessage::GetHeader to include mime field even when no attachments
are included.
PJN / 16-02-00 1) Fixed a problem which was occuring when code was compiled with VC++ 6.0.
PJN / 19-03-00 1) Fixed a problem in GetHeader on Non-English Windows machines
2) Now ships with a VC 5 workspace. I accidentally shipped a VC 6 version in one of the previous versions of the code.
3) Fixed a number of UNICODE problems
4) Updated the sample app to deliberately assert before connecting to the author's SMTP server.
PJN / 28-03-00 1) Set the release mode timeout to be 10 seconds. 2 seconds was causing problems for slow dial
up networking connections.
PJN / 07-05-00 1) Addition of some ASSERT's in CPJNSMTPSocket::Connect
PP / 16-06-00 The following modifications were done by Puneet Pawaia
1) Removed the base64 encoder from this file
2) Added the base64 encoder/decoder implementation in a separate
file. This was done because base64 decoding was not part of
the previous implementation
3) Added support for ESMTP connection. The class now attempts to
authenticate the user on the ESMTP server using the username and
passwords supplied. For this connect now takes the username and
passwords as parameters. These can be null in which case ESMTP
authentication is not attempted
4) This class can now handle AUTH LOGIN and AUTH LOGIN PLAIN authentication
schemes on
PP / 19-06-00 The following modifications were done by Puneet Pawaia
1) Added the files md5.* containing the MD5 digest generation code
after modifications so that it compiles with VC++ 6
2) Added the CRAM-MD5 login procedure.
PJN / 10-07-00 1) Fixed a problem with sending attachments > 1K in size
2) Changed the parameters to CPJNSMTPConnection::Connect
PJN / 30-07-00 1) Fixed a bug in AuthLogin which was transmitting the username and password
with an extra "=" which was causing the login to failure. Thanks to Victor Vogelpoel for
finding this.
PJN / 05-09-00 1) Added a CSMTP_NORSA preprocessor macro to allow the CPJNSMTPConnection code to be compiled
without the dependence on the RSA code.
PJN / 28-12-2000 1) Removed an unused variable from ConnectESMTP.
2) Allowed the hostname as sent in the HELO command to be specified at run time
in addition to using the hostname of the client machine
3) Fixed a problem where high ascii characters were not being properly encoded in
the quoted-printable version of the body sent.
4) Added support for user definable charset's for the message body.
5) Mime boundaries are now always sent irrespective if whether attachments are included or
not. This is required as the body is using quoted-printable.
6) Fixed a bug in sendLines which was causing small message bodies to be sent incorrectly
7) Now fully supports custom headers in the SMTP message
8) Fixed a copy and paste bug where the default port for the SMTP socket class was 110.
9) You can now specify the address on which the socket is bound. This enables the programmer
to decide on which NIC data should be sent from. This is especially useful on a machine
with multiple IP addresses.
10) Addition of functions in the SMTP connection class to auto dial and auto disconnect to
the Internet if you so desire.
11) Sample app has been improved to allow Auto Dial and binding to IP addresses to be configured.
PJN / 26-02-2001 1) Charset now defaults to ISO 8859-1 instead of us-ascii
2) Removed a number of unreferrenced variables from the sample app.
3) Headers are now encoded if they contain non ascii characters.
4) Fixed a bug in getLine, Thanks to Lev Evert for spotting this one.
5) Made the charset value a member of the message class instead of the connection class
6) Sample app now fully supports specifying the charset of the message
7) Added a AddMultipleAttachments method to CPJNSMTPMessage
8) Attachments can now be copied to each other via new methods in CSMTPAttachment
9) Message class now contains copies of the attachments instead of pointers to them
10) Sample app now allows multiple attachments to be added
11) Removed an unnecessary assert in QuotedPrintableEncode
12) Added a Mime flag to the CPJNSMTPMessage class which allows you to decide whether or not a message
should be sent as MIME. Note that if you have attachments, then mime is assumed.
13) CSMTPAttachment class has now become CPJNSMTPBodyPart in anticipation of full support for MIME and
MHTML email support
14) Updated copright message in source code and documentation
15) Fixed a bug in GetHeader related to _tsetlocale usage. Thanks to Sean McKinnon for spotting this
problem.
16) Fixed a bug in SendLines when sending small attachments. Thanks to Deng Tao for spotting this
problem.
17) Removed the need for SendLines function entirely.
18) Now fully supports HTML email (aka MHTML)
19) Updated copyright messages in code and in documentation
PJN / 17-06-2001 1) Fixed a bug in CPJNSMTPMessage::HeaderEncode where spaces were not being interpreted correctly. Thanks
to Jim Alberico for spotting this.
2) Fixed 2 issues with ReadResponse both having to do with multi-line responses. Thanks to Chris Hanson
for this update.
PJN / 25-06-2001 1) Code now links in Winsock and RPCRT40 automatically. This avoids client code having to specify it in
their linker settings. Thanks to Malte and Phillip for spotting this issue.
2) Updated sample code in documentation. Thanks to Phillip for spotting this.
3) Improved the code in CPJNSMTPBodyPart::SetText to ensure lines are correctly wrapped. Thanks to
Thomas Moser for this fix.
PJN / 01-07-2001 1) Modified QuotedPrintableEncode to prevent the code to enter in an infinite loop due to a long word i.e.
bigger than SMTP_MAXLINE, in this case, the word is breaked. Thanks to Manuel Gustavo Saraiva for this fix.
2) Provided a new protected variable in CPJNSMTPBodyPart called m_bQuotedPrintable to bypass the
QuotedPrintableEncode function in cases that we don't want that kind of correction. Again thanks to
Manuel Gustavo Saraiva for this fix.
PJN / 15-07-2001 1) Improved the error handling in the function CPJNSMTPMessage::AddMultipleAttachments. In addition the
return value has been changed from BOOL to int
PJN / 11-08-2001 1) Fixed a bug in QuotedPrintableEncode which was wrapping encoding characters across multiple lines.
Thanks to Roy He for spotting this.
2) Provided a "SendMessage" method which sends a email directly from disk. This allows you
to construct your own emails and the use the class just to do the sending. This function also has the
advantage that it efficiently uses memory and reports progress.
3) Provided support for progress notification and cancelling via the "OnSendProgress" virtual method.
PJN / 29-09-2001 1) Fixed a bug in ReadResponse which occured when you disconnected via Dial-Up Networking
while a connection was active. This was originally spotted in my POP3 class.
2) Fixed a problem in CPJNSMTPBodyPart::GetHeader which was always including the "quoted-printable" even when
m_bQuotedPrintable for that body part was FALSE. Thanks to "jason" for spotting this.
PJN / 12-10-2001 1) Fixed a problem where GetBody was reporting the size as 1 bigger than it should have been. Thanks
to "c f" for spotting this problem.
2) Fixed a bug in the TRACE statements when a socket connection cannot be made.
3) The sample app now displays a filter of "All Files" when selecting attachments to send
4) Fixed a problem sending 0 byte attachments. Thanks to Deng Tao for spotting this problem.
PJN / 11-01-2002 1) Now includes a method to send a message directly from memory. Thanks to Tom Allebrandi for this
suggestion.
2) Change function name "IsReadible" to be "IsReadable". I was never very good at English!.
3) Fixed a bug in CPJNSMTPBodyPart::QuotedPrintableEncode. If a line was exactly 76 characters long plus
\r\n it produced an invalid soft linebreak of "\r=\r\n\n". Thanks to Gerald Egert for spotting this problem.
PJN / 29-07-2002 1) Fixed an access violation in CPJNSMTPBodyPart::QuotedPrintableEncode. Thanks to Fergus Gallagher for
spotting this problem.
2) Fixed a problem where in very rare cases, the QuotedPrintableEncode function produces a single dot in a
line, when inserting the "=" to avoid the mail server's maxline limit. I inserted a call to FixSingleDot
after calling the QuotedPrintableEncode function in GetBody. Thanks to Andreas Kappler for spotting this
problem.
3) Fixed an issue in CPJNSMTPBodyPart::GetHeader where to ensure some mail clients can correctly handle
body parts and attachments which have a filename with spaces in it. Thanks to Andreas kappler for spotting
this problem.
4) QP encoding is now only used when you specify MIME. This fixes a bug as reported by David Terracino.
5) Removed now unused "API Reference" link in HTML file supported the code.
PJN / 10-08-2002 1) Fixed a number of uncaught file exceptions in CPJNSMTPBodyPart::GetBody (Tick), CPJNSMTPMessage::SaveToDisk (Tick), and
CPJNSMTPConnection::SendMessage. Thanks to John Allan Miller for reporting this problem.
2) The CPJNSMTPConnection::SendMessage version of the method which sends a file directly from disk, now fails if the
file is empty.
3) Improved the sample app by displaying a wait cursor while a message from file is being sent
4) Improved the sample app by warning the user if mail settings are missing and then bringing up the configuration
dialog.
PJN / 20-09-2002 1) Fixed a problem where the code "Coder.EncodedMessage" was not being converted from an ASCII string to a UNICODE
string in calls to CString::Format. This was occurring when sending the username and password for "AUTH LOGIN" support
in addition to sending the "username digest" for "AUTH CRAM-MD5" support. Thanks to Serhiy Pavlov for reporting this
problem.
2) Removed a number of calls to CString::Format and instead replaced with string literal CString constructors instead.
PJN / 03-10-2002 1) Quoted printable encoding didn't work properly in UNICODE. (invalid conversion from TCHAR to BYTE). Thanks to
Serhiy Pavlov for reporting this problem.
2) Subject encoding didn't work properly in UNICODE. (invalid conversion from TCHAR to BYTE). Thanks to Serhiy Pavlov
for reporting this problem.
3) It didn't insert "charset" tag into root header for plain text messages (now it includes "charset" into plain text
messages too). Thanks to Serhiy Pavlov for reporting this problem.
PJN / 04-10-2002 1) Fixed an issue where the header / body separator was not being sent correctly for mails with attachments or when
the message is MIME encoded. Thanks to Serhiy Pavlov for reporting this problem.
2) Fixed a bug in QuotedPrintableEncode and HeaderEncode which was causing the errors. Thanks to Antonio Maiorano
for reporting this problem.
3) Fixed an issue with an additional line separator being sent between the header and body of emails. This was only
evident in mail clients if a non mime email without attachments was sent.
PJN / 11-12-2002 1) Review all TRACE statements for correctness
2) Provided a virtual OnError method which gets called with error information
PJN / 07-02-2003 1) Addition of a "BOOL bGracefully" argument to Disconnect so that when an application cancels the sending of a
message you can pass FALSE and close the socket without properly terminating the SMTP conversation. Thanks to
"nabocorp" for this nice addition.
PJN / 19-03-2003 1) Addition of copy constructors and operator= to CPJNSMTPMessage class. Thanks to Alexey Vasilyev for this suggestion.
PJN / 13-04-2003 1) Fixed a bug in the handling of EHLO responses. Thanks to "Senior" for the bug report and the fix.
PJN / 16-04-2003 1) Enhanced the CPJNSMTPAddress constructor to parse out the email address and friendly name rather than assume
it is an email address.
2) Reworked the syntax of the CPJNSMTPMessage::ParseMultipleRecipients method. Also now internally this function
uses the new CPJNSMTPAddress constructor
PJN / 19-04-2003 1) Fixed a bug in the CPJNSMTPAddress constructor where I was mixing up the friendly name in the "<>" separators,
when it should have been the email address.
PJN / 04-05-2003 1) Fixed an issue where the class doesn't convert the mail body and subject to the wanted encoding but rather changes
the encoding of the email in the email header. Since the issue of supporting several languages is a complicated one
I've decided that we could settle for sending all CPJNSMTPConnection emails in UTF-8. I've added some conversion functions
to the class that - at this point - always converts the body and subject of the email to UTF-8. A big thanks to Itamar
Kerbel for this nice addition.
2) Moved code and sample app to VC 6.
PJN / 05-05-2003 1) Reworked the way UTF8 encoding is now done. What you should do if you want to use UTF-8 encoding is set the charset
to "UTF-8" and use either QP or base 64 encoding.
2) Reworked the automatic encoding of the subject line to use the settings as taken from the root SMTP body part
3) Only the correct headers according to the MIME RFC are now encoded.
4) QP encoding is the encoding mechanism now always used for headers.
5) Headers are now only encoded if required to be encoded.
PJN / 12-05-2003 1) Fixed a bug where the X-Mailer header was being sent incorrectly. Thanks to Paolo Vernazza for reporting this problem.
2) Addition of X-Mailer header is now optional. In addition sample app now does not send the X-Mailer header.
3) The sample app now does not send the Reply-To header.
PJN / 18-08-2003 1) Modified the return value from the ConnectToInternet method. Instead of it being a boolean, it is now an enum. This
allows client code to differentiate between two conditions that it couldn't previously, namely when an internet connection
already existed or if a new connection (presumably a dial-up connection was made). This allows client code to then
disconnect if a new connection was required. Thanks to Pete Arends for this nice addition.
PJN / 05-10-2003 1) Reworked the CPJNSMTPConnection::ReadResponse method to use the timeout provided by IsReadable rather than calling sleep.
Thanks to Clarke Brunt for reporting this issue.
PJN / 03-11-2003 1) Simplified the code in CPJNSMTPConnection::ReadResponse. Thanks to Clarke Brunt for reporting this issue.
PJN / 03-12-2003 1) Made code which checks the Login responses which contain "Username" and "Password" case insensitive. Thanks to
Zhang xiao Pan for reporting this problem.
PJN / 11-12-2003 1) Fixed an unreferrenced variable in CPJNSMTPConnection::OnError as reported by VC.Net 2003
2) DEBUG_NEW macro is now only used on VC 6 and not on VC 7 or VC 7.1. This avoids a problem on these compilers
where a conflict exists between it and the STL header files. Thanks to Alex Evans for reporting this problem.
PJN / 31-01-2004 1) Fixed a bug in CPJNSMTPBodyPart::GetBody where the size of the body part was incorrectly calculating the size if the
encoded size was an exact multiple of 76. Thanks to Kurt Emanuelson and Enea Mansutti for reporting this problem.
PJN / 07-02-2004 1) Fixed a bug in CPJNSMTPBodyPart::SetText where the code would enter an endless loop in the Replace function. It has now
been replaced with CString::Replace. This now means that the class will not now compile on VC 5. Thanks to Johannes Philipp
Grohs for reporting this problem.
2) Fixed a number of warnings when the code is compiled with VC.Net 2003. Thanks to Edward Livingston for reporting
this issue.
PJN / 18-02-2004 1) You can now optionally set the priority of an email thro the variable CPJNSMTPMessage::m_Priority. Thanks to Edward
Livingston for suggesting this addition.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -