You are on page 1of 9

11/10/2009

Adobe(Macromedia): ColdFusion - usi

forumposters (Programmer)

29 Jan 08 18:05

Here's some java code that does almost exactly what I want to do. http://www.rgagnon.com/javadetails/java-0458.html However, I want to use this java code in a cfc I'm clueless how to put this java code into a cfc. Any pointers or tips to help me get started would be very much appreciated.
Find A Job or Post a Job Opening Click Here.

cfSearching (Programmer)

30 Jan 08 13:18

It is very simple. The key part is using the full path to the java classes. In java you can call a static method of a core class simply using its name. Where System is the name of a core java class: java.lang.System

CODE ... System.getProperties(); In ColdFusion you must use createObject() to get a reference to the class, using the full package path:

CODE ... createObject("java", "java.lang.System").getProperties(); Watch out for ugly line wrapping..

CODE <cfscript> props = createObject("java", "java.lang.System").getProperties(); props.put( javacast("string", "mail.host"), javacast("string", "smtp.dummydomain.com")); props.put( javacast("string", "mail.transport.protocol"), javacast("string", "smtp")); mailSession = createObject("java", "javax.mail.Session").getDefaultInstance( props, javacast("null", "") ); pathToEmailFile = "c:\tmp\message.eml"; source = createObject("java", "java.io.FileInputStream").init(pathToEmailFile); message = createObject("java", "javax.mail.internet.MimeMessage").init(mailSession, source);

tek-tips.com/viewthread.cfm?qid=144

1/9

11/10/2009

Adobe(Macromedia): ColdFusion - usi

WriteOutput("Subject : "& message.getSubject() &"<br>"); from = message.getFrom(); // note unlike java arrays, ColdFusion arrays are 1-based, not 0-based WriteOutput("From : "& from[1] &"<br>"); WriteOutput("--------------<br>"); WriteOutput("Body : "& message.getContent()&"<br>"); </cfscript> ---------------------------------http://cfsearching.blogspot.com/ forumposters (Programmer) 31 Jan 08 15:07

Thanks, that was very helpful. One thing that is giving me trouble now is trying to figure out where all the data is stored in the message object. I can't use cfdump to see where everything is store and under what function. Instead, I have to keep guessing and checking. Right now for example, I'm trying to find where attachments are stored and I'm lost. Thanks again for you help, you're awesome! forumposters (Programmer) 31 Jan 08 15:13

Here's how I had to get the body of the email, and it seems to contain the attachments: <cfset content = message.getContent()> <cfset i = 0> <cfset bodyPart = content.getBodyPart(javacast("int", i))> <cfreturn bodyPart.getContent()> But, now I want to get the attachments that are in bodyPart.getContent() cfSearching (Programmer) 1 Feb 08 1:45

If you are doing more than just extracting the from and subject lines, that is going to be more involved. You will have to extract each part of the multipart messages, and process each type found. It is not a topic that can be covered in single forum post. Your best best bet is to start with this java guide. It has all of the fundamentals you will need. http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#GettingAttachments You should also get familiar with the API's for the different classes. If you have not worked with java before they may seem foreign at first. But when it comes to working with java classes, they are really your best reference. http://java.sun.com/products/javamail/javadocs/javax/mail/internet/MimeMessage.html ---------------------------------http://cfsearching.blogspot.com/ forumposters (Programmer) 20 Feb 08 17:26

Thanks for another helpful post. I am able to create a variable that is a byte array for each attachment. I can then use cffile with the readbinary action. I'm not sure what to do next though. Might I want to use cfcontent to display the attachments? Or, might there be a
tek-tips.com/viewthread.cfm?qid=144 2/9

11/10/2009

Adobe(Macromedia): ColdFusion - usi

better way to display the attachments? cfSearching (Programmer) 20 Feb 08 18:34

Using cfcontent to display or download the attachments is probably the simplest option. But I assume you want to save the files first, either to disk or to a db table? I usually prefer saving to disk, because it does not bloat the db. But I do not know what your requirements are. To write the file to disk try the FileWrite() function for CF8. Off the top of my head, I do not remember whether MX 7 allows you to write binary content with cffile. If it does not try

CODE <!--- save the byte array to a file ---> <cfscript> saveToFile = expandPath("./theFileName.fileExt"); outStream = createObject("java", "java.io.FileOutputStream").init( saveToFile ); outStream.write( theByteArray ); outStream.flush(); // always close the stream outStream.close(); </cfscript> Then display the file with cfheader and cfcontent. Watch the line wrapping ..

CODE <!--- example, download a pdf file ---> <cfheader name="Content-Disposition" value="attachment; filename=yourFileName.PDF"> <cfcontent file="#fullPathToYourFile#" type="application/pdf" deletefile="no"> ... <!--- example, display a pdf file in browser ---> <cfheader name="Content-Disposition" value="inline; filename=yourFileName.PDF"> <cfcontent file="#fullPathToYourFile#" type="application/pdf" deletefile="no"> ---------------------------------http://cfsearching.blogspot.com/ forumposters (Programmer) 22 Feb 08 12:17

Actually, I wasn't thinking of saving it to disk nor too a database. Can't I use the variable attribute of cfcontent instead of the file attribute? cfSearching (Programmer) 22 Feb 08 13:51

Sure. Just change the cfcontent code above to use the variable attribute instead of file.
tek-tips.com/viewthread.cfm?qid=144 3/9

11/10/2009

Adobe(Macromedia): ColdFusion - usi

---------------------------------http://cfsearching.blogspot.com/ forumposters (Programmer) 22 Feb 08 19:07

this seems to work for images. i'm actually using jQuery to write create an iframe for each attachment like this: $('.attachDisplay').html("<iframe width='500' height='200' src='showFile.cfm? emailFilename=#url.file#&attachFilename="+$(this).attr('attachFilename')+"'></iframe>"); And, then in showFile.cfm I call my functions that return the byte data for each attachment and then cfcontent to display it. For some reason this works great for images. But, when i try to display a .rtf document, it does not work right. It prompts me to download the file instead of showing it in the iframe. Maybe there's a better way to do this? You can only have 1 cfcontent tag on a page though and that's what makes this difficult. cfSearching (Programmer) 22 Feb 08 21:41

Well, if the browser is not configured to open rtfs inside the browser window, you cannot display them in an iframe anyway. It will always prompt to download. Being a client controlled setting, there is not much you can do about that. I do not know of any way around the 1 content-type limitation, other than using frames or iframes. I think the idea of using multiple iframes to display files is interesting, but it has the downside mentioned above. I am also thinking that it could look a bit too "busy" depending on the number of files ;) ---------------------------------http://cfsearching.blogspot.com/ cfSearching (Programmer) 22 Feb 08 21:45

To clarify, the 1 content-type limitation is not a limitation of cfcontent per se. It is just the way the http content-type header works. ---------------------------------http://cfsearching.blogspot.com/ forumposters (Programmer) 26 Feb 08 16:39

many thanks for your helpful replies... very much appreciated. you got me thinking when you explained that the browsers is not configured to open rtf files... i did some googling on this issue and found a plugin for firefox that allows me to configure this terrific browser to open rtf files automagically. if anyone else is wondering what this plugin is called, it is "MIME Edit 0.60". problem solved. thanks again cfsearching! cfSearching (Programmer) I am glad you got it worked out. Thanks for the follow up. Yes, I did not know you were referring to your browser settings only. Those you obviously can configure ;-) You just cannot control the browser settings of other users from your CF page.
tek-tips.com/viewthread.cfm?qid=144 4/9

26 Feb 08 17:48

11/10/2009

Adobe(Macromedia): ColdFusion - usi

---------------------------------http://cfsearching.blogspot.com/ RocketDan (Programmer) 18 Mar 08 18:44

forumposters... how did you assign the byte array to a cf variable without just getting the string that indicates it is a byte array? cfSearching (Programmer) 18 Mar 08 22:14

Assigning a byte array to a variable should not behave any differently. What code are you using that produces that result? ---------------------------------http://cfsearching.blogspot.com/ RocketDan (Programmer) Here is the code I'm using. 18 Mar 08 22:49

CODE <cfscript> props = createObject("java", "java.lang.System").getProperties(); props.put( javacast("string", "mail.host"), javacast("string", "smtp.dummydomain.com")); props.put( javacast("string", "mail.transport.protocol"), javacast("string", "smtp")); mailSession = createObject("java", "javax.mail.Session").getDefaultInstance(props, javacast("null", "")); pathToEmailFile = "c:\test.eml"; source = createObject("java", "java.io.FileInputStream").init(pathToEmailFile); message = createObject("java", "javax.mail.internet.MimeMessage").init(mailSession, source); mp = message.getContent(); for(i=0;i<mp.getCount();i++) { part = mp.getBodyPart(javacast("int",i)); disp = part.getDisposition(); if(( isDefined("disp") ) && (UCase(disp)=="ATTACHMENT" || UCase(disp)=="INLINE") ) { FileWrite("C:\" & part.getFileName(),part.getInputStream() ); } } </cfscript> The result is a file is: CODE
tek-tips.com/viewthread.cfm?qid=144 5/9

11/10/2009

Adobe(Macromedia): ColdFusion - usi

com.sun.mail.util.BASE64DecoderStream@58924a cfSearching (Programmer) 19 Mar 08 0:12

You are not actually passing a byte array to FileWrite(). The second value is an InputStream. Copy the bytes from the InputStream to a ByteArrayOutputStream. Then use toByteArray() to pass the bytes to FileWrite. You may also wish to add handling to make the file names unique to avoid overwriting.

CODE </cfscript> //... other code saveToFileName = "C:\" & part.getFileName(); inputStream = part.getInputStream(); outStream = createObject("java","java.io.ByteArrayOutputStream").init(); // create byte array. read up to the first // 1024 bytes (or however many) into the array byteClass = createObject("java", "java.lang.Byte").TYPE; byteArray = createObject("java","java.lang.reflect.Array").newInstance(byteClass, javacast("int", 1024)); length = inputStream.read(byteArray); // if there is any data to read offset = 0; while ( length GT 0) { outStream.write( byteArray, offset, length ); length = inputStream.read( byteArray ); } outStream.close(); inputStream.close(); FileWrite( saveToFileName, outStream.toByteArray() ); </cfscript> ---------------------------------http://cfsearching.blogspot.com/ RocketDan (Programmer) That worked great. Thank you. cfSearching (Programmer) You are welcome. Glad I could help. ---------------------------------http://cfsearching.blogspot.com/
tek-tips.com/viewthread.cfm?qid=144 6/9

19 Mar 08 10:42

19 Mar 08 10:53

11/10/2009

Adobe(Macromedia): ColdFusion - usi

forumposters (Programmer) cfsearching,

27 Jun 08 12:20

How might you parse emails from a zip file without unzipping it first? Here's what I've come up with so far which makes use of the new cfzip tag: <cfzip action="list" file="#ExpandPath( '/test/test.zip' )#" name="test" /> <cfoutput query="test"> <cfzip action="read" entrypath="#test.name#" file="#ExpandPath( '/test/test.zip' )#" variable="emlFile" /> So, now I have the file in a variable called emlFile? Do I need to create a file out of this and then use the following code: pathToEmailFile = "c:\test.eml"; source = createObject("java", "java.io.FileInputStream").init(pathToEmailFile); message = createObject("java", "javax.mail.internet.MimeMessage").init(mailSession, source); Or, can I use something other than java.io.FileInputStream to create the source object from this variable? If I knew Java better, I could probably get this figured out... cfSearching (Programmer) forumposters, The email files you are reading in are binary, correct? If so you can pass the bytes into something like ByteArrayInputStream. That would take the place of your FileInputStream. 27 Jun 08 13:23

---------------------------------http://cfsearching.blogspot.com/ forumposters (Programmer) 27 Jun 08 17:02

Thanks, cfsearching. Here's the code that I've come up with after some further help from the Javamail forum:

CODE <cfset variables.b = emlFile.getBytes("iso-8859-1")>


tek-tips.com/viewthread.cfm?qid=144 7/9

11/10/2009

Adobe(Macromedia): ColdFusion - usi

<cfset variables.byteInputStream = createObject("java","java.io.ByteArrayInputStream").init(b)/> <cfset variables.oMail = createObject("Java", "javax.mail.internet.MimeMessage").init(objSession,byteInputStream)> cfSearching (Programmer) 28 Jun 08 12:01

You could also just pass the emlFile variable into the constructor 'as is'. Assuming encoding is not an issue.

CODE .. <cfset variables.inStream = createObject("java","java.io.ByteArrayInputStream").init(emlFile)/> <cfset variables.oMail = createObject("Java", "javax.mail.internet.MimeMessage").init(objSession, inStream)/> ..

---------------------------------http://cfsearching.blogspot.com/ cfSearching (Programmer) Quote: You could also just pass the emlFile variable into the constructor 'as is'. Assuming encoding is not an issue. I forgot to mention you must also use <cfzip action="readBinary" ..> so the variable contents are bytes. It will not work if you use <cfzip action="read" ..>. ---------------------------------http://cfsearching.blogspot.com/ cfSearching (Programmer) 11 Jul 08 21:43 28 Jun 08 12:12

For any future readers, the example at http://www.rgagnon.com/javadetails/java0458.html probably assumes readers understand they are changing properties at a System level. If you do not wish to modify System settings, you can use a Properties object instead. ie use something like: props = createObject("java", "java.util.Properties").init();
tek-tips.com/viewthread.cfm?qid=144 8/9

11/10/2009

Adobe(Macromedia): ColdFusion - usi

instead of: props = createObject("java", "java.lang.System").getProperties(); ---------------------------------http://cfsearching.blogspot.com/

tek-tips.com/viewthread.cfm?qid=144

9/9

You might also like