Using Windows Explorer, create a new directory where you can build the ActiveX E-Mail component for your e-mail information system. Name the directory \VBMAIL.
Start a new Visual Basic 5.0 ActiveX DLL project. Access the properties for your new project by choosing Project1 Properties from the Project menu. On the General tab in the Project Properties dialog box, change the project name to VBMail and change the project description to Active Server Pages E-Mail Component. Click OK.
When the project is first opened, a default class named CLASS1 is created. Change the name of this default class to Connector.
Save the project in the directory \VBMAIL.
You can create the e-mail connector by using Winsock, an ActiveX control that ships with Visual Basic 5.0 and allows a Visual Basic component to access the Internet. With Winsock, you can automatically send e-mail.
Because Winsock is an ActiveX control, you need to add a form to your project to host the control, even though the component will not have a user interface. To add this form, choose Add Form from the Project menu and name the form frmVBMail.
Choose Components from the Project menu. In the Components dialog box, select Microsoft Winsock Control 5.0 and click OK.
Add the Winsock control to your newly created form by double-clicking the control in the Visual Basic Toolbox. Change the name of the control to sockVBMail. Save your project.
In Active Server Pages code, the Connector class will be the primary interface for e-mail. This class must include properties that allow you to designate the sender and receiver of the mail, as well as the mail content. Add the following code to the Connector class to create the necessary properties for the E-Mail component:
` Variables Private m_Sender As String Private m_Recipient As String Private m_SenderName As String Private m_RecipientName As String Private m_Subject As String Private m_Body As String Private m_Server As String ` Property procedures Public Property Get Sender() As String Sender = m_Sender End Property Public Property Let Sender(strSender As String) m_Sender = strSender End Property Public Property Get Recipient() As String Recipient = m_Recipient End Property Public Property Let Recipient(strRecipient As String) m_Recipient = strRecipient End Property Public Property Get SenderName() As String SenderName = m_SenderName End Property Public Property Let SenderName(strSenderName As String) m_SenderName = strSenderName End Property Public Property Get RecipientName() As String RecipientName = m_RecipientName End Property Public Property Let RecipientName(strRecipientName As String) m_RecipientName = strRecipientName End Property Public Property Get Subject() As String Subject = m_Subject End Property Public Property Let Subject(strSubject As String) m_Subject = strSubject End Property Public Property Get Body() As String Body = m_Body End Property Public Property Let Body(strBody As String) m_Body = strBody End Property Public Property Get Server() As String Server = m_Server End Property Public Property Let Server(strServer As String) m_Server = strServer End Property
After all the information is provided to the component through the properties, you can call the Send method of the Connector class to send e-mail. To create the Send method, choose Add Procedure from the Tools menu. In the Add Procedure dialog box, set the following attributes:
Name: Send Type: Function Scope: Public
After the Send function is added, designate the return data type as Boolean. The complete function definition should appear as follows:
Public Function Send() As Boolean End Function
You will complete the Send method shortly, but first add some supporting functionality to the component.
Because many of the steps involved in sending e-mail require a response from the e-mail server, you need to create a routine that causes the E-Mail component to wait for the server's response. This can be accomplished with a private routine named GetResponse. Add the routine to the Connector class by choosing Insert Procedure from the Tools menu. In the Insert Procedure dialog box, set the following attributes:
Name: GetResponse Type: Sub Scope: Private
After the GetResponse routine is created, add the following code to force a wait for the server's response:
Dim strTemp As String strTemp = "" Do While strTemp = "" frmVBMail.sockVBMail.GetData strTemp DoEvents Loop
In this step, you will create the code necessary to use SMTP, the protocol that allows communication between the e-mail server and the client. Although this project was originally designed to use Microsoft Exchange Server 5.0, you can use any e-mail server that supports SMTP.
Return to the Send method, and add the following code to connect to the SMTP mail server:
With frmVBMail.sockVBMail Send = True ` Connect to SMTP server If .State <> sckConnected And .State <> sckOpen Then .Connect Server, 25 Do While .State <> sckConnected DoEvents Loop End If
Once the E-Mail component is successfully connected to the e-mail server, you can construct and send a message using SMTP. The message is sent to the server in parts until each necessary piecesender, recipient, bodyis received. Then the server sends the message to the recipient. Add the following code to the Send method to build and send an e-mail message:
On Error GoTo SendErr With frmVBMail.sockVBMail Send = True ` Connect to SMTP server If .State <> sckConnected And .State <> sckOpen Then .Connect Server, 25 Do While .State <> sckConnected DoEvents Loop End If ` Create the message Dim strMessage As String ` The sender strMessage = "MAIL FROM:" & Sender & vbCrLf .SendData strMessage GetResponse ` The recipient strMessage = "RCPT TO:" & Recipient & vbCrLf .SendData strMessage GetResponse ` The body of the message strMessage = "DATA" & vbCrLf .SendData strMessage GetResponse strMessage = "DATE:" & Format$(Now, "dd/mm/yy") & vbCrLf strMessage = strMessage & "FROM:" & SenderName & vbCrLf strMessage = strMessage & "TO:" & RecipientName & vbCrLf strMessage = strMessage & "SUBJECT:" & Subject & vbCrLf & vbCrLf strMessage = strMessage & Body & vbCrLf strMessage = strMessage & "." & vbCrLf .SendData strMessage GetResponse
After the message is sent, you must close the connection to the e-mail server. Use the Close method of the Winsock control to close the connection and complete the Send method code:
` Close connection .Close Do While .State <> sckClosed DoEvents Loop End With SendExit: Exit Function SendErr: Send = False Resume SendExit
Save your project.
Before you can use the E-Mail component in an ASP page, you must compile the DLL by choosing Make VBMail.dll from the File menu. Then save your work, and exit Visual Basic.