The SendMessage method sends an email message to one or
more recipients. For most applications, it is recommended that
messages be submitted through an SMTP relay server provided by the
user's email service or organization. Port 587 is commonly used for
message submission with explicit TLS negotiation using STARTTLS,
while port 465 is commonly used for implicit TLS connections.
The Secure property and related options control the use of
TLS when connecting to the mail server.
SMTP servers typically require the client to authenticate before
accepting messages for delivery. Traditional authentication provides a
user name and password with the Authenticate method. For
servers that support OAuth 2.0, the application can specify an access
token using the BearerToken property. By default, the control
uses the XOAUTH2 authentication mechanism when a bearer token is provided.
The application is responsible for obtaining a valid access token
from the mail service and refreshing it when necessary. Authentication
credentials and bearer tokens should only be transmitted over a
secure TLS connection.
Most SMTP servers require a secure TLS connection before allowing
a client to authenticate. Servers that support explicit TLS using
STARTTLS may refuse authentication commands until the secure
connection has been established, while servers configured for
implicit TLS require the secure connection to be established
immediately upon connecting. Applications should enable the
Secure property and configure the appropriate connection
options for the relay server. Attempting to authenticate over an
unencrypted connection may result in the server rejecting the
authentication command or immediately terminating the connection
without returning an error. This behavior is commonly encountered
with servers that enforce strict security policies.
Using an authenticated SMTP relay server avoids many of the
problems associated with direct delivery and allows the mail service
to handle message routing and delivery. Direct delivery of messages
using port 25 is supported for compatibility with
existing applications, but it is not recommended for most client
applications. Many networks and Internet service providers restrict
outbound connections on port 25, and receiving mail servers may
reject messages originating from residential networks or systems
without appropriate DNS records, sender authentication policies
and an established sending reputation.
This method will cause the current thread to block until the
message transfer completes, a timeout occurs or the transfer is
canceled. During the transfer, the OnProgress event will fire
periodically, enabling the application to update any user interface
objects such as a progress bar.
The following example demonstrates how to use the
SendMessage method, along with the MailMessage control, to
compose and deliver a message:
' Compose a new message
nError = MailMessage1.ComposeMessage(strFrom, _
strTo, _
strCc, _
strBcc, _
strSubject, _
strMessageText)
If nError > 0 Then
MsgBox MailMessage1.LastErrorString, vbExclamation
Exit Sub
End If
If MailMessage1.Recipients = 0 Then
MsgBox "There are no recipients for this message"
Exit Sub
End If
' Connect to the mail server
nError = SmtpClient1.Connect(strMailServer)
If nError > 0 Then
MsgBox SmtpClient1.LastErrorString, vbExclamation
Exit Sub
End If
' Deliver the message
nError = SmtpClient1.SendMessage(MailMessage1.Sender, MailMessage1.AllRecipients, MailMessage1.Message)
If nError > 0 Then
MsgBox SmtpClient1.LastErrorString, vbExclamation
SmtpClient1.Disconnect
Exit Sub
End If
' Disconnect
SmtpClient1.Disconnect