Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Send Email Using ASP

ASP-Enabled Web Server

How to Send Email Using ASP

by  lottastuff  Posted    (Edited  )
This question seems to come up quite a bit, and the answer depends on which email component your web server makes available. Below is code for four widely used components as well as a simple method for determining which component you have access to (assuming it's not easiest to just ask your hosting provider):

Code:
<%
'Begin by discovering which email component your server runs:
dim objMail
set objMail = Server.CreateObject("Persits.MailSender")	'ASPEmail
set objMail = Server.CreateObject("SMTPsvg.Mailer")		'ASPMail
set objMail = Server.CreateObject("CDONTS.Newmail")	'CDONTS
set objMail = Server.CreateObject("JMail.SMTPMail")		'Jmail
%>

ONE of the above is likely to work without producing an error. Comment out the ones that don't work until you find the one that works (or you COULD just ask whoever manages your web server).

Do just the lines above until you determine which email component you have available for use. Once that's settled, eliminate all the "set objMail" lines that do not work, and add whichever block of code below corresponds to that component. In each case, a "see X for more" link is provided.

ASPEmail (Persits) example (see http://www.aspemail.com/Manual.htm for more)
Code:
<%
Set objMail = Server.CreateObject("Persits.MailSender")
objMail.Host = "mail.smtp-server.com" ' Specify a valid SMTP server
objMail.From = "sendersemail@yourdomain.com" ' Specify sender's address
objMail.FromName = "Senders Name" ' Optionally specify sender's name
objMail.AddAddress  "recipientsemail@theirdomain.com", "Optional Recipient Name" 'Specify recipient
objMail.AddCC = "someotherrecipient@somedomain.com"
objMail.AddBcc = "someotherrecipient@somedomain.com"
objMail.AddAttachment = "c:\images\cakes.gif"	'How to add an attachment
objMail.Subject = "Sending Email with  ASP using Persits"
objMail.Body = "Please disregard this email. It is only a test."
'---or---
objMail.Body = "<html><body><h1>Please disregard this email!</h1>It is only a test</body></html>"
objMail.IsHTML  = true
'--Finally, send it
objMail.Send
%>

ASPMail example (see for http://www.serverobjects.com/comp/Aspmail4.htm for more)
Code:
<%
set objMail = Server.CreateObject("SMTPsvg.Mailer")
objMail.RemoteHost = "mail.smtp-server.com" ' Specify a valid SMTP server
objMail.FromAddress = "sendersemail@yourdomain.com" ' Specify sender's address (only one)
objMail.FromName = "Senders Name" ' Optionally specify sender's name
objMail.AddRecipient  "Recipients Name", "recipientsemail@theirdomain.com"  'Specify recipient
objMail.AddCc = "Additional Recipients Name", "someotherrecipient@somedomain.com"
objMail.Bcc = "Additional Recipients Name", "someotherrecipient@somedomain.com"
objMail.AddAttachment  = "c:\images\cakes.gif"	'How to add an attachment
objMail.Subject = "Sending Email with  ASP using CDONTS"
objMail.BodyText = "Please disregard this email. It is only a test."
'---or---
objMail.BodyText = "<html><body><h1>Please disregard this email!</h1>It is only a test</body></html>"
objMail.ContentType   = "text/html"
'--Finally, send it
objMail.SendMail 
%>

CDONTS example (see for http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdo/html/_denali_newmail_object_cdonts_library_.asp for more)
Code:
<%
set objMail = Server.CreateObject("CDONTS.Newmail")
objMail.From = "sendersemail@yourdomain.com" ' Specify sender's address (only one)
objMail.To = "recipientsemail@theirdomain.com"  'Use ; to seperate items in a list of recipients
objMail.Cc = "someotherrecipient@somedomain.com"
objMail.Bcc = "someotherrecipient@somedomain.com"
objMail.AttachFile  = "c:\images\cakes.gif"	'How to add an attachment
objMail.Subject = "Sending Email with  ASP using CDONTS"
objMail.Body = "Please disregard this email. It is only a test."
'---or---
objMail.Body = "<html><body><h1>Please disregard this email!</h1>It is only a test</body></html>"
objMail.BodyFormat   = 0
'--Finally, send it
objMail.Send 
%>

JMAIL example (see for http://www.dimac.net/?contentKey=%7bB8B59804-C6FE-4A61-B687-6897008381F4%7d&nil=nil for more)
Code:
<%
set objMail = Server.CreateObject("JMail.SMTPMail")
objMail.ServerAddress  = "mail.smtp-server.com" ' Specify a valid SMTP server
objMail.From  = "sendersemail@yourdomain.com" ' Specify sender's address (only one)
objMail.FromName = "Senders Name" ' Optionally specify sender's name
objMail.AddRecipient  = "recipientsemail@theirdomain.com", "Recipients Name"  'email and optional recipient name
objMail.AddRecipientCC = "someotherrecipient@somedomain.com"
objMail.AddRecipientBCC = "someotherrecipient@somedomain.com"
objMail.AddAttachment = "c:\images\cakes.gif"	'How to add an attachment
objMail.Subject = "Sending Email with  ASP using CDONTS"
objMail.Body  = "Please disregard this email. It is only a test."
'---or---
objMail.AppendHTML = "<html><body><h1>Please disregard this email!</h1>It is only a test</body></html>"
objMail.BodyFormat   = 0
'--Finally, send it
objMail.Execute 
%>
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top