SendMessage Method  
 

Submit the specified message to the mail server for delivery.

Syntax

object.SendMessage( Sender, Recipients, Message, [Options] )

Parameters

Sender
A string argument which specifies the email address of the person sending the message. This typically corresponds to the address in the From header of the message, but it is not required that they be the same.
Recipients
A string argument which specifies the email address of the person or persons to receive the message. Multiple addresses may be specified by separating each address with a comma. It should be noted that this protocol is only concerned with the delivery of a message and not its contents. Header fields in the message are not parsed to automatically determine the recipients. This argument should be a concatenation of all recipients, including carbon copies and blind carbon copies, with each address separated with a comma.
Message
A string argument contains the message to be delivered to the specified recipients. The message must be text and conform to the basic structure defined in RFC 822. There must be one or more headers separated by a blank line, followed by the body of the message. Each line of text must be terminated by a carriage return and linefeed character sequence. Note that more complex multipart MIME messages may also be used, but it is recommended that you use the Mail Message control to compose them.
Options
An optional argument that is reserved for future use. This argument should be omitted when calling this method.

Return Value

A value of zero is returned if the operation was successful, otherwise a non-zero error code is returned which indicates the cause of the failure.

Remarks

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.

Example

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

See Also