Session session = Session.getDefaultInstance(properties);
Store store = session.getStore(mailAccountVO.getAccountType());
store.connect(mailAccountVO.getMailServer(), mailAccountVO.getLogin(), mailAccountVO.getPassword());
POP3Folder folder = null;
Message[] messageArray = null;
try {
folder = (POP3Folder)store.getFolder("INBOX");
// open the remote folder for reading (and writing)
folder.open(Folder.READ_WRITE);
// get all the messages from the remote folder
messageArray = folder.getMessages();
}catch(MessagingException e){
try {folder.close(mailAccountVO.isLeaveMessagesOnServer());} catch (Exception ex) {}
store.close();
}
CVDal cvdal = new CVDal(this.dataSource);
ArrayList messageIDs = new ArrayList();
// Check wheather the Account is view as a Support Email Account
boolean isSupportEmailAccount = false;
isSupportEmailAccount = this.isSupportEmailAccount(mailAccountVO.getAccountID(), cvdal);
try {
// Get list of UID's from database for this account
ArrayList uidList = this.getUidList(mailAccountVO.getAccountID(), cvdal);
// this will hold all the UIDs that are left on the server, so
// that we can store them and not download them next time
ArrayList serverUIDs = new ArrayList();
// loop through the array of Message objects, and process each
for (int i=0; i<messageArray.length; i++) {
// get the unique ID of the message
String messageUID = folder.getUID(messageArray[i]);
HashMap embededImageMap = new HashMap();
// if the uid List in the database does not contain the UID
// of the current message, then we need to download this message.
// Note that if uid list is empty, we'll download all messages
if (! uidList.contains(messageUID)) {
// save message in database, then save attachments
int newMessageID = this.addMessage(messageArray[i], messageUID, mailAccountVO.getAccountID(), mailAccountVO.getOwnerID(), defaultFolder.getFolderID(), cvdal);
this.saveAttachments(this.getPartContent(messageArray[i]), newMessageID, mailAccountVO.getAccountID(), mailAccountVO.getOwnerID(), attachmentFolderID, cvdal, embededImageMap);
this.addBody(messageArray[i], newMessageID, embededImageMap, cvdal);
embededImageMap = null;
// Add the newMessageID to the messageIDs Collection which will
// be used later by the Rules framework and the Support framework
messageIDs.add(new Integer(newMessageID));
newMessages++;
} // end if (addMessage)
if (mailAccountVO.isLeaveMessagesOnServer()) {
// need to add this UID to the local database, so that
// we don't download it next time. No need to do so if
// we're expunging messages from the server.
serverUIDs.add(messageUID);
messageArray[i].setFlag(Flags.Flag.SEEN, true);
}else{
// need to delete this message from the remote server
messageArray[i].setFlag(Flags.Flag.DELETED, true);
}
} // end for(int i=0; i<messageArray.length; i++)
// cleanup uid list in database, adding new uids or deleting the ones that aren't on server anymore
this.removeInvalidUIDs(mailAccountVO.getAccountID(), serverUIDs, cvdal);
// if we have just downloaded any messages, we need to apply
// any rules to the messages that we just downloaded. We'll
// pass the list of messageIDs (from our database) to
// this.applyRules() which will do all the rules stuff.
if (messageIDs != null && messageIDs.size() > 0) {
this.applyRules(mailAccountVO.getAccountID(), messageIDs, cvdal);
}
}finally{
//Be a good programmer and clean up your connections.
try { folder.close(! mailAccountVO.isLeaveMessagesOnServer());} catch (Exception e) {}
try { store.close(); } catch (Exception e) {}
cvdal.destroy();
cvdal = null;
}