Search in sources :

Example 1 with IMAPFolder

use of com.sun.mail.imap.IMAPFolder in project nhin-d by DirectProject.

the class MessageServiceImplService method requestStatus.

@Override
public StatusResponseType requestStatus(StatusRefType body) {
    List<String> msgs = body.getMessageID();
    StatusResponseType response = new StatusResponseType();
    try {
        checkAuth(response);
        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getInstance(imapProps, auth);
        session.setDebug(true);
        if (msgs.size() > 0) {
            Store store = session.getStore(new javax.mail.URLName("imaps://" + username));
            store.connect(getImapHost(), Integer.valueOf(getImapPort()).intValue(), username, password);
            for (int x = 0; x < msgs.size(); x++) {
                String msgid = msgs.get(x);
                MessageIDTerm messageIdTerm = new MessageIDTerm(msgid);
                IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
                folder.open(Folder.READ_ONLY);
                SearchTerm st = messageIdTerm;
                IMAPMessage[] msgsearch = (IMAPMessage[]) folder.search(st);
                if (msgsearch.length > 0) {
                    Flags flags = msgsearch[0].getFlags();
                    Flag[] inboxflags = flags.getSystemFlags();
                    String[] listofflags = new String[inboxflags.length];
                    listofflags = setSystemFlags(inboxflags);
                    setMessageIdStatus(msgid, listofflags, response.getMessageIDAndStatus());
                }
            }
        }
    } catch (AddressException e) {
        log.error(e);
    } catch (MessagingException e) {
        log.error(e);
    } catch (Exception e) {
        log.error(e);
    }
    return response;
}
Also used : MessageIDTerm(javax.mail.search.MessageIDTerm) MessagingException(javax.mail.MessagingException) IMAPFolder(com.sun.mail.imap.IMAPFolder) Store(javax.mail.Store) Flags(javax.mail.Flags) SearchTerm(javax.mail.search.SearchTerm) Flag(javax.mail.Flags.Flag) StatusResponseType(org.nhindirect.schema.edge.ws.StatusResponseType) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) InvalidPropertyException(org.springframework.beans.InvalidPropertyException) IOException(java.io.IOException) AddressException(javax.mail.internet.AddressException) IMAPMessage(com.sun.mail.imap.IMAPMessage) Authenticator(javax.mail.Authenticator) Session(javax.mail.Session)

Example 2 with IMAPFolder

use of com.sun.mail.imap.IMAPFolder in project Openfire by igniterealtime.

the class EmailListener method listenMessages.

private void listenMessages() {
    try {
        // Add messageCountListener to listen for new messages
        messageListener = new MessageCountAdapter() {

            @Override
            public void messagesAdded(MessageCountEvent ev) {
                Message[] msgs = ev.getMessages();
                // Send new messages to specified users
                for (Message msg : msgs) {
                    try {
                        sendMessage(msg);
                    } catch (Exception e) {
                        Log.error("Error while sending new email message", e);
                    }
                }
            }
        };
        folder.addMessageCountListener(messageListener);
        // Check mail once in "freq" MILLIseconds
        int freq = getFrequency();
        boolean supportsIdle = false;
        try {
            if (folder instanceof IMAPFolder) {
                IMAPFolder f = (IMAPFolder) folder;
                f.idle();
                supportsIdle = true;
            }
        } catch (FolderClosedException fex) {
            throw fex;
        } catch (MessagingException mex) {
            supportsIdle = false;
        }
        while (messageListener != null) {
            if (supportsIdle && folder instanceof IMAPFolder) {
                IMAPFolder f = (IMAPFolder) folder;
                f.idle();
            } else {
                // sleep for freq milliseconds
                Thread.sleep(freq);
                // EXISTS notifications.
                if (folder != null && folder.isOpen()) {
                    folder.getMessageCount();
                }
            }
        }
    } catch (Exception ex) {
        Log.error("Error listening new email messages", ex);
    }
}
Also used : MessageCountEvent(javax.mail.event.MessageCountEvent) MessageCountAdapter(javax.mail.event.MessageCountAdapter) IMAPFolder(com.sun.mail.imap.IMAPFolder)

Example 3 with IMAPFolder

use of com.sun.mail.imap.IMAPFolder in project camel by apache.

the class MailConsumer method retrieveMessages.

/**
     * @return Messages from input folder according to the search and sort criteria stored in the endpoint
     * @throws MessagingException If message retrieval fails
     */
private List<KeyValueHolder<String, Message>> retrieveMessages() throws MessagingException {
    List<KeyValueHolder<String, Message>> answer = new ArrayList<>();
    Message[] messages;
    final SortTerm[] sortTerm = getEndpoint().getSortTerm();
    final SearchTerm searchTerm = computeSearchTerm();
    if (sortTerm != null && serverCanSort) {
        final IMAPFolder imapFolder = (IMAPFolder) folder;
        if (searchTerm != null) {
            // Sort and search using server capability
            messages = imapFolder.getSortedMessages(sortTerm, searchTerm);
        } else {
            // Only sort using server capability
            messages = imapFolder.getSortedMessages(sortTerm);
        }
    } else {
        if (searchTerm != null) {
            messages = folder.search(searchTerm, retrieveAllMessages());
        } else {
            messages = retrieveAllMessages();
        }
        // Now we can sort (emulate email sort but restrict sort terms)
        if (sortTerm != null) {
            MailSorter.sortMessages(messages, sortTerm);
        }
    }
    for (Message message : messages) {
        String key = getEndpoint().getMailUidGenerator().generateUuid(getEndpoint(), message);
        if (isValidMessage(key, message)) {
            answer.add(new KeyValueHolder<>(key, message));
        }
    }
    return answer;
}
Also used : SortTerm(com.sun.mail.imap.SortTerm) Message(javax.mail.Message) IMAPFolder(com.sun.mail.imap.IMAPFolder) ArrayList(java.util.ArrayList) KeyValueHolder(org.apache.camel.util.KeyValueHolder) SearchTerm(javax.mail.search.SearchTerm)

Aggregations

IMAPFolder (com.sun.mail.imap.IMAPFolder)3 SearchTerm (javax.mail.search.SearchTerm)2 IMAPMessage (com.sun.mail.imap.IMAPMessage)1 SortTerm (com.sun.mail.imap.SortTerm)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Authenticator (javax.mail.Authenticator)1 Flags (javax.mail.Flags)1 Flag (javax.mail.Flags.Flag)1 Message (javax.mail.Message)1 MessagingException (javax.mail.MessagingException)1 Session (javax.mail.Session)1 Store (javax.mail.Store)1 MessageCountAdapter (javax.mail.event.MessageCountAdapter)1 MessageCountEvent (javax.mail.event.MessageCountEvent)1 AddressException (javax.mail.internet.AddressException)1 MessageIDTerm (javax.mail.search.MessageIDTerm)1 KeyValueHolder (org.apache.camel.util.KeyValueHolder)1 StatusResponseType (org.nhindirect.schema.edge.ws.StatusResponseType)1 InvalidPropertyException (org.springframework.beans.InvalidPropertyException)1