Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / VBScript
Tip/Trick

Send Email using Classic ASP

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
3 Apr 2014CPOL1 min read 84.9K   2.5K   7   5
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:

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

Set the SMTP server and authentication information:

VB.NET
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:

VB.NET
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.

VB.NET
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSend Email using Classic ASP Pin
Member 1557807311-Apr-22 17:51
Member 1557807311-Apr-22 17:51 
Questionerror '80040211' /Default.asp, line 39 Pin
Member 141064482-Jan-19 18:52
Member 141064482-Jan-19 18:52 
QuestionTo: Pin
Member 1018411822-Jul-14 11:17
Member 1018411822-Jul-14 11:17 
AnswerRe: To: Pin
Code Help 201422-Jul-14 19:16
professionalCode Help 201422-Jul-14 19:16 
GeneralRe: To: Pin
Member 1018411823-Jul-14 5:24
Member 1018411823-Jul-14 5:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.