Sending Messages  
 

After a mail message has been created or imported, that message can be delivered to the recipients using the Simple Mail Transfer Protocol (SMTP) control. To send a message, simply follow these steps:

  • Create or import a message to be submitted for delivery
  • Create a list of one or more recipients for the message
  • Connect to the mail server, authenticating if needed
  • Submit the message to the mail server for delivery
  • Disconnect from the mail server

In the following example, the Mail Message control is used to compose a message and the SMTP control is used to submit the message to a mail server for delivery. Typically the name of the mail server would be provided by the user and would be the server provided by their Internet Service Provider.

With MailMessage1
    ' Create a standard mail message with the sender, recipients,
    ' subject and message body
    .ComposeMessage strFrom, strTo, strCc, , strSubject, strMessage

    ' Create a string which contains a comma separated list of all of
    ' the message recipients for use with the SendMessage method
    For nIndex = 0 To .Recipients - 1
        If Len(strRecipients) > 0 Then strRecipients = strRecipients & ", "
        strRecipients = strRecipients & .Recipient(nIndex)
    Next

    ' Connect to the mail server and display a message box if the
    ' connection fails for any reason
    nError = SmtpClient1.Connect(strServer)
    If nError Then
        MsgBox SmtpClient1.LastErrorString, vbExclamation
        Exit Sub
    End If

    ' Submit the message to the mail server
    nError = SmtpClient1.SendMessage(.From, strRecipients, .Message)
    If nError Then
        MsgBox SmtpClient1.LastErrorString, vbExclamation
        SmtpClient1.Disconnect
        Exit Sub
    End If

    ' Disconnect from the mail server after the message has
    ' been submitted for delivery
    SmtpClient1.Disconnect
End With