Download a free copy of NetMon - Your Internet Performance Monitor at http://www.codeoftheweek.com/netmon/index.html
In this issue we show how to send an email message using the Winsock control and a single class with one method and a few properties. This class is limited to sending a single text based message to a single user.
Questions? Email us at email@example.com.
This issue allows you to easily add the ability to send email using the SMTP protocol to your applications. This is the standard email method used on the Internet. Most large email systems support this as well (such as Novell Groupwise, Lotus Notes and Microsoft Exchange).
If you want to send more than one message simultaneously you will need to create multiple instances of this class and multiple copies of the winsock control. We have tested this and it does work. You can use this idea to create a very fast mailer. If you are interested in a very fast bulk mailer which can be used for sending out newsletters or emails like Code of the Week, let us know at firstname.lastname@example.org
If you have any questions about using this class, let us know at email@example.com
Public SMTPHost As String
IP address or domain name of the SMTP server or host. An example of this would be smtp.codeoftheweek.com
Public SMTPPort As Long
The SMTP port number of the SMTP server. The default is 25.
Public DomainName As String
This is the domain you are sending your email from. This should be something like abc.com or hotmail.com
Public FromUsername As String
Your fully qualified username. It should be in the format firstname.lastname@example.org
Public ToUsername As String
The fully qualified username you are sending mail to.
Public MessageText As String
Any message text. It can contain carriage return/line feeds to support multi-line messages.
Public Subject As String
The subject of the message you are sending.
Public WithEvents WinSockControl As Winsock
You must assign this property with the Winsock control you have dropped on your form. This class will not work without assigning this property.
Public Sub SendMessage()
Once you have set all the above properties you call this method to actually send the message. This routine will raise an error if the message can not be sent. It will also raise an error if it is in the process of sending a message and you call it recursively.
See source code.
This sample will send a message to the smtp.codeoftheweek.com mail server. It will be addressed to email@example.com and from firstname.lastname@example.org. It assumes you have a form called Form1 which has the WinSock control on it called wsControl.
Dim SMTP As New cSimpleSMTP Set SMTP.WinSockControl = Form1.wsControl SMTP.SMTPHost = "smtp.codeoftheweek.com" SMTP.DomainName = "codeoftheweek.com" SMTP.FromUsername = "email@example.com" SMTP.ToUsername = "firstname.lastname@example.org" SMTP.Subject = "Where's VB7?" SMTP.MessageText = "Bill, Please get VB7 out ASAP. Thanks, COTW" SMTP.SendMessage
To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address: http://www.codeoftheweek.com/membersonly/bi/0097.html