ASP Sending Email with CDOSYS

CDOSYS, accessed in ASP through the CDO.Message object, lets a classic ASP page send real email by setting properties like To, From, Subject and TextBody, configuring an SMTP server through Configuration.Fields, and calling Send.

What CDOSYS Is

CDOSYS (Collaboration Data Objects for Windows 2000) is the email library built into Windows Server and IIS. In classic ASP you use it through a single object, CDO.Message, which represents one email — its recipients, subject, body, and the SMTP server it should be sent through.

CDOSYS replaced an older, simpler component called CDONTS that shipped with earlier versions of Windows NT. CDONTS is deprecated and often missing entirely on modern servers, so CDO.Message is the object you should reach for in any current classic ASP project that needs to send mail.

The CDO.Message Object

  • To — the recipient's email address
  • From — the sender's email address shown in the message
  • Subject — the subject line
  • TextBody (or HTMLBody) — the content of the message
  • Configuration — the Fields collection that tells CDO which SMTP server to use

Example

<%
Dim objMessage
Set objMessage = Server.CreateObject("CDO.Message")

objMessage.To = "someone@example.com"
objMessage.From = "noreply@example.com"
objMessage.Subject = "Welcome to our site"
objMessage.TextBody = "Thanks for signing up! We're glad you're here."

objMessage.Send

Set objMessage = Nothing
%>

Configuring an SMTP Server

The example above only works if the server has a local SMTP pickup directory configured, which is rare on shared or modern hosting. In practice you almost always need to tell CDO which SMTP server to connect to, on which port, and with which login. You do that by setting items in objMessage.Configuration.Fields using the CDO configuration schema, then calling .Update so the settings take effect.

Example

<%
Dim objMessage
Set objMessage = Server.CreateObject("CDO.Message")

objMessage.To = "someone@example.com"
objMessage.From = "noreply@example.com"
objMessage.Subject = "Your order has shipped"
objMessage.TextBody = "Your order #4521 shipped today and should arrive in 3-5 business days."

With objMessage.Configuration.Fields
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.com"
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "mailuser@example.com"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "MailPassword123"
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
    .Update
End With

On Error Resume Next
objMessage.Send
If Err.Number <> 0 Then
    Response.Write "Email failed: " & Err.Description
    Err.Clear
Else
    Response.Write "Email sent successfully."
End If
On Error Goto 0

Set objMessage = Nothing
%>
FieldPurpose
sendusing2 sends through an SMTP server over the network; 1 sends through the local pickup directory
smtpserverThe hostname of the SMTP server, such as smtp.example.com
smtpserverportThe port to connect on, commonly 25, 587, or 465
smtpauthenticateSet to 1 to log in with a username and password before sending
sendusername / sendpasswordThe credentials for the SMTP account
smtpusesslSet to 1 to encrypt the connection with SSL/TLS
Note: Wrap objMessage.Send in On Error Resume Next and check Err.Number afterward. SMTP problems — a wrong password, a blocked port, an unreachable server — raise a runtime error, and an unhandled one will crash the whole page instead of showing a friendly message.
  • Many hosts block outbound port 25, so port 587 or 465 is usually needed for real delivery
  • Consumer webmail providers often require an app password instead of the normal account password
  • Forgetting to call Configuration.Fields.Update means the settings you assigned are silently ignored
  • Sending formatted mail means setting HTMLBody instead of TextBody; CDO sets the correct MIME type automatically

Exercise: ASP Sending Email

What are components like CDOSYS commonly used for in classic ASP?