Send and email from a webpage,sample code

Send and email from a webpage,sample code

Home |  About Us |  Contact |  Articles

To send and email from a web page, there are two options. The first uses the visitor’s own default email program; the second utilizes a process available with asp called CDOSYS. The sample code that follows will allow a visitor to send an e-mail to an e-mail address associated with the website.

Using the visitor’s email program.

Two methods that will be demonstrated using the visitor's email are the use of a link and the use of a button.

Using a link.

This is by far the easiest methodology but also the least pleasing to look at.

Sample code for an email link.

<A HREF="mailto:support@mywebsite.com?subject=Support Request ">E-mail Support</A>

This will appear on the webpage as:

E-mail Support

Using a button

This method definitely looks more professional.

Sample code for sending an e-mail using a button.

<INPUT TYPE="button" value="E-mail Support" onClick="parent.location='mailto:support@mywebsite.com?subject=Support Request’">

NOTE: The code for the button should be placed on a single line as follows:

<INPUT type="button" Value="E-mail Support" onClick="parent.location='mailto:support@mywebsite.com?subject=Support Request’">

This will appear on the webpage as:

Using CDOSYS and a webpage form

Using CDOSYS to send an e-mail from a website requires the use of asp and a webpage form. This method does NOT use the visitor’s e-mail program, it uses your website.

The webpage form

<form method="post" action="sendmail.asp"><br>
Your name: <input type="text" name="visitorname" size="20"><br>
Your e-mail: <input type="text" name="from" size="40"><br>
<center>Questions or Comments</center><br>
<textarea cols="40" rows="6" name="msgBody"></textarea><br><br>

<input type="submit" value="Send Email"><br>

This will appear on the webpage as:

Your name:
Your e-mail:
Questions or Comments

The sendmail.asp page

Sample code:

<% visitorname = Request("visitorname")
from = Request("from")
msgBody = Request("msgBody")

Set ObjSendMail = CreateObject("CDO.Message")

'This section provides the configuration information for the remote SMTP server.

ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Send the message using the network (SMTP over the network).
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="relay-hosting.secureserver.net"
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False 'Use SSL for the connection (True or False)
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

' If your server requires outgoing authentication uncomment the lines bleow and use a valid email address and password.
'ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'basic (clear-text) authentication
'ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="somemail@yourserver.com"
'ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="yourpassword"


'End remote SMTP server configuration section==
ObjSendMail.To = "support@mywebsite.com"
ObjSendMail.Subject = "Support Request"
ObjSendMail.From = from & "@mywebsite.com"

ObjSendMail.TextBody= msgBody

Set ObjSendMail = Nothing
Response.Redirect "Enter the url you want the visitor to go to."

Obviously the use of a webpage form is more desirable, it's much more professional in appearance.

Have a question or Comment
or need some help?
Fill in the form below and we will get back to you as quickly as possible.
We may also be reached by phone at 612-871-6089
Questions or Comments