Using the CDOSYS COM Object in ColdFusion

I recently had to use the CDOSYS object in Cold Fusion.  Using COM Objects in Cold Fusion can get tricky.  After some trial and error I have figured out its quirks.

Cold Fusion won’t let you reference nested objects more than one level deep.  In using CDOSYS I needed to set an object.property("named_property") value. 

Using the CDOSYS object as an example:

Normally:
  Flds.Item("urn:schemas:mailheader:FOO") = “somevalue”

In Cold Fusion:
  mailheader = Flds.Item("urn:schemas:mailheader:FOO");
  mailheader.value = “somevalue”;

Not a major deal, but this isn?t documented anywhere. 

Full Code Example:

<cfscript>
objMail=CreateObject("COM”,"CDO.Message");
objMail.To="#to#";
objMail.CC="#cc#";
objMail.BCC="#bcc#";
objMail.From="#from#";
objMail.Subject="#subject#";
objMail.ReplyTo="#replyTo#";

if (#emailtype# EQ “HTML")
{
objMail.HTMLBody="#body#";
} else {
objMail.TextBody="#body#”;
}

// If you have an attachment, another CF quirk
// is all parameters of an object must be passed.
objMail.AddAttachment("#filename#”,"","");

// Set a custom header field, in this case “foo”
// with the value “somevalue”
iFlds = objMail.Fields;
mailheader = iFlds.Item("urn:schemas:mailheader:foo");
mailheader.value = “somevalue”;
iFlds.Update();

objMail.Send();
objMail="”;
</cfscript>

If you have any questions please feel free to write.

posted in: Coding