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;
}
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);
}
}
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;
}
Aggregations