Search in sources :

Example 1 with IMAPSSLStore

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();
            }
        }
    }
}
Also used : Message(javax.mail.Message) IMAPNestedMessage(com.sun.mail.imap.IMAPNestedMessage) IMAPMessage(com.sun.mail.imap.IMAPMessage) IMAPFolder(com.sun.mail.imap.IMAPFolder) FileWriter(java.io.FileWriter) Properties(java.util.Properties) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BufferedWriter(java.io.BufferedWriter) IMAPSSLStore(com.sun.mail.imap.IMAPSSLStore) IMAPMessage(com.sun.mail.imap.IMAPMessage) Session(javax.mail.Session)

Aggregations

IMAPFolder (com.sun.mail.imap.IMAPFolder)1 IMAPMessage (com.sun.mail.imap.IMAPMessage)1 IMAPNestedMessage (com.sun.mail.imap.IMAPNestedMessage)1 IMAPSSLStore (com.sun.mail.imap.IMAPSSLStore)1 BufferedWriter (java.io.BufferedWriter)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Properties (java.util.Properties)1 Message (javax.mail.Message)1 Session (javax.mail.Session)1