Setting properties and executing methods



The following example, which uses the sample Mailer COM object, shows how to assign properties to your mail message and how to execute component methods to handle mail messages.

In the example, form variables contain the method parameters and properties, such as the name of the recipient, the desired e-mail address, and so on:

<!--- First, create the object ---> 
<cfobject type="COM" 
    action="Create" 
    name="Mailer" 
    class="CDONTS.NewMail"> 
 
<!--- Second, use the form variables from the user entry form to populate a number 
        of properties necessary to create and send the message. ---> 
<cfset Mailer.From = "#Form.fromName#"> 
<cfset Mailer.To = "#Form.to#"> 
<cfset Mailer.Subject = "#Form.subject#"> 
<cfset Mailer.Importance = 2> 
<cfset Mailer.Body = "#Form.body#"> 
<cfset Mailer.Cc = "#Form.cc#"> 
 
<!--- Last, use the Send() method to send the message. 
    Invoking the Send() method destroys the object.---> 
<cfset Mailer.Send()>
Note: Use the cftry and cfcatch tags to handle exceptions thrown by COM objects. For more information on exception handling, see Handling runtime exceptions with ColdFusion tags.

Releasing COM objects

By default, COM object resources are released when the Java garbage collector cleans them. You can use the ReleaseCOMObject function to immediately release resources if an object is no longer needed.

Use the ReleaseCOMObject function to release COM objects that are launched as an external process, such as Microsoft Excel. The garbage collector does not always clean these processes in a short time, resulting in multiple external processes running, which drains system resources.

If the COM object has an end method, such as a quit method that terminates the program, call this method before you call the ReleaseComObject function. If you use the ReleaseComObject function on an object that is in use, the object is prematurely released and your application gets exceptions.

Example

The following example creates a Microsoft Excel application object, uses it, then releases the object when it is no longer needed:

<h3>ReleaseComObject Example</h3> 
<cfscript> 
obj = CreateObject("Com", "excel.application.9"); 
//code that uses the object goes here 
obj.quit(); 
ReleaseComObject(obj); 
</cfscript>