SMTP Client Library with Mail Server Authentication
By David E. Beseke
  Download Source Code

I was recently searching the internet looking for a component to use for sending email from an application via SMTP.  I found a very nice component by Dr. Peter A. Bromberg on the EggHead Cafe website that looked like it was exactly what I needed.  Upon further investigation, I found that it had most of the features I required, but there were a couple of things it didn't have that I could not live without.  I am a firm believer in the concept of giving something in order to get something back in return.  With that in mind, I downloaded the original component and set about the task of enhancing it to get what I needed.  Once I was finished, I contacted Dr. Bromberg to get his permission to put the enhanced version of the component back on the site with proper credit given to it's originator.

The original author did a very good job of putting this component together, so it was a fairly simple task to enhance it.  The main thing the component lacked was the ability to send a user name and password to an SMTP server that required authentication.  Since this is a good way to keep spammers from hijacking mail servers, I have a feeling more mail servers will require this in the future.  I added the code to check for a user name and password on the connection to the mail server.  If the information is not provided, the component assumes it is not required and attempts to send the message without authenticating.  If a user id and password are sent, authentication is performed.
I also added some variables to allow the developer to get the last error code and error message returned from the mail server when a failure is encountered.  On each call to the Send procedure, a short label is passed that represents the operation being performed.  If a failure occurs, that string is placed in the variable lastOperation.  In addition, the last error code and error message text are placed in the variables replyCode and replyString.
As I was finishing this article it also occurred to me that it would be nice to be able to send HTML formatted messages with this component.  So, I added another field to the library, isHTMLFormat, that allows the developer to send a message formatted as a web page.  The field defaults to false.  This feature only works if the message does not contain an attachment.
I have included a demonstration program that was also based upon the original author's work, but now includes a user id and password field for passing authentication information.  I hope this component helps other developers save some time in the same way that the original version helped me.  Let's keep these exchanges going - everyone involved will benefit!  Again, many thanks to Dr. Bromberg for publishing the original work this was based upon.
Please take a moment to rate this article (opens new browser window).  Rate Article

About the author
David is an independent consultant with 30 years experience related to information processing.  He currently does the bulk of his contract work through Strategic Software Designs, Inc. based in Naperville, IL.  Most of his current projects involve website and database design utilizing Microsoft platforms such as C# and .Net and Borland's Delphi.  He can be reached at