Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using java in a cfc to parse emails 1

Status
Not open for further replies.

forumposters

Programmer
Aug 31, 2006
61
US
Here's some java code that does almost exactly what I want to do.
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.
 
cfsearching,

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...
 
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.



----------------------------------
 
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")>
<cfset variables.byteInputStream = createObject("java","java.io.ByteArrayInputStream").init(b)/>
<cfset variables.oMail = createObject("Java", "javax.mail.internet.MimeMessage").init(objSession,byteInputStream)>
 
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)/>
..





----------------------------------
 
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" ..>.


----------------------------------
 
For any future readers, the example at 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();

instead of:

props = createObject("java", "java.lang.System").getProperties();

----------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top