Visual Basic Code of the Week (COTW)
Issue #97
Online Version at (paid subscribers only)
All content and source code is Copyright (c) 1999 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp. Word, Excel and Visual Basic are trademarks of Microsoft Corp.

Cool Software

Download a free copy of NetMon - Your Internet Performance Monitor at


In this Issue

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


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

If you have any questions about using this class, let us know at


Public SMTPHost As String

IP address or domain name of the SMTP server or host. An example of this would be

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 or

Public FromUsername As String

Your fully qualified username. It should be in the format

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.

Sample Usage

This sample will send a message to the mail server. It will be addressed to and from 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.DomainName = ""
    SMTP.FromUsername = ""
    SMTP.ToUsername = ""
    SMTP.Subject = "Where's VB7?"
    SMTP.MessageText = "Bill, Please get VB7 out ASAP.  Thanks, COTW"

Source Code

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:

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945