ASP Primer: Forming Up
Application Security Testing: An Integral Part of DevOps
Sending Out Some Email
There are literally billions of emails flying around the globe almost every day. Because of this ever increasing reliance on email, ASP has the ability to generate email on the fly. This is a very useful tool especially for automated responses to subscription submissions, information requests and e-commerce applications.
Sending an email from your ASP pages is actually quite simple. There are several different software manufacturers that sell server software to handle email, however. I am going to show you how to use CDONTS, which is most commonly used with ASP. Be sure to check and see what is installed on your particular server before using the code below.
Most ASP email is handled pretty much the same way, it's just the syntax that changes. Even if you don't have CDONTS available, this example should at least give you a working understanding of how to create email from your ASP pages.
<% Dim MyEmail
Set MyEmail =
MyEmail.From = "firstname.lastname@example.org"
MyEmail.To = "email@example.com"
MyEmail.Subject = "ASP email Test"
MyEmail.Body = "This is my ASP email test!"
Set MyEmail = Nothing %>
Look pretty easy? Well, it is. At first glance, almost everything should look pretty logical to you. Let's see how the process works line by line.
The first line will probably be the most confusing for you. What we are doing here is basically just creating the shell for our new email. By setting MyEmail equal to Server.CreateObject("CDONTS.NewMail") we are making an instance of the email object that we are affectionately naming "MyEmail".
The next four lines should make perfect sense to you. Here we are defining all of the basic elements of the email, i.e. the from address, the to address, the subject line and the body itself. Be careful to always complete both the To and the From, otherwise you will get an error when sending.
Once you have set the contents of your email, it's a simple process to send it on its way. Just tell the object to send by using MyEmail.Send.
Lastly, we need to clear the email object when we are done with it. It is a good practice to clear all objects when you are done with them so that you don't waste valuable memory space on the server. To clear an object just set the object equal to Nothing. Also, be sure to not use quotes around Nothing, otherwise you will get an error.
So, how do you insert a line break into the body of your email?
MyEmail.Body = "Hello!" & vbcrlf & vbcrlf & _
"This is my ASP email test!"
The example above will insert two returns after "Hello!" which will effectively insert a blank line between "Hello!" and the rest of the body text.
Notice in the example above I used an underscore ("_"). The underscore lets the server know that you want to continue your code to the next line. When the server reads the underscore it will paste the two lines together. Be sure to always leave a space before the underscore or you will generate an error. Also, you can use the underscore to break up a line of code into as many shorter lines as you like.
What about sending an HTML email?
Sending an HTML email works pretty much the same as sending a plain text email. Create your email object exactly like the plain text example above but instead of entering simple text in the body you will use HTML.
Once you have accomplished that task, you need to add a couple of extra lines before you tell the server to send. Here they are:
MyEmail.BodyFormat = 0
MyEmailMailFormat = 0
What this does is tell the server that the body of the email is comprised of HTML and that you are intending to send this email out as HTML. By default, the server will always assume you are sending plain text unless you add the lines above.
IT Solutions Builder TOP IT RESOURCES TO MOVE YOUR BUSINESS FORWARD
Which topic are you interested in?
What is your company size?
What is your job title?
What is your job function?
Searching our resource database to find your matches...