List of usage examples for javax.mail.search AndTerm AndTerm
public AndTerm(SearchTerm t1, SearchTerm t2)
From source file:org.springframework.ws.transport.mail.monitor.AbstractMonitoringStrategy.java
/** * Retrieves new messages from the given folder. This implementation creates a {@link SearchTerm} that searches for * all messages in the folder that are {@link javax.mail.Flags.Flag#RECENT RECENT}, not {@link * javax.mail.Flags.Flag#ANSWERED ANSWERED}, and not {@link javax.mail.Flags.Flag#DELETED DELETED}. The search term * is used to {@link Folder#search(SearchTerm) search} for new messages. * * @param folder the folder to retrieve new messages from * @return the new messages//from ww w. ja v a 2s . c o m * @throws MessagingException in case of JavaMail errors */ protected Message[] searchForNewMessages(Folder folder) throws MessagingException { if (!folder.isOpen()) { return new Message[0]; } Flags supportedFlags = folder.getPermanentFlags(); SearchTerm searchTerm = null; if (supportedFlags != null) { if (supportedFlags.contains(Flags.Flag.RECENT)) { searchTerm = new FlagTerm(new Flags(Flags.Flag.RECENT), true); } if (supportedFlags.contains(Flags.Flag.ANSWERED)) { FlagTerm answeredTerm = new FlagTerm(new Flags(Flags.Flag.ANSWERED), false); if (searchTerm == null) { searchTerm = answeredTerm; } else { searchTerm = new AndTerm(searchTerm, answeredTerm); } } if (supportedFlags.contains(Flags.Flag.DELETED)) { FlagTerm deletedTerm = new FlagTerm(new Flags(Flags.Flag.DELETED), false); if (searchTerm == null) { searchTerm = deletedTerm; } else { searchTerm = new AndTerm(searchTerm, deletedTerm); } } } return searchTerm != null ? folder.search(searchTerm) : folder.getMessages(); }
From source file:org.wso2.esb.integration.common.utils.MailToTransportUtil.java
/** * Check a particular email has received to a given email folder by email subject. * * @param emailSubject - Email emailSubject to find email is in inbox or not * @return - found the email or not/*from ww w.j a v a 2 s.c o m*/ * @throws ESBMailTransportIntegrationTestException - Is thrown if an error occurred while reading the emails */ public static boolean isMailReceivedBySubject(String emailSubject, String folder) throws ESBMailTransportIntegrationTestException { boolean emailReceived = false; Folder mailFolder; Store store = getConnection(); try { mailFolder = store.getFolder(folder); mailFolder.open(Folder.READ_WRITE); SearchTerm searchTerm = new AndTerm(new SubjectTerm(emailSubject), new BodyTerm(emailSubject)); Message[] messages = mailFolder.search(searchTerm); for (Message message : messages) { if (message.getSubject().contains(emailSubject)) { log.info("Found the email emailSubject : " + emailSubject); emailReceived = true; break; } } return emailReceived; } catch (MessagingException ex) { log.error("Error when getting mail count ", ex); throw new ESBMailTransportIntegrationTestException("Error when getting mail count ", ex); } finally { if (store != null) { try { store.close(); } catch (MessagingException e) { log.warn("Error when closing the store ", e); } } } }
From source file:org.wso2.esb.integration.common.utils.servers.GreenMailServer.java
/** * Check mail folder for an email using subject. * * @param emailSubject Email subject// w w w.ja v a 2s . c om * @param folder mail folder to check for an email * @param protocol protocol used to connect to the server * @return whether mail received or not * @throws MessagingException if we're unable to connect to the store */ private static boolean isMailReceivedBySubject(String emailSubject, String folder, String protocol, GreenMailUser user) throws MessagingException { boolean emailReceived = false; Folder mailFolder; Store store = getConnection(user, protocol); try { mailFolder = store.getFolder(folder); mailFolder.open(Folder.READ_WRITE); SearchTerm searchTerm = new AndTerm(new SubjectTerm(emailSubject), new BodyTerm(emailSubject)); Message[] messages = mailFolder.search(searchTerm); for (Message message : messages) { if (message.getSubject().contains(emailSubject)) { log.info("Found the Email with Subject : " + emailSubject); emailReceived = true; break; } } } finally { if (store != null) { store.close(); } } return emailReceived; }