props.setProperty("mail.store.protocol", "imaps");
props.setProperty("mail.imaps.ssl.protocols", "SSLv3");
Session session = Session.getDefaultInstance(props, null);
//session.setDebug(true);
Store store = session.getStore("imaps");
Config config = Application.getConfig();
store.connect(
config.getValue("checkmail.host"),
config.getValue("checkmail.user"),
config.getValue("checkmail.pass")
);
String receivingFolder = config.getValue("checkmail.receiving");
String processedFolder = config.getValue("checkmail.processed");
Folder sourceFolder = navigateToFolder(receivingFolder, store);
Folder archiveFolder = navigateToFolder(processedFolder, store);
sourceFolder.open(Folder.READ_WRITE);
// stores all daybreak emails in a set for each report
DaybreakSetContainer daybreakSetContainer = new DaybreakSetContainer();
// Add all emails
for(Message message : sourceFolder.getMessages()) {
daybreakSetContainer.addMessage(message);
}
List<DaybreakSet> completeSets = daybreakSetContainer.getCompleteSets();
if(completeSets.size()>0) {
logger.info("Detected " + completeSets.size() + " complete daybreak reports");
for(DaybreakSet daybreakSet : completeSets){
logger.info("Saving report " + daybreakSet.getPrefix() + ":");
for(DaybreakDocType messageType : DaybreakDocType.values()){
Message message = daybreakSet.getMessage(messageType);
String filename = daybreakSet.getPrefix() + messageType.getLocalFileExt();
if (message.isMimeType("multipart/*")) {
Multipart content = (Multipart) message.getContent();
for (int i = 0; i < content.getCount(); i++) {
Part part = content.getBodyPart(i);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
logger.info("\tSaving " + part.getFileName() + " to " + filename);
String attachment = IOUtils.toString(part.getInputStream());
String lrsFileDir = config.getValue("checkmail.lrsFileDir");
FileUtils.write(new File(lrsFileDir, filename), attachment);
}
}
}