I have a script in ColdFusion that is reading some files .EML
from a local SMTP server and extracting some data from the files.
I have everything working fine, but from time to time the files gets locked and I cannot delete the file and I get the following error in ColdFusion.
ColdFusion could not delete the file C:/inetpub/mailroot/Queue/NTFS_AAAAAAAAAAAAAAAAAA.EML for an unknown reason.
Here is the code I'm using
<cfscript>
props = createObject("java", "java.lang.System").getProperties();
props.put( javacast("string", "mail.host"), javacast("string", "localhost"));
props.put( javacast("string", "mail.transport.protocol"), javacast("string", "smtp"));
mailSession = createObject("java", "javax.mail.Session").getDefaultInstance(props, javacast("null", ""));
fileList = directoryList('C:\inetpub\mailroot\Queue\');
for (x=1; x LTE ArrayLen(fileList); x=x+1) {
pathToEmailFile = fileList[x];
this.fileSource = createObject("java", "java.io.FileInputStream").init(pathToEmailFile);
try {
message = createObject("java", "javax.mail.internet.MimeMessage").init(mailSession, this.fileSource);
bodyData = message.getContent();
bodyPart = bodyData.getBodyPart(javacast("int", 0)).getContent();
from = reMatchNoCase('[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,63}', message.getFrom()[1].toString())[1];
subject = message.getSubject();
// CALL FUNCTION TO PROCESS DATA HERE
} catch (any e) {
// CLOSE THE FILE IF THERE IS ANY ERROR
this.fileSource.close();
writeDump(e);
}
this.fileSource.close();
fileDelete(pathToEmailFile);
}
</cfscript>
Am I forgetting to close anything else and that is why it is causing the file to be locked?