C and S Design.
Search Friendly Programming and Design

Go to -> C and S Design -> Articles -> Code -> ASP -> Send Email -> CDOSYS -> Show Code

Click here to view or download the code for this example



ASP Code Articles

Sending email with CDOSYS

CDOSYS
Is the ASP SMTP component supplied with IIS 5 and upwards (Windows 2000 on) replacing the CDONTS component.

While the many parameters that can be used with CDOSYS look complex and a little daunting the use of a few constants and creating a function to interface with the component can make it easier to understand.

CODE:
Get this Code: Click to download the ASP file.
Do not copy and paste the displayed code. The display function adds line breaks and numbers, so what you see is definitely not you would get. The continuation marker " _ " is used to indicate the break.
01: ' SMTP Constants
02: const strMailServer = "mail.yourdomain.tld"
03: const strSMTPUser = "your.mailserver.username"
04: const strSMTPPassword = "your_password"
05: const strDefMailName = "SenderName"
06: const strDefMailTo = "Recipient"
The section of code above sets up a few constants relative to the site the script is hosted on. How this is set up on the site is upto the developer. It can be a part of of fixed config included file or read from a database into the constants.

strMailServer should be self-explanatory, but will be the DNS name of your mail server.
strSMTPUser will need to be set to a valid username on your mail server.
strSMTPPassword is the password associated with the above username.
strDefMailName is used as a default senders name.
strDefMailTo is used as a default recipient.

While it may seem odd to have a default sender and recipient configured it can be useful depending on the use the script is put to.

For example;
The function can be used for a contact form, where there is only one recipient, a multi-contact form where the recipient could be chosen from a list. It can also be used to send emails, newsletters etc to individuals or to a list server, where there would be a default "from" address

The send mail function

The code to configure and send the message is in two functions, both are esentially the same the only difference being that one is set up to send HTML emails the other as plain text. With some extra coding it is feasible to change the function to send both plain text and HTML in the one message and allow the users email client to display whichever the users preferences are.

CODE:
Get this Code: Click to download the ASP file.
Do not copy and paste the displayed code. The display function adds line breaks and numbers, so what you see is definitely not you would get. The continuation marker " _ " is used to indicate the break.
01: ' function using CDOSYS to send mail messages.

02: function _ 
	CDOSYSMail(strMailFrom,strMailTo,strMailMessage,strCCList,strBccList)
03: if strMailFrom = "" then strMailFrom = strDefMailName
04: if strMailTo = "" then strMailTo = strDefMailTo
05: dim iConf
06: dim Flds
07: dim objNewMail
08: const schema = _ 
	"http://schemas.microsoft.com/cdo/configuration/sendusing"

09: '		on error resume next '## Ignore Errors
10: 	        Set iConf = Server.CreateObject _ 
	("CDO.Configuration")
11: 	Set Flds = iConf.Fields

12: 	        'Set and update fields properties
13: 			Flds(schema) = cdoSendUsingPort
14: 			Flds(schema) = strMailServer
15: 			Flds(schema) = cdoBasic
16: 			Flds(schema) = strSMTPUser
17: 			Flds(schema) = strSMTPPassword
18: 		    Flds.Update

19: 	        Set objNewMail = Server.CreateObject("CDO.Message")
20: 	Set objNewMail.Configuration = iConf

21: 	        'Format and send message
22: 	Err.Clear
23: 		objNewMail.To = strMailTo
24: 		objNewMail.From = strMailFrom
25: 	if strCCList <> "" then		
26: 		objNewMail.cc = strCCList
27: 	end if
28: 	if strBccList <> "" then		
29: 		objNewMail.bcc = strBccList
30: 	end if
31: 		objNewMail.Subject = strMailSubject
32: 		objNewMail.TextBody =  strMailMessage
33: 	On Error Resume Next
34: if bDebug then
35: response.write strMailTo & "<br>" & strBccList & _ 
	"<br>" & strccList
36: else
37: 		objNewMail.Send
38: end if
39: 		If Err <> 0 Then
40: 		' add in some error messages.
41: 		CDOSYSMail = Err
42: 		else
43: 		CDOSYSMail = true
44: 		end if
45: 		set Flds = nothing
46: 		set iConf = nothing
47: 		Set objNewMail = nothing
48: end function
See The Form

Valid HTML 4.01! Valid CSS! copyright © C and S Design 2004 - 2005
Website Design and SE Friendly Coding C and S Design