Displaying images inline

If an HTML message includes inline images, the Exchange server saves the images as attachments. Take the following steps to display the images in the retrieved message:

  1. Use cfexchangemail tag get action to get the mail message.

  2. Use cfexchangemail tag getattachments action to get the message attachments. Specify the UID of the mail message you got in the previous step. Also specify an attachmentPath attribute value that is under your web root, so that you can access the saved files by using a URL.

  3. Search through the HTMLMessage field text that you got in step 1 and find the image items. Get the CID (content ID) value for each image.

  4. Search the attachments query that you got in step 1. For each row with a CID column value that you got in step 3, get the corresponding attachmentFilePath column value.

  5. Replace every img tag src attribute value with the attachmentFilePath field value that corresponds to the cid value.

  6. Display the resulting HTML.

The following example shows how to display a message with an inline image by retrieving the image from the attachments.

<!--- Open the connection to the Exchange server. ---> 
<cfexchangeconnection 
    action="open" 
    username = "#user1#" 
    password = "#password1#" 
    server = "#exchangeServerIP#" 
    connection = "testconn"> 
     
<!--- Get the mail message. ---> 
<cfexchangeMail action="get" connection ="testconn" name="getMail"> 
    <cfexchangeFilter name="Subject" value="sample inline image"> 
</cfexchangeMail> 
 
<cfdump var="#getMail#"> 
 
<!--- The following code assumes we found only one matching message. ---> 
<cfoutput query="getMail"> 
    <cfset theUID = #getMail.UID#> 
    <cfset htmlmessage = getMail.htmlmessage> 
</cfoutput> 
 
<!--- Get the message attachments. ---> 
<CFExchangeMail action="getAttachments" UID ="#theUID#" connection="testconn" name="attachments" attachmentPath="C:\ColdFusion8\wwwroot\My_Stuff\cfexchange\Book\attachments" generateuniquefilenames="no"> 
 
<!--- Extract the image names from the mail message ---> 
<!--- Initialize the index into the message used in finding ---> 
<cfset findstart = 1> 
<!--- Use an index loop to find all image source entries in the message ---> 
<!--- This example supports up to 25 inline images ---> 
<cfloop index="i" from="1" to="25"> 
    <!--- find a cid: entry ---> 
    <cfset stringStart[i] = Find('"cid:', htmlmessage, findstart)> 
     
    <!--- Exit the loop if no match was found ---> 
    <cfif (stringstart[i] EQ 0)> 
        <cfbreak> 
    </cfif> 
    <!--- Increment the string index used in finding images. ---> 
    <cfset findstart = stringstart[i] +5 > 
    <!--- Get text to the right of âBADCHAR˜cid:.âBADCHAR™ 
        Using a string length of 30 should get more than the image name. ---> 
    <cfset rightpart[i]=Mid(htmlmessage, findstart, 30)> 
    <!--- use the ListFirst function to remove all the text starting  
        at the quotation mark. ---> 
    <cfset imagename[i]=ListFirst(rightpart[i], '"')> 
 
    <!--- Loop over the attachments query and find the CID. ---> 
    <cfloop query="attachments"> 
        <!--- Replace the image name with the contents of the attachment ---> 
        <cfif attachments.CID EQ imagename[i]> 
            <cfset htmlmessage = Replace(htmlmessage,"cid:#imagename[i]#",  
            "attachments/#attachments.ATTACHMENTFILENAME#")> 
        </cfif> 
    </cfloop> 
</cfloop> 
 
<h3>The full mail message</h3> 
<cfoutput>#htmlmessage#</cfoutput> 
 
<cfexchangeconnection 
    action="close" 
    connection = "testconn">