本文整理汇总了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;
}
示例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);
}
}
}
}
示例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;
}
示例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);
}
}
示例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;
}
示例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);
}
示例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;
}
示例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;
}
示例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);
}