Catalyst Internet Mail Control

As a developer who may have had some experience with SocketTools or other Internet software development toolkits, we thought it would be useful to show the difference between using a collection of e-mail related controls and the single Catalyst Internet Mail control. For this comparison, a simple Visual Basic application was created with a form that enables the user to compose a message:

We created two versions of the program, one using the SocketTools 3.6 e-mail components, and one using the Internet Mail control. The source code for the two versions is listed below. Excluding blank lines and comments, the version using the Catalyst Internet Mail control is less than 20 lines of code, calling just two methods, ComposeMessage and SendMessage. The SocketTools 3.6 version is a lower-level implementation that  is over 100 lines of code, using dozens of properties and methods in the MIME, DNS and SMTP controls.


Catalyst Internet Mail Control 6.0

SocketTools 3.6 Visual Edition

Option Explicit

Private Sub Command1_Click()
Dim strMessageHTML As String

Command1.Enabled = False
'
' Create a simple HTML version of the message
'
strMessageHTML = "<html><body><b>" & editMessage.Text & _
                 "</b></body></html>"

'
' Compose the message, specifying both a plain text and
' HTML text version of the message
'
InternetMail1.ComposeMessage editFrom.Text, _
    editTo.Text, editCc.Text, editBcc.Text, _
    editSubject.Text, editMessage.Text, _
    strMessageHTML
'
' Send the message to the recipients
'
If InternetMail1.SendMessage() <> 0 Then
  MsgBox "Unable to send the message" & vbCrLf & _
  InternetMail1.LastErrorString, vbExclamation
End If

Command1.Enabled = True
End Sub
Option Explicit

Private Declare Function GetTempPath Lib "Kernel32" _
  Alias "GetTempPathA" (ByVal cbPathName As Long, _
  ByVal strPathName As String) As Long

Private Declare Function GetTempFileName Lib "Kernel32" _
  Alias "GetTempFileNameA" (ByVal strPathName As String, _
  ByVal strPrefix As String, ByVal nUnique As Long, _
  ByVal strFileName As String) As Long

Private Sub Command1_Click()
Dim strMessage As String
Dim strTempPath As String
Dim strTempFile As String
Dim strRecipients() As String
Dim strRecipient As String
Dim strDomain As String
Dim strHostName As String
Dim bDelivered As Boolean
Dim nPos As Integer
Dim nRecipient As Integer
Dim nExchange As Integer
Dim nError As Integer

Command1.Enabled = False
'
' Create the main part of the message
'
MailMessage1.From = editFrom.Text
MailMessage1.To = editTo.Text
MailMessage1.Cc = editCc.Text
MailMessage1.Subject = editSubject.Text
MailMessage1.Date = Date & " " & Time
  
'
' Create the message part that will contain the plain text
' version of the message
'
MailMessage1.CreatePart
MailMessage1.SetHeader "Content-Type", "text/plain"
MailMessage1.SetHeader "Content-Transfer-Encoding", "7bit"
MailMessage1.Text = editMessage.Text

'
' Create a simple HTML version of the message
'
strMessage = "<html><body><b>" & editMessage.Text & _
             "</b></body></html>"
'
' Create the message part that will contain the HTML text
' version of the message
'
MailMessage1.CreatePart
MailMessage1.SetHeader "Content-Type", "text/html"
MailMessage1.SetHeader "Content-Transfer-Encoding", _
                       "quoted-printable"
MailMessage1.Text = strMessage
   
'
' Adjust the Content-Type header in the main message part,
' setting it to multipart/alternative
'
MailMessage1.Part = 0
MailMessage1.SetHeader "Content-Type", _
             "multipart/alternative; " & _
             "boundary=" & Chr(34) & _
             MailMessage1.Boundary & Chr(34)
'
' Create a temporary file that will be used to compose
' our message
'
strTempPath = Space(256)
If GetTempPath(255, strTempPath) = 0 Then
  MsgBox "Unable to determine temporary directory", _
         vbExclamation
  Command1.Enabled = True
  Exit Sub
End If
strTempPath = Left(strTempPath, _
              InStr(strTempPath, Chr$(0)) - 1)

strTempFile = Space(256)
If GetTempFileName(strTempPath, "~m", 0, strTempFile) = 0 Then
  MsgBox "Unable to create temporary file", vbExclamation
  Command1.Enabled = True
  Exit Sub
End If
strTempFile = Left(strTempFile, _
              InStr(strTempFile, Chr$(0)) - 1)

'
' Export the message to a temporary file; this will be used by
' the SMTP control's SendMessage method
'
nError = MailMessage1.ExportFile(strTempFile)
If nError Then
  MsgBox "Unable to save message in temporary file", _
         vbExclamation
  Command1.Enabled = True
  Exit Sub
End If

'
' Create an array of recipients for the message, using the
' comma as delimited that separates e-mail addresses
'
strRecipients = Split(editTo.Text & "," & _
                      editCc.Text & "," & _
                      editBcc.Text, ",")

'
' For each recipient in the list, deliver the message to
' that address
'
For nRecipient = 0 To UBound(strRecipients)
  strRecipient = Trim(strRecipients(nRecipient))
  nPos = InStr(strRecipient, "@")
  If nPos > 0 Then
    strDomain = Right(strRecipient, Len(strRecipient) - nPos)
    bDelivered = False: nExchange = 0
    '
    ' Use the DNS control to resolve the domain portion
    ' of the address, and obtain the mail exchanges which
    ' are responsible for accepting mail for that domain
    '
    DnsClient1.HostName = strDomain
    strHostName = DnsClient1.MailExchanges(0)
    Do While Len(strHostName) > 0
      '
      ' Attempt to connect to the mail server and
      ' deliver the message using the SendMessage
      ' method; if we cannot connect to the server,
      ' then try the next mail exchange in the list
      '
      SmtpClient1.AutoResolve = False
      SmtpClient1.HostName = strHostName
      nError = SmtpClient1.Connect()
      If nError = 0 Then
        nError = SmtpClient1.SendMessage(editFrom.Text, _
                              strRecipient, strTempFile)
        If nError = 0 Then bDelivered = True
          SmtpClient1.Disconnect
          Exit Do
        End If
      nExchange = nExchange + 1
      strHostName = DnsClient1.MailExchanges(nExchange)
    Loop
    If bDelivered = False Then
      MsgBox "Unable to deliver message to " & strRecipient, _
             vbExclamation
    End If
  End If
Next

Kill strTempFile
Command1.Enabled = True
End Sub