April4
Using ASP Classic and need to send an email? This is done using CDO, a built component of ASP, and an SMTP server that allows message relay. This website lists the SMTP variables needed based on your email provider, if you are using a shared hosting account or other free-based email account: http://www.emailaddressmanager.com/tips/mail-settings.html
If you are using a corporate SMTP server, obviously your settings will the custom to your organization.
Below is an example web form that will send an email. Simply change the email server settings variables to your specific SMTP server:
<%
On Error Resume Next
'These are the email server settings
Dim sSendTo, sSMTPServer, sSMTPPort
sSendTo = "someone@somedomain.com"
sSMTPServer = "smtp.server.com"
sSMTPPort = 25
' Get form variables
Dim sEmailAddress, sSubject, sBody, bError
sEmailAddress = Request.Form("email")
sSubject = Request.Form("subject")
sBody = Request.Form("body")
bError = true
If sEmailAddress <> "" Then
Set myMail=CreateObject("CDO.Message")
myMail.Subject=sSubject
myMail.From=sEmailAddress
myMail.To=sSendTo
myMail.TextBody=sBody
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
'Name or IP of remote SMTP server
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")=sSMTPServer
'Server port
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=sSMTPPort
myMail.Configuration.Fields.Update
myMail.Send
set myMail=nothing
If err.number <> 0 Then
sMessage = "<h1 style='color:red;'>An error has occurred sending your email. Please try again.</h1>"
bError = true
Else
sMessage = "<h1>Successfully sent email. Thank you for your input.</h1>"
bError = false
End If
End If
%>
<html>
<body>
<%
If sMessage <> "" Then
Response.Write sMessage
End If
' If there was an error (including no form submitted) display the form
If bError = true Then
%>
<table cellpadding=0 cellspacing=0>
<tr>
<td>Email Address</td>
<td><input type="text" name="email" value="<%
Response.Write sEmailAddress
%>"></td>
</tr>
<tr>
<td>Subject</td>
<td><input type="text" name="subject" value="<%
Response.Write sSubject
%>"></td>
</tr>
<tr>
<td>Body</td>
<td><input type="text" name= "body" value="<%
Response.Write sBody
%>"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Send Email"></td>
</tr>
</table>
<%
End If
%>
</body>
</html>
More advanced functionality can be added by adding the following syntax to the above example before the myMail.Send line of code…
Sending HTML formatted emails:
myMail.HTMLBody = sSubject
Sending emails with carbon copies or blind carbon copies:
myMail.Cc = "example@email.com"
myMail.Bcc = "example@email.com"
Sending HTML formatted emails from a file on your computer:
myMail.CreateMHTMLBody "file://c:/html-email.htm"
Using SMTP server authentication (replace username and password with your values and add before the
myMail.Configuration.Fields.Update line):
'SMTP server username
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername")="username"
'SMTP server password
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword")="password"
Adding attachments:
myMail.AddAttachment "c:\attachment.pdf"
Additional Note:the CDONTs functionality in ASP has been discontinued. If you are using CDONTs to send emails you should upgrade to the CDO example shown above.