Creating and Using COM Objects

In the following example, an SMTP mail handling component is created using CFOBJECT.

<CFOBJECT ACTION=CREATE
NAME=MAILER
CLASS=SMTP.Mailer>

The component needs to be created by ColdFusion before any methods in the component can be invoked or properties assigned in your application pages. This sample SMTP component includes a number of methods and properties to perform a wide range of mail handling tasks.. In the OLE/COM Viewer, methods and properties may be grouped together, making it a little confusing at first to determine one from the other.

Our SMTP mail component includes properties such as:

Screen
User
FullName
FromName
FromAddress

You use these properties to define elements of the mail message you want to send. The SMTP Mailer component also includes a number of methods, such as:

SendMail
AddRecipient
AddCC
AddAttachment

Connecting to COM objects

There are essentially two ways, specified with the ACTION attribute of CFOBJECT, to connect to COM objects using CFOBJECT:

In addition to specifying which way to connect to a COM object, you also have to specify the following with the CONTEXT attribute:

Setting properties and invoking methods

The following example, using the sample SMTPMailer COM object, shows how to assign properties to the mail message you want to send and how to execute component methods to handle mail messages.

In the example, form variables are used to provide method parameters and properties, such as the name of the recipient, the desired email address, and so on.

<!--- First, create the object --->

<CFOBJECT ACTION="Create"
    NAME="Mailer"
    CLASS="SMTPsvg.Mailer">

<!--- Then, use the form variables from the
user entry form to populate a number of properties
necessary to create and send the message. --->

<CFSET Mailer.FromName = #form.fromname#>
<CFSET Mailer.RemoteHost = #RemoteHost#>
<CFSET Mailer.FromAddress = #form.fromemail#>
<CFSET Mailer.Subject = "Testing CFOBJECT">
<CFSET Mailer.BodyText = "#form.msgbody#">
<CFSET Mailer.SMTPLog = "#logfile#">

<!--- Last, use the AddRecipient and SendMail
methods to finish and send the message along --->

<CFSET Mailer.AddRecipient("#form.fromname#","#form.fromemail#")>
<CFSET success=Mailer.SendMail()>