Share via


Sending SMTP Mail by Port 25 Using CDOSYS (Visual Basic)

Topic Last Modified: 2006-06-11

Example

Visual Basic

'Sending SMTP mail via port 25 using CDOSYS
'This VB sample uses CDOSYS to send SMTP mail using the cdoSendUsingPort option and specifying a SMTP host.

' Note: It is recommended that all input parameters be validated when they are
' first obtained from the user or user interface.
Private Sub SendMessage(strTo As String, strFrom As String)

'Send using the Port on a SMTP server
Dim iMsg As New CDO.Message
Dim iConf As New CDO.Configuration
Dim Flds As ADODB.Fields
Dim strHTML

Set Flds = iConf.Fields

With Flds
    .Item(cdoSendUsingMethod) = cdoSendUsingPort
    .Item(cdoSMTPServer) = "mail.example.com"
    'Use SSL to connect to the SMTP server:
    '.Item(cdoSMTPUseSSL) = True
    .Item(cdoSMTPConnectionTimeout) = 10
    .Update
End With

' Build HTML for message body
strHTML = "<HTML>"
strHTML = strHTML & "<HEAD>"
strHTML = strHTML & "<BODY>"
strHTML = strHTML & "<b> This is the test HTML message body</b></br>"
strHTML = strHTML & "</BODY>"
strHTML = strHTML & "</HTML>"

With iMsg
    Set .Configuration = iConf
    .To = strTo
    .From = strFrom
    .Subject = "This is a test CDOSYS message (Sent by Port)"
    .HTMLBody = strHTML
    '.TextBody = "This is the text body of the message..."

    .Send
End With

' cleanup of variables
Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing

End Sub