๐ง Classic ASP โ Email Handling โ Send Emails Using CDOSYS and SMTP
๐งฒ Introduction โ What Is Email Handling in Classic ASP?
Email handling in Classic ASP allows your web applications to send automated emailsโlike contact form submissions, user confirmations, or admin notificationsโusing SMTP and CDOSYS (Collaboration Data Objects). With Classic ASP and VBScript, you can connect to any SMTP server and send messages in plain text or HTML format.
๐ฏ In this guide, youโll learn:
- How to send emails using CDOSYS in Classic ASP
- Configure SMTP settings (localhost or external)
- Handle form data and email it to recipients
- Use plain text and HTML email formats with examples
๐ค Basic CDOSYS Email Sending Example
<%
Dim mail
Set mail = Server.CreateObject("CDO.Message")
mail.Subject = "Test Email"
mail.From = "your_email@example.com"
mail.To = "recipient@example.com"
mail.TextBody = "This is a test message from Classic ASP."
' Configure SMTP settings
mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.yourserver.com"
mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
mail.Configuration.Fields.Update
mail.Send
Set mail = Nothing
%>
๐งช Output:
An email is sent from your_email@example.com to recipient@example.com.
๐ฉ Handling Email Form Submission
๐ contact.asp โ Form Page
<form method="post" action="sendmail.asp">
Name: <input type="text" name="username"><br>
Email: <input type="text" name="useremail"><br>
Message:<br>
<textarea name="usermessage"></textarea><br>
<input type="submit" value="Send">
</form>
๐ sendmail.asp โ Processing and Sending Email
<%
Dim name, email, message
name = Request.Form("username")
email = Request.Form("useremail")
message = Request.Form("usermessage")
Dim mail
Set mail = Server.CreateObject("CDO.Message")
With mail
.Subject = "New Contact Form Message"
.From = email
.To = "admin@example.com"
.TextBody = "Name: " & name & vbCrLf & _
"Email: " & email & vbCrLf & _
"Message: " & message
' SMTP Config
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.yourserver.com"
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Configuration.Fields.Update
.Send
End With
Set mail = Nothing
Response.Write "Email Sent Successfully!"
%>
๐ก Sending HTML Emails
mail.HTMLBody = "<h2>Welcome!</h2><p>This is a <b>HTML</b> message.</p>"
Set HTMLBody instead of TextBody to format the email using HTML.
๐ Using SMTP Authentication (Optional)
mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your_smtp_user"
mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your_smtp_pass"
๐ Common CDOSYS Field Configuration
| Field | Purpose |
|---|---|
sendusing = 2 | Use network (SMTP) |
smtpserver | Your SMTP host |
smtpserverport | Port (default is 25 or 587) |
sendusername / sendpassword | Auth credentials |
smtpauthenticate | 0 = None, 1 = Basic |
smtpusessl (set to true) | Use SSL for secure emails |
โ ๏ธ Common Issues & Solutions
| Issue | Solution |
|---|---|
| โSendUsingโ or โBad Gatewayโ errors | Check SMTP server address, port, and credentials |
| Email not received | Check spam folder; verify From/To domains |
| ASP error 500 | Confirm correct syntax and that CDOSYS is enabled in IIS |
๐ Best Practices for Email Handling
โ Do:
- Sanitize form inputs before using in email
- Use
HTMLBodyfor rich formatting - Use
Configuration.Fields.Updatebefore sending
โ Avoid:
- Using user-supplied input directly in headers (security risk)
- Hardcoding credentials in production code
- Skipping error handling in live environments
๐ Summary โ Recap & Next Steps
Email handling in Classic ASP is made possible via CDOSYS and a properly configured SMTP server. Whether you’re processing a form or sending welcome messages, Classic ASP gives you the tools to automate email delivery securely and flexibly.
๐ Key Takeaways:
- Use CDOSYS via
Server.CreateObject("CDO.Message") - Configure SMTP with
.Configuration.Fields - Use
TextBodyorHTMLBodyfor message content
โ๏ธ Real-world Use Cases:
- Contact form submission notifications
- Password reset and login confirmation emails
- Admin alerts for order or feedback processing
โ FAQs โ Classic ASP Email Handling
โ Do I need IIS to send email in Classic ASP?
โ
Yes. Classic ASP requires IIS and CDOSYS (enabled by default) to send emails.
โ Can I send emails via Gmail SMTP?
โ
Yes, with port 587, smtpauthenticate = 1, and smtpusessl = true. Make sure less secure apps are allowed or use an app password.
โ Whatโs the difference between TextBody and HTMLBody?
โ
TextBody sends plain text. HTMLBody allows formatting, styles, and clickable links.
Share Now :
