Send SMTP Mail from a Web Site

The following function (written in VBScript) can be used to mail-enable a web-based application. If you are sending HTML based body, you must specify "HTML" as the msgType input parameter. The function returns either a success or failure notice as its output parameter. This code uses CDO objects on an IIS SMTP server.

Function SendSMTPMail (userFrom, userTo, userCC, msgSubject, msgBodyHTML, msgType, msgImportance)

'*** DECLARE MAIL VARIABLES AND CDO CONSTANTS

DIM objMail

SET objMail = CreateObject("CDONTS.NewMail")

'*** Clear Out Errors That May Already Exist From Calling Routine

ERR = 0

'*** SET PARAMETERS OF THE MAIL OBJECT

objMail.From = userFrom

objMail.To = userTo

objMail.CC = userCC

objMail.Subject = msgSubject

objMail.Body = msgBodyHTML

'*** SET IMPORTANCE: (0)LOW, (1)Normal, (2)High

objmail.Importance = msgImportance

'*** SET THE FORMAT OF THE MESSAGE TO DELIVER IN HTML OR TEXT FORMAT

If msgType = "HTML" Then

objMail.BodyFormat = 0 'cdoBodyFormatHTML

objMail.MailFormat = 0 'cdoMailFormatMime

Else

objMail.BodyFormat = 1 'CdoBodyFormatText

objMail.MailFormat = 1 'CdoMailFormatText

End If

'*** SEND THE MAIL AND DESTROY IT

objMail.Send

If Err <> 0 Then

SendSMTPMail = "FAILURE: " & Err.Description

Else

SendSMTPMail = "SUCCESS: Mail Sent To " & userTo

End If

'*** DESTROY MAIL OBJECT

Set objMail = Nothing

End Function