ASP Mail - CDO
This code shows how to send authenticated email using classic ASP and the CDO object. To send authenticated mail you need to pass your email account user name and password to the SMTP server, which this code does. This could also be used from in a vbs file if you are not using ASP, and just need some code to run locally.
Aquest Hosting customers:
You must use smtp authentication when sending mail from our servers which means that you must first setup your email account on our system if you are using our SMTP server.
- Code running on our servers must use the smtp server: "hostmail.aquesthosting.com"
- Code running outside our network needs to use: "smtp.[yourDomainName].com"
- UserName: your full valid email address (ie: bob@abc.com)
- Password: your email password
Not an Aquest Hosting customer?
Well you should be :)
You can use this code, but you need to know your smtp server, user name and password. If you don't need to use authentcated mail, then comment out the lines that have to do with authentication.
And now, the code...
Sub SendCDOMail(MailTo, MailFrom, subject, body, smtpServer, mailUserName, mailPassword)
Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing"
Const cdoSendUsingPort = 2
Const cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
Const cdoSMTPServerPort = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Const cdoSMTPConnectionTimeout = "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Const cdoSMTPAuthenticate = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Const cdoBasic = 1
Const cdoSendUserName = "http://schemas.microsoft.com/cdo/configuration/sendusername"
Const cdoSendPassword = "http://schemas.microsoft.com/cdo/configuration/sendpassword"
Dim objConfig ' As CDO.Configuration
Dim objMessage ' As CDO.Message
Dim Fields ' As ADODB.Fields
' Get a handle on the config object and it's fields
Set objConfig = CreateObject("CDO.Configuration")
Set Fields = objConfig.Fields
' Set config fields we care about
With Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = smtpServer
.Item(cdoSMTPServerPort) = 25
.Item(cdoSMTPConnectionTimeout) = 10
.Item(cdoSMTPAuthenticate) = cdoBasic
.Item(cdoSendUserName) = mailUserName
.Item(cdoSendPassword) = mailPassword
.Update
End With
Set objMessage = CreateObject("CDO.Message")
Set objMessage.Configuration = objConfig
With objMessage
.To = MailTo
.From = MailFrom
.Subject = subject
.HTMLBody = body
.Send
End With
Set Fields = Nothing
Set objMessage = Nothing
Set objConfig = Nothing
End sub