65.9K
CodeProject is changing. Read more.
Home

Send Email using Classic ASP

starIconstarIconstarIconstarIconstarIcon

5.00/5 (4 votes)

Apr 3, 2014

CPOL

1 min read

viewsIcon

88086

downloadIcon

2558

This is a complete working example with code on how to send email using classic ASP.

Introduction

This tip explains how to send email using classic ASP. I am using gmail as relay server in this tip.

Background

I have seen a lot of people trying to send emails using classic ASP, but face some issues. Although there are lot of code snippets available, but here I am sharing a complete working ASP file that can be used to send emails.

Using the Code

In this example, I will use CDO.Message for sending email from classic ASP code. The code is tested using a Gmail account, but you can use any valid email account from any provider. The attached file is a complete working example and is tested in live setup. To test this example, you have to set the SMTP server and authentication information. Here is the explanation of the code.

To send email, first of all, create an object of CDO.Message:

dim objMail 
Set objMail = Server.CreateObject("CDO.Message") 

Set the SMTP server and authentication information:

dim smtpServer, yourEmail, yourPassword
smtpServer = "smtp.gmail.com"
yourEmail = "youremail@gmail.com"     'replace with a valid gmail account
yourPassword = "yourpassword"   'replace with a valid password for account set in yourEmail 

Replace youremail@gmail.com and yourpassword with the email id and password of a valid gmail account.

Now set the various configuration properties:

objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpServer
objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465 
objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = yourEmail
objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = yourPassword
objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60 
objMail.Configuration.Fields.Update 

Now prepare your actual message and send email using Send method.

objMail.Subject="Application Form Registration Details"
objMail.htmlBody = "This is test message"
objMail.Send   

Points of Interest

The default port for SMTP is 25. If you are using gmail server, i.e., smtp.gmail.com, then you have to use port number 465. Whichever SMTP server you are using, set the smtpserver and smtpserverport according to that. Most people face the issue in email due to incorrect port number.