ASP Sending E-Mail with CDOSYS

Welcome to The Coding College, your trusted resource for coding tutorials and web development techniques. In this article, we’ll explore how to send emails using CDOSYS in classic ASP.

CDOSYS (Collaboration Data Objects for Windows 2000 System) is a built-in component in Windows that facilitates sending emails via SMTP. It’s a robust solution for integrating email functionality into your ASP applications.

What Is CDOSYS?

CDOSYS is a Microsoft COM-based library for sending emails. It replaces the older CDONTS component and is more efficient, offering enhanced support for MIME types and SMTP configurations.

Key Features of CDOSYS:

  • Works with SMTP to send emails.
  • Supports attachments and custom headers.
  • Compatible with modern email protocols and security standards.

Why Use CDOSYS in ASP?

CDOSYS is built into the Windows operating system, making it a readily available and reliable solution for sending emails in ASP applications.

Benefits:

  • No additional libraries or installations are needed.
  • Works seamlessly with IIS (Internet Information Services).
  • Can send HTML and plain-text emails.
  • Supports embedded images and attachments.

Step-by-Step Guide to Sending Email with CDOSYS

1. Setting Up Your SMTP Server

To use CDOSYS, you’ll need an SMTP server for email delivery. Options include:

  • Local SMTP Server: Provided by IIS.
  • External SMTP Servers: Services like Gmail, Outlook, or a third-party SMTP provider.

2. Sending a Basic Email

Here’s how to send a simple email using CDOSYS.

ASP Code: Sending Plain Text Email

<%
Dim objEmail, smtpConfig

' Create email object
Set objEmail = Server.CreateObject("CDO.Message")

' Set email properties
objEmail.From = "[email protected]"
objEmail.To = "[email protected]"
objEmail.Subject = "Test Email from ASP with CDOSYS"
objEmail.TextBody = "Hello, this is a test email sent using CDOSYS in ASP."

' Configure the SMTP server
Set smtpConfig = objEmail.Configuration.Fields
smtpConfig.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
smtpConfig.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.com"
smtpConfig.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
smtpConfig.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
smtpConfig.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your_smtp_username"
smtpConfig.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your_smtp_password"
smtpConfig.Update

' Send the email
objEmail.Send

' Clean up
Set objEmail = Nothing
Set smtpConfig = Nothing

Response.Write("Email sent successfully!")
%>

3. Sending an HTML Email

CDOSYS supports rich HTML content, allowing you to send visually appealing emails.

ASP Code: Sending HTML Email

<%
Dim objEmail, smtpConfig

' Create email object
Set objEmail = Server.CreateObject("CDO.Message")

' Set email properties
objEmail.From = "[email protected]"
objEmail.To = "[email protected]"
objEmail.Subject = "HTML Email from ASP"
objEmail.HTMLBody = "<h1>Hello!</h1><p>This is an HTML email sent using <b>CDOSYS</b>.</p>"

' Configure the SMTP server
Set smtpConfig = objEmail.Configuration.Fields
smtpConfig.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
smtpConfig.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.com"
smtpConfig.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
smtpConfig.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
smtpConfig.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your_smtp_username"
smtpConfig.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your_smtp_password"
smtpConfig.Update

' Send the email
objEmail.Send

' Clean up
Set objEmail = Nothing
Set smtpConfig = Nothing

Response.Write("HTML email sent successfully!")
%>

4. Adding Attachments

You can easily attach files to your emails with CDOSYS.

ASP Code: Sending Email with Attachments

<%
Dim objEmail, smtpConfig

' Create email object
Set objEmail = Server.CreateObject("CDO.Message")

' Set email properties
objEmail.From = "[email protected]"
objEmail.To = "[email protected]"
objEmail.Subject = "Email with Attachment"
objEmail.TextBody = "Please find the attached document."

' Add attachment
objEmail.AddAttachment Server.MapPath("files/document.pdf")

' Configure the SMTP server
Set smtpConfig = objEmail.Configuration.Fields
smtpConfig.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
smtpConfig.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.com"
smtpConfig.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
smtpConfig.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
smtpConfig.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your_smtp_username"
smtpConfig.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your_smtp_password"
smtpConfig.Update

' Send the email
objEmail.Send

' Clean up
Set objEmail = Nothing
Set smtpConfig = Nothing

Response.Write("Email with attachment sent successfully!")
%>

Troubleshooting Common Issues

1. SMTP Server Errors

  • Verify the SMTP server address and port.
  • Ensure authentication credentials are correct.

2. Email Delivery Issues

  • Check spam filters and email whitelists.
  • Use proper SPF, DKIM, and DMARC configurations.

3. Debugging Tips

  • Use On Error Resume Next to capture errors.
  • Log errors to a file or display them during development.

Best Practices for Sending Emails with CDOSYS

  1. Secure Your SMTP Credentials:
    • Store credentials securely, avoid hardcoding them.
  2. Use TLS/SSL for Security:
    • If your SMTP server requires encryption, set the smtpusessl configuration field to true.
  3. Test Thoroughly:
    • Test emails with different content types, recipients, and attachments.
  4. Handle Errors Gracefully:
    • Provide meaningful feedback to users if an email fails to send.

Conclusion

CDOSYS makes it easy to integrate email functionality into classic ASP applications. With support for plain text, HTML, and attachments, it’s a versatile tool for meeting your web application’s communication needs.

For more tutorials like this, visit The Coding College and enhance your programming knowledge with expert guidance.

Leave a Comment