dcsimg

ASP Primer: Forming Up

By Joe Burns

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.

<% Option Explicit %>

<% Dim MyEmail

       Set MyEmail =

       Server.CreateObject("CDONTS.NewMail")

       MyEmail.From = "name@yourdomain.com"

       MyEmail.To = "name@theirdomain.com"

       MyEmail.Subject = "ASP email Test"

       MyEmail.Body = "This is my ASP email test!"

       MyEmail.Send

       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?

What I always use is vbcrlf. That stands for "Visual Basic Carriage Return Line Feed" which pops the text to a new line. This only works if you are using VBScript, though. If you are using JavaScript, you will have to add in the carriage return and line feed characters. Here's an example using vbcrlf:

       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.

New Concept

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.




Page 3 of 4

Previous Page
1 2 3 4
Next Page

Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
  •  
  •  
  •  
Thanks for your registration, follow us on our social networks to keep up-to-date