Getting item attachments

To get the attachments to an Exchange contact, event, message, or task, use a ColdFusion Exchange tag with a getAttachments action. Also specify the following information in the tag:

  • The UID of the message that contains the attachment or attachments.

  • The name of the query to hold the information about the returned attachments. When the tag completes processing, the query object contains one record for each retrieved attachment. The query has six columns that contain the filename, complete path to the saved attachment file, MIME type, file size, CID value (or an empty string) and an indicator that shows whether the attachment is a message.

  • The path where the attachment is saved. (If you omit the path, ColdFusion does not get the attachments, but does get the information about the attachments.)

  • Optionally, whether to create unique filenames by appending numbers to the names when two or more attachments have the same names. (The default is to not create unique filenames.)

The following ColdFusion Exchange tag gets all attachments to the message identified by the theUID variable, saves them in the C:/temp/cf_files/attachments directory, and stores information about the attachments in the attachInfo structure:

<cfexchangemail action="getattachments" 
    connection="myconn1"  
    uid="#theUID#" 
    name="#attachInfo#" 
    attachmentPath="C:/temp/cf_files/attachments" 
    generateUniqueFilenames="true">

To get message attachments, you must have the UID of the message and know that the message has attachments. Use a ColdFusion Exchange tag, such as cfexchangemail, with the get action to determine this information. When the tag completes processing, the query specified by the name attribute includes the following columns:

  • The HasAttachments field is true if a message has one or more attachments

  • The UID field contains the Exchange UID of the item. The exact UID format depends on the type of item; event, contact, message, or task.

You can use these fields in your decision logic that determines whether to get attachments for a message and determines the message UID.

The following example gets the attachments to all mail messages from docuser2 in the last week. It places each message’s attachments in a directory whose name is the hexadecimal part of the message UID. For each message with attachments, the application reports subject and date of the message, followed by a table listing the message’s attachments. The table includes the attachment name, MIME type, and size.

Notice that if a message has multiple attachments with the same name, the attachment information query always lists the attachments with their original, duplicate names, even if you specify generateUniqueFilenames="true". The generateUniqueFilenames attribute only affects the names of the files on disk. The attachmentFilePath column of the attachment information structure does have the unique filenames, however.

<cfset rightNow = Now()> 
<cfset lastWeek = DateAdd("d","-7", rightNow)> 
 
<cfexchangeconnection 
    action="open" 
    username ="#user1#" 
    password="#password1#" 
    server="#exchangeServerIP#" 
    connection="conn1"> 
         
<cfexchangemail action="get" folder="Inbox/MailTest" name="weeksMail" 
        connection="conn1"> 
    <cfexchangefilter name="FromID" value="docuser2"> 
    <cfexchangefilter name="TimeSent" from="#lastWeek#" to="#rightNow#"> 
</cfexchangemail> 
 
<cfloop query="weeksMail"> 
    <cfif weeksmail.HasAttachment> 
        <!--- The UID is surrounded in <> characters and has an @ character. 
        Extract the hexadecimal number part for use as a directory name. ---> 
        <cfset atpos=Find('@', weeksMail.UID)> 
        <cfset shortUID=Mid(weeksMail.UID, 2, atpos-2)> 
         
        <cfexchangemail action="getAttachments"  
            connection="conn1" 
            folder="Inbox/MailTest" 
            uid="#weeksMail.uid#"  
            name="attachData"  
            attachmentPath="C:/temp/cf_files/attachments/#shortUID#" 
            generateUniqueFilenames="true"> 
 
        <cfoutput> 
            Directory #shortUID# contains these attachments to the 
            following message:<br /> 
            Subject: #weeksMail.Subject#<br /> 
            Sent: #dateFormat(weeksmail.TimeSent)#<br /> 
            <cftable query="attachData" colheaders="true"> 
                <cfcol header="Filename" text="#attachmentFilename#"> 
                <cfcol header="Size" text="#size#"> 
                <cfcol header="MIME type" text="#mimeType#"> 
            </cftable> 
        </cfoutput> 
 
    </cfif> 
</cfloop> 
 
<cfexchangeconnection 
    action="close" 
    connection="conn1">