Send an Email Using ASP (CDOSYS)

For Windows Web Hosting packages

In this article, we'll provide an example that you can adapt with your own details to send an email using ASP.

CDOSYS, which replaced CDONTS in 2000, provides you with Collaboration Data Objects in order to send authenticated emails (requiring a username and password) in ASP.

Using the example below for testing purposes, you will need to replace some of the details provided:
 

<%
CONST SMTPSendUsing = 2 ' Send using Port (SMTP over the network)
CONST SMTPServer = "smtp.ionos.co.uk"
CONST SMTPServerPort = 25
CONST SMTPConnectionTimeout = 10 'seconds
CONST SMTPUser = "email@example.com"
CONST SMTPPassword = "password"

dim sSubject, sEmail, sMailBody, sFrom, sReadReceipt, sMsg
sSubject = "Test"
sEmail = "recipient@domain.com"
sMailBody = "This is a test message."
sFrom = "sender@domain.com"
sReadReceipt = true
sMsg = ""

On Error Resume Next

dim oMail, oConfig, oConfigFields
set oMail = Server.CreateObject("CDO.Message")
set oConfig = Server.CreateObject("CDO.Configuration")
set oConfigFields = oConfig.Fields

with oConfigFields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = SMTPSendUsing
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTPServerPort
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = SMTPUser
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = SMTPPassword
.Update
end with
oMail.Configuration = oConfig

oMail.Subject = sSubject
oMail.From = sFrom
oMail.To = sEmail
oMail.HTMLBody = sMailBody

oMail.Send
set oMail=nothing

sMsg = "Message Sent"

if Err.Number > 0 then sMsg = "ERROR: " & Err.Description

Response.Write sMsg

%>