Article

Sending eMail in ASP

Page: 1 2

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!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: