A generic form mail with CDOSYS - ASP Scripts


The following example is a simple form mail to show you how you can send emails with ASP using CDOSYS email component.

The first time the code is executed, the script shows a classic form module.
Once requested fields have been filled and the form submitted, the script will reload the page and an email will be sent to selected recipient with information collected from the form.

Warning: you should be aware that the following example doesn't perform any kind of validation before sending the email. Invalid form fields may cause a mail delivery failure.

You can add additional form fields using the following ones as an example.

<% 

' Check whether you should send the email
if Request.QueryString("send") = "1" AND Request.Form("submit") = "send" then 

Dim objMail, objConfig 

' Create a new email instance
Set objMail = Server.CreateObject("CDO.Message") 

' Create a new configuration object
Set objConfig = Server.CreateObject ("CDO.Configuration")

' Set configurations
With objConfig 

' SMTP Server (be sure to change this configuration!)
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.example.com" 

' SMTP Port
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 

' CDO Port
.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 

' Timeout 
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60 
.Fields.Update 

End With 

' Apply configurations
Set objMail.Configuration = objConfig 

' Customize the email
objMail.From = "mittente@dominio.com" ' Sender
objMail.To = Request.Form("email") ' Receiver
objMail.Subject = Request.Form("subject") ' Subject
objMail.HTMLBody = Request.Form("message") ' Message

' Send email
objMail.Send() 

Set objMail = Nothing 

Response.Write("The email has been send to the following recipient " &_ 
Request.Form("email")) 

else %> 
<form name="formail" method="post" action="?send=1"> 
<table align="center" cellpadding="3" cellspacing="1">
<tr> 
<td>Email</td> 
<td><input type="text" name="email" /></td> 
</tr> 
<tr> 
<td>Subject</td> 
<td><input type="text" name="subject" /></td> 
</tr> 
<tr> 
<td>Message</td> 
<td><textarea name="message"></textarea></td> 
</tr> 
<tr> 
<td colspan="2"> <input type="submit" value="send" name="submit" /> </td> 
</tr>
</table> 
</form> 
<% end if %> 

ASP.weppos.com and ASP.weppos.it are property of Simone Carletti.
Discover my website or visit my Simone Carletti's profile on LinkedIn