use of com.sun.mail.imap.IMAPSSLStore in project vcell by virtualcell.
the class ListservMail method getBouncedEmails.
private static void getBouncedEmails(String emailPassword) throws Exception {
IMAPSSLStore store = null;
IMAPFolder[] inboxs = null;
BufferedWriter bw = null;
try {
// connect to mail server using imaps protocol
Properties properties = System.getProperties();
Session session = Session.getDefaultInstance(properties);
// Use this instead of pop3s to access non 'Inbox' folders
store = (IMAPSSLStore) session.getStore("imaps");
store.connect(mailServer, mailUser, emailPassword);
inboxs = new IMAPFolder[] { (IMAPFolder) store.getFolder(mailFolder) };
inboxs[0].open(Folder.READ_ONLY);
// get the list of messages from folder
Message[] messages = inboxs[0].getMessages();
if (messages.length == 0)
System.out.println("No messages found.");
for (int i = 0; i < messages.length; i++) {
// // stop after listing ten messages
// if (i > 10) {
// break;
// }
System.out.println("Message " + (i + 1));
IMAPMessage message = (IMAPMessage) messages[i];
System.out.println("From : " + message.getFrom()[0]);
System.out.println("Subject : " + message.getSubject());
System.out.println("Sent Date : " + message.getSentDate());
System.out.println("Type : " + message.getContentType());
if (message.getSubject().contains(subjectSearchForBounced)) {
readMessage(message, 0, 0);
}
System.out.println();
}
System.out.println("Found " + bouncedEMails.size() + " bounced emails");
bw = new BufferedWriter(new FileWriter(rawBounceFile));
for (String s : bouncedEMails) {
bw.write(s + "\n");
}
} finally {
if (bw != null) {
try {
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (inboxs != null) {
for (int i = 0; i < inboxs.length; i++) {
if (inboxs[i] != null && inboxs[i].isOpen()) {
try {
inboxs[i].close(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
if (store != null) {
try {
store.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Aggregations