當前位置: 首頁>>代碼示例>>Java>>正文


Java Folder.search方法代碼示例

本文整理匯總了Java中javax.mail.Folder.search方法的典型用法代碼示例。如果您正苦於以下問題:Java Folder.search方法的具體用法?Java Folder.search怎麽用?Java Folder.search使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.mail.Folder的用法示例。


在下文中一共展示了Folder.search方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: isMailReceivedBySubject

import javax.mail.Folder; //導入方法依賴的package包/類
/**
 * Check mail folder for an email using subject.
 *
 * @param emailSubject Email subject
 * @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;
}
 
開發者ID:wso2,項目名稱:product-ei,代碼行數:34,代碼來源:GreenMailServer.java

示例2: isMailReceivedBySubject

import javax.mail.Folder; //導入方法依賴的package包/類
/**
 * 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
 * @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);
            }
        }
    }
}
 
開發者ID:wso2,項目名稱:product-ei,代碼行數:39,代碼來源:MailToTransportUtil.java

示例3: readLatestEmailWithSubject

import javax.mail.Folder; //導入方法依賴的package包/類
/**
 * Reads the latest email with the given subject from the email inbox.
 * 
 * @param subject
 *            the email subject
 * @return the email body or null if no email was found
 * @throws Exception
 */
public String readLatestEmailWithSubject(String subject) throws Exception {
    Store store = mailSession.getStore();
    store.connect();

    Folder folder = store.getFolder(MAIL_INBOX);
    folder.open(Folder.READ_WRITE);

    Message[] messages = null;

    messages = folder.search(new SubjectTerm(subject));

    String body = null;
    if (messages.length > 0) {
        Message latest = messages[0];

        for (Message m : messages) {
            if (latest.getSentDate().compareTo(m.getSentDate()) < 0) {
                latest = m;
            }
        }
        body = (String) latest.getContent();
    }

    folder.close(false);
    store.close();

    return body;
}
 
開發者ID:servicecatalog,項目名稱:development,代碼行數:37,代碼來源:WebTester.java

示例4: checkEmail

import javax.mail.Folder; //導入方法依賴的package包/類
/**
 * Check the inbox for new messages, and process each message.
 */
public void checkEmail() {
	try {
		log("Checking email.", Level.INFO);
        Store store = connectStore();		
	    Folder inbox = store.getFolder("INBOX");
	    if (inbox == null) {
	      throw new BotException("Failed to check email, no INBOX.");
	    }
	    inbox.open(Folder.READ_WRITE);
           Message[] messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
           inbox.setFlags(messages, new Flags(Flags.Flag.SEEN), true);
	    //Message[] messages = inbox.getMessages();
	    if ((messages != null) && (messages.length > 0)) {
   			log("Processing emails", Level.INFO, messages.length);
			Network memory = getBot().memory().newMemory();
			Vertex sense = memory.createVertex(getPrimitive());
			Vertex vertex = sense.getRelationship(Primitive.LASTMESSAGE);
			long lastMessage = 0;
			if (vertex != null) {
				lastMessage = ((Number)vertex.getData()).longValue();
			}
			long maxMessage = 0;
			int count = 0;
			// Increase script timeout.
			Language language = getBot().mind().getThought(Language.class);
			int timeout = language.getMaxStateProcess();
			language.setMaxStateProcess(timeout * 2);
			try {
			    for (int index = 0; index < messages.length; index++) {
			    	if (index > (this.maxEmails * 2)) {
		    			log("Max old email limit reached", Level.WARNING, this.maxEmails * 2);
			    		break;
			    	}
			    	long recievedTime = 0;
			    	if (messages[index].getReceivedDate() == null) {
		    			log("Missing received date", Level.FINE, messages[index].getSubject());
			    		recievedTime = messages[index].getSentDate().getTime();
			    	} else {
			    		recievedTime = messages[index].getReceivedDate().getTime();
			    	}
			    	if ((System.currentTimeMillis() - recievedTime) > DAY) {
						log("Day old email", Level.INFO, messages[index].getSubject());
			    		continue;
			    	}
			    	if (recievedTime > lastMessage) {
			    		count++;
			    		if (count > this.maxEmails) {
			    			log("Max email limit reached", Level.WARNING, this.maxEmails);
			    			break;
			    		}
			    		input(messages[index]);
				    	Utils.sleep(100);
				    	if (recievedTime > maxMessage) {
				    		maxMessage = recievedTime;
				    	}
			    	}
			    }
			    if (maxMessage != 0) {
					sense.setRelationship(Primitive.LASTMESSAGE, memory.createVertex(maxMessage));
			    	memory.save();
			    }
			} finally {
				language.setMaxStateProcess(timeout);
			}
	    }
		log("Done checking email.", Level.INFO);
	    inbox.close(false);
	    store.close();
	} catch (MessagingException exception) {
		log(exception);
	}
}
 
開發者ID:BotLibre,項目名稱:BotLibre,代碼行數:76,代碼來源:Email.java

示例5: getInbox

import javax.mail.Folder; //導入方法依賴的package包/類
/**
 * Return a list in inbox message headers.
 */
public List<String> getInbox() {
	List<String> emails = new ArrayList<String>();
	Store store = null;
	Folder inbox = null;
	try {
		store = connectStore();		
	    inbox = store.getFolder("INBOX");
	    if (inbox == null) {
	      log(new BotException("Failed to access inbox, no INBOX."));
	    }
	    inbox.open(Folder.READ_ONLY);
	    
           Message[] messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
	    //Message[] messages = inbox.getMessages(1, Math.min(inbox.getMessageCount(), 50));
	    for (int index = 0; index < 10 && index < messages.length; index++) {
	      emails.add(0, messages[index].getReceivedDate() + " - " + String.valueOf(getFrom(messages[index])) + ": " + messages[index].getSubject());
	    }
	    inbox.close(false);
	    store.close();
	} catch (MessagingException exception) {
		log(new BotException("Failed to access email: " + exception.getMessage(), exception));
	} finally {
		try {
			if (inbox != null) {
				inbox.close(false);
			}
			if (store != null) {
				store.close();
			}
		} catch (Exception ignore) {}
	}
	return emails;
}
 
開發者ID:BotLibre,項目名稱:BotLibre,代碼行數:37,代碼來源:Email.java

示例6: checkMessages

import javax.mail.Folder; //導入方法依賴的package包/類
protected void checkMessages(Store store, Session session) throws MessagingException {
    if (!store.isConnected()) {
        store.connect();
    }

    // open the default folder
    Folder folder = store.getDefaultFolder();
    if (!folder.exists()) {
        throw new MessagingException("No default (root) folder available");
    }

    // open the inbox
    folder = folder.getFolder(INBOX);
    if (!folder.exists()) {
        throw new MessagingException("No INBOX folder available");
    }

    // get the message count; stop if nothing to do
    folder.open(Folder.READ_WRITE);
    int totalMessages = folder.getMessageCount();
    if (totalMessages == 0) {
        folder.close(false);
        return;
    }

    // get all messages
    Message[] messages = folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
    FetchProfile profile = new FetchProfile();
    profile.add(FetchProfile.Item.ENVELOPE);
    profile.add(FetchProfile.Item.FLAGS);
    profile.add("X-Mailer");
    folder.fetch(messages, profile);

    // process each message
    for (Message message: messages) {
        // process each un-read message
        if (!message.isSet(Flags.Flag.SEEN)) {
            long messageSize = message.getSize();
            if (message instanceof MimeMessage && messageSize >= maxSize) {
                Debug.logWarning("Message from: " + message.getFrom()[0] + "not received, too big, size:" + messageSize + " cannot be more than " + maxSize + " bytes", module);

                // set the message as read so it doesn't continue to try to process; but don't delete it
                message.setFlag(Flags.Flag.SEEN, true);
            } else {
                this.processMessage(message, session);
                if (Debug.verboseOn()) Debug.logVerbose("Message from " + UtilMisc.toListArray(message.getFrom()) + " with subject [" + message.getSubject() + "]  has been processed." , module);
                message.setFlag(Flags.Flag.SEEN, true);
                if (Debug.verboseOn()) Debug.logVerbose("Message [" + message.getSubject() + "] is marked seen", module);

                // delete the message after processing
                if (deleteMail) {
                    if (Debug.verboseOn()) Debug.logVerbose("Message [" + message.getSubject() + "] is being deleted", module);
                    message.setFlag(Flags.Flag.DELETED, true);
                }
            }
        }
    }

    // expunge and close the folder
    folder.close(true);
}
 
開發者ID:gildaslemoal,項目名稱:elpi,代碼行數:62,代碼來源:JavaMailContainer.java

示例7: doActionGmail

import javax.mail.Folder; //導入方法依賴的package包/類
public synchronized static boolean doActionGmail(MessageProcess f,
		int attempts) {
	try {
		Properties props = System.getProperties();
		props.setProperty("mail.smtp.host", "smtp.gmail.com");
		props.setProperty("mail.smtp.socketFactory.port", "465");
		props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.smtp.port", "465");
		
		Session session = Session.getDefaultInstance(props, null);
		Store store = session.getStore("imaps");
		store.connect("smtp.gmail.com", DefaultUserConf.EMAIL,
				DefaultUserConf.PASSWORD);

		Folder inbox = store.getFolder("Inbox");
		inbox.open(Folder.READ_WRITE);

		// Get directory
		Flags seen = new Flags(Flags.Flag.SEEN);
		FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
		Message messages[] = inbox.search(unseenFlagTerm);

		for (int i = 0; i < messages.length; i++) {
			f.treatMessage(messages[i]);
		}

		// Close connection
		inbox.close(true);
		store.close();

		return true;
	} catch (Exception e) {
		Logger.traceERROR(e);
		Logger.traceERROR("Attempts : " + attempts + " / " + MAX_ATTEMPS);
		if (attempts > MAX_ATTEMPS) {
			return false;
		} else {
			doActionGmail(f, attempts++);
		}
	}
	return false;
}
 
開發者ID:philipperemy,項目名稱:Leboncoin,代碼行數:44,代碼來源:MailManager.java

示例8: doActionAEI

import javax.mail.Folder; //導入方法依賴的package包/類
public synchronized static boolean doActionAEI(MessageProcess f,
		int attempts) {
	try {
		Properties props = new Properties();
		props.put("mail.smtp.host", "ssl0.ovh.net");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");

		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(
								"philippe.remy%junior-aei.com", "phremy");
					}
				});

		Store store = session.getStore("imap");
		store.connect("ssl0.ovh.net", "philippe.remy%junior-aei.com",
				"phremy");

		// Get folder
		Folder inbox = store.getFolder("INBOX");
		inbox.open(Folder.READ_WRITE);

		// Get directory
		Flags seen = new Flags(Flags.Flag.SEEN);
		FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
		Message messages[] = inbox.search(unseenFlagTerm);

		for (int i = 0; i < messages.length; i++) {
			f.treatMessage(messages[i]);
		}

		// Close connection
		inbox.close(true);
		store.close();

		return true;
	} catch (Exception e) {
		Logger.traceERROR(e);
		Logger.traceERROR("Attempts : " + attempts + " / " + MAX_ATTEMPS);
		if (attempts > MAX_ATTEMPS) {
			return false;
		} else {
			doActionAEI(f, attempts++);
		}
	}

	return false;
}
 
開發者ID:philipperemy,項目名稱:Leboncoin,代碼行數:54,代碼來源:MailManager.java

示例9: getMessagesByIds

import javax.mail.Folder; //導入方法依賴的package包/類
protected Message[] getMessagesByIds(Folder folder, List<String> messageIds) throws MessagingException {

    List<MessageIDTerm> idTerms = messageIds.stream()
        .map(MessageIDTerm::new)
        .collect(Collectors.toList());

    OrTerm searchTerm = new OrTerm(idTerms.toArray(new MessageIDTerm[idTerms.size()]));

    return folder.search(searchTerm);
  }
 
開發者ID:camunda,項目名稱:camunda-bpm-mail,代碼行數:11,代碼來源:DeleteMailConnector.java


注:本文中的javax.mail.Folder.search方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。