Sending eMail in ASP

Tweet

Sending email in Active Server Pages isn’t at complex as it seems. All you need is an ASP Page that contains your code, a host that supports ASP and CDONTS (Collaborative Data Objects for Windows NT Server), and a basic knowledge of Active Server Pages.

CDONTS provides you with objects and classes that enable you to send email from an ASP page. CDONTS only works on Windows NT/2000 Operating Systems, but it has powerful functions, allowing you to:

  • attach files while sending email,
  • send email in HTML Format,
  • use the Carbon Copy(CC) and Blind Carbon Copy(BCC) functions as in any email client,
  • set email priority

…and that’s just the beginning! Again, the process is not as complicated as it seems.

Create CDONTS

First of all, we must create an instance of the CDONTS on the server using this code:

<% 
Set Mail=Server.CreateObject("CDONTS.NewMail")
%>

In the above code, you can replace Mail with any other variable, but you must be sure to use that variable constantly. Now that we’ve created an instance of CDONTS on the server, we need to input the required values.

Input Your Values

The first input should be the Recipient to whom we’ll send the email. To add the Recipient address, use:

<% 
Mail.to=" me@mydomain.com"
%>
Mail.to consists of the same variable that we used to name the instance of our CDONTS.NewMail The to after the . tells the object the address to which the email should be sent. So now our code looks like this:

<%  
Set Mail=Server.CreateObject("CDONTS.NewMail")
Mail.to=" me@mydomain.com"
%>

Now we need to tell the object the email address of the Sender, using:

<% 
Mail.From=" testing-my@SP-Script.com"
%>

The From field after Mail. indicates the email address of the Sender. The email address should always appear in quotes. With this addition, our code becomes:

<% 
Set Mail=Server.CreateObject("CDONTS.NewMail")
Mail.To="me@mydomain.com"
Mail.From="testing-my@SP-Script.com"
%>

By now you're clear on the concept of using the main classes, so let's look at how to add the Subject line to the email. If you haven't already guessed, this is how to do it:

<% 
Mail.Subject="Just testing my script"
%>

Now the code is:

<% 
Set Mail=Server.CreateObject("CDONTS.NewMail")
Mail.To="me@mydomain.com"
Mail.From="testing-my@SP-Script.com"
Mail.Subject="Just testing my script"
%>

All that's left now is the content of the email, which we add like this:

<% 
Mail.Body="Hey! I am sending this email through an ASP Page, and  
guess what? I haven't learnt much yet, but know that ASP is very  
powerful."
%>

The Body class represents the content of the email we're sending. It can contain any sort of text, but it doesn't recognize HTML tags -- it would simply send these as normal text.

So to sum up, our code looks like this:

<%  
Set Mail=Server.CreateObject("CDONTS.NewMail")  
Mail.To="me@mydomain.com"  
Mail.From="testing-my@SP-Script.com"  
Mail.Subject="Just testing my script"  
Mail.Body="Hey! I am sending this email through an ASP Page and  
guess what? I haven't learnt much yet, but know that ASP is very  
powerful."  
%>

OK. Now it's time to send the email, using:

<%  
Mail.Send  
%>

And that should do it! Our code is neat and should successfully send the email. Let's take a last look at the complete code:

<%  
Set Mail=Server.CreateObject("CDONTS.NewMail")  
Mail.To="me@mydomain.com"  
Mail.From="testing-my@SP-Script.com"  
Mail.Subject="Just testing my script"  
Mail.Body="Hey! I am sending this email through an ASP Page and  
guess what? I haven't learnt much yet, but know that ASP is very  
powerful."  
Mail.Send  
Set Mail=nothing  
%>

The last line: Set Mail=nothing releases the instance of the object -- and hence some resources -- from the server. This will make your Server Administrator happy!

You can now copy and paste the above code into your favorite editor, save it as SendingEmail.asp (remember the file extension), and upload it to your Website. Remember to replace the Recipient's as well as the Sender's email addresses with your own. Now you can visit the page you've uploaded, and read the email you sent through the ASP page.

Obtaining Addresses

You can also use fields that are requested through a form or querystring to obtain recipients' email addresses. If you use form or querystring for the Recipient field, then the code for the .To would look like this:

<%  
' In case of requesting recipient's address from a form  
Mail.to=request.form("txteMail")  
'txteMail is just a variable, you can change it as per your form  
%>  
 
<%  
'In case of Querystring  
Mail.To=Request.QueryString("email")  
' eMail is just a variable, again you can change it as per your page  
%>

Extra Help?

Now we've explored how to send email through an ASP page – and found that it's extremely simple. If you have any problems or questions, or you find ASP email confusing, try posting your problems at SitePoint Forums. Our loyal members will be pleased to help you out!

A Parting Project:
To practice what you've learnt, create a page which asks a user for their email address, and sends them a test email. It won't be difficult if you've understood this tutorial!

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

No Reader comments