# 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:

- The Create method (CFOBJECT ACTION="Create"), which takes a COM object, typically a DLL, and instantiates it prior to invoking methods and assigning properties.
- The Connect method (CFOBJECT ACTION="Connect"), which links to an object that is already running on the server, typically an executable.

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

- INPROC -- This means an In-Process server object (typically a DLL) that is running in the same process space as the calling process, such as ColdFusion.
- LOCAL -- This is an Out-of-Process server object (typically an EXE) that is running outside the ColdFusion process space but running locally on the same server.
- REMOTE -- This is also an Out-of-Process server object (also typically an EXE) that is running remotely on the network. Using REMOTE implies using the SERVER attribute to identify where the object resides.

## 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()>