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

Comments (2)

  1. Does this send SMTP mail only or does it use whatever MAPI transport (eg, Lotus Notes) you have set as your preferred mailer in Windows?

  2. I don’t think this will work in IIS 6 / Windows 2003. Wasn’t the CDONTS object taken out?

    I usually end up using System.Web.Mail via C#, though you could use CDOSYS as well.

    More info at: http://support.microsoft.com/default.aspx?scid=kb;en-us;315197

Skip to main content