Sending an Email with ASP

To send an email using ASP, you first need to ensure that the server the ASP file is served from has the SMTP (mail-sending) service active. If installing SMTP, remember to give the appropriate accounts permission to write to the mailroot/ folder, otherwise you'll get an internal server error (code 500).

Assuming everything is in place, the process is quite simple. First, you need to provide some content for the message. For this example, I demonstrate the use of an email form for providing the email's content. An often used technique is to pull the content (which might be different To:, Subject:, or Message: fields, or any combination of them)) from a database. An obvious example of database use would be a mailing list.

For simplicity, I have split the HTML form from the ASP code that does the mailing. However, the usual technique is to place the HTML form, and the ASP script that handles the form in the same file. This is not difficult to do, but I've avoided it here for clarity.


Here is the HTML form, I have used a table to arrange the visual appearance of the fields, but a form needn't contain a table. The ACTION attribute of the form indicates what file the browser should request of the server when the submit button is clicked. In this case, it requests the ASP file that will generate the email message.

   <P>Send Mail</P>

   <FORM ACTION="sendmail.asp" METHOD="POST">
         <TD><INPUT TYPE="TEXT" NAME="TO" SIZE="50"></TD>

         <TD><INPUT NAME="SUBJ" SIZE="50"></TD>



Here is the ASP script that generates an email message based on the data it receives from the HTML form. It first tests to see that it has received sufficient data to generate the email (the only essential field is the TO: field). It then uses Microsoft's Colaboration Data Object (CDONTS) to create a blank email. It then proceeds to fill-in the relevant fields based on the values entered on the HTML form. It then sends the message, and deletes the now-sent mail object from memory to save space. It finally sends a success message to the remote user as a primitive HTML page.

If the script detects that insufficient data has been given, it sends the remote user a simple HTML page indicating the fact.

<% @language = vbscript %>
dim objMail
   if request.servervariables("CONTENT_LENGTH") <> 0 then
      'Reject the form submission, if the TO field was empty
      if Request.Form("MAIL_TO") <> "" then
         set objMail = CreateObject( "CDONTS.NewMail" )
         objMail.from = "" = request.form("MAIL_TO")
         objMail.subject = request.form("MAIL_SUB") 
         objMail.body = request.form("MAIL_MESG")
         set objMail = nothing
         response.write("<HTML><BODY><P>Message sent</P></BODY></HTML>")
      else 'TO field was empty
         response.write("<P><EM>You didn't tell me who to send it to!<BR>Please click your browser's back button to try again.</EM></P></BODY></HTML>")
      end if
   end if 'content_length <> 0

Home About Me
Copyright © Neil Carter

Content last updated: 2003-10-30