Hi,
I have email messages stored in a MySQL database and their attachments stored as base64-encoded text. I'm trying to loop through a query and write out a properly formatted .EML file for each message. I started from some borrowed code that was used to send IMAP messages, and I had no prior experience with Java, but I've since worked through some tutorials and I now know how to write and compile a simple class. But I'm still having trouble (lots!) instantiating the correct Java objects in the correct way and order in ColdFusion.
Here's what sort of works, with problems I'll detail below:
<cfscript>
clsSession = createObject("Java", "javax.mail.Session");
objProperties = createObject("Java", "java.util.Properties");
msg = CreateObject("Java", "javax.mail.internet.MimeMessage");
mmp = CreateObject("Java", "javax.mail.internet.MimeMultipart");
mbp = CreateObject("Java", "javax.mail.internet.MimeBodyPart");
objRecipientType = CreateObject("Java", "javax.mail.Message$RecipientType");
fds = CreateObject("Java", "javax.activation.FileDataSource");
fos = CreateObject("Java", "java.io.FileOutputStream");
oos = CreateObject("Java", "java.io.ObjectOutputStream");
</cfscript>
<cfloop query="getAllMessages">
<!--- Here I also do a query "qGetMsgAtts" to get all attachments by getAllMessages.msg_id ,
and use valueList(qGetMsgAtts.attIndex) to create "msgAttachmentsList" --->
<!--- initialize a java MimeMessage object --->
<cfscript>
objProperties.init();
// (I was actually setting properties here, but I never retrieved them and code works w/o them)
objSession = clssession.getInstance(objProperties);
// Message
msg.init(objSession);
msg.addFrom(add.parse(getAllMessages.msgFrom, false));
msg.setReplyTo(add.parse(getAllMessages.msgFrom, false));
msg.addRecipients(objRecipientType.TO, add.parse(getAllMessages.msgTo, false));
// ETC... no problems setting msg properties
loopCount = loopCount + 1;
// file name
thisFileName = '#userId#_#myFun.doCountToken(loopCount)#.eml'; // my doCountToken(integer) function simply pads with zeros.
// Message body parts
mbp.init();
if (not msgIsHTML)
{
mbp.setText(msgBody);
}
else
{
mbp.setContent(msgBody, "text/html");
}
mmp.addBodyPart(mbp);
msg.setContent(mmp);
if (len (msgAttachmentsList)) // message has attachments
try // TRY TO ATTACH FILES
{
{ // qGetMsgAtts.colnames: msg_Id, attIndex, fileName, fileMIMEType, fileMacCreator, fileMacType, fileContents
writeOutput('Has attachments ' & msgAttachmentsList & '<br />');
for (i=1; i is listLen(msgAttachmentsList); i++)
{
fds.init(qGetMsgAtts.fileContents); // WORKS
mbp.init();
mbp.setDataHandler(dh.init(fds)); // WORKS
mbp.setFileName(qGetMsgAtts.fileName[i]); // WORKS
mbp.setContent(qGetMsgAtts.fileContents[i],qGetMsgAtts.fileMIMEType[i]); // WORKS
mmp.addBodyPart(mbp);
}
msg.setContent(mmp);
}
}
catch(Any excpt) {
writeOutput("#excpt.Type# error attaching content. #excpt.Message# #excpt.Detail#<br>");
}
aFile = thisFilePath & thisFileName; // PATH AND FILE NAME
fos.init(aFile); //
oos.init(fos); //
try// WRITE THE FILE TO DISK
{
msg.writeTo(oos); // WORKS ONLY IF MIME TYPE IS text/plain or text/html
oos.flush();
oos.close();
fos.close();
}
catch (Any excpt) {
writeOutput("#excpt.Type# error attaching content. #excpt.Message# #excpt.Detail#<br>");
oos.flush();
oos.close();
}
</cfscript>
</cfloop>
Although that writes the correct number of files with the correct file names, there are 2 major problems at this point:
- the contents of the file on disk appears to contain all the messages. I'm doing msg.init() atop every loop so I don't understand why. (Also the part boundary appears to be the same in every part in every message.)
- I get an error like the following for any MIME type other than text/plain or text/html :
javax.activation.UnsupportedDataTypeException error attaching content. no object DCH for MIME type application/pdf no object DCH for MIME type application/pdf
I Googled that and I understand that I probably need to use something like
bads = createObject("java","javax.mail.util.ByteArrayDataSource");
baos = createObject("Java", "java.io.ByteArrayOutputStream");
...somewhere in the mix, but all my attempts to instantiate such objects have so far failed, usually with "Method Not Found" or "Constructor not found".
I hope that explanation makes sense. Anyone ever write EML files using ColdFusion? I'll be forever grateful for any suggestions or snippets.
Richard
York U CA