use of javax.mail.Message in project opennms by OpenNMS.
the class JavaReadMailer method retrieveMessages.
/*
* TODO: Need readers that:
* - use FetchProfiles
* - make use of pre-fetch nature of getMessages()
* - A reader for the entire system could be implemented using events/event listeners
* TODO: Need to make this more efficient... probably needs state so that message contents can be retrieved from the
* store after they are read via this message.
*/
/**
* <p>retrieveMessages</p>
*
* @param term a {@link javax.mail.search.SearchTerm} object.
* @return a {@link java.util.List} object.
* @throws org.opennms.javamail.JavaMailerException if any.
*/
public List<Message> retrieveMessages(SearchTerm term) throws JavaMailerException {
Message[] msgs;
Folder mailFolder = null;
final ReadmailHost readmailHost = getReadmailHost(m_config);
final UserAuth userAuth = getUserAuth(m_config);
try {
Store store = m_session.getStore(readmailHost.getReadmailProtocol().getTransport());
store.connect(readmailHost.getHost(), (int) readmailHost.getPort(), userAuth.getUserName(), userAuth.getPassword());
mailFolder = store.getFolder(m_config.getMailFolder());
mailFolder.open(Folder.READ_WRITE);
msgs = mailFolder.search(term);
} catch (NoSuchProviderException e) {
throw new JavaMailerException("No provider matching:" + readmailHost.getReadmailProtocol().getTransport() + " from config:" + m_config.getName(), e);
} catch (MessagingException e) {
throw new JavaMailerException("Problem reading messages from configured mail store", e);
}
List<Message> msgList = Arrays.asList(msgs);
return msgList;
}
use of javax.mail.Message in project opennms by OpenNMS.
the class JavaReadMailerTest method testReadMessagesWithSearchTerm.
/**
* Un-ignore this test with a proper gmail account
* @throws JavaMailerException
* @throws MessagingException
* @throws InterruptedException
*/
@Test
@Ignore
public void testReadMessagesWithSearchTerm() throws JavaMailerException, MessagingException, InterruptedException {
String gmailAccount = getUser();
String gmailPassword = getPassword();
JavaSendMailer sendMailer = createSendMailer(gmailAccount, gmailPassword);
String term1 = String.valueOf(Calendar.getInstance().getTimeInMillis());
Thread.sleep(2);
String term2 = String.valueOf(Calendar.getInstance().getTimeInMillis());
Thread.sleep(2);
String term3 = String.valueOf(Calendar.getInstance().getTimeInMillis());
Thread.sleep(2);
SendmailMessage sendMsg = createAckMessage(gmailAccount, "1", term1, "ack");
sendMailer.setMessage(sendMailer.buildMimeMessage(sendMsg));
sendMailer.send();
sendMsg = createAckMessage(gmailAccount, "2", term2, "ack");
sendMailer.setMessage(sendMailer.buildMimeMessage(sendMsg));
sendMailer.send();
sendMsg = createAckMessage(gmailAccount, "3", term3, "ack");
sendMailer.setMessage(sendMailer.buildMimeMessage(sendMsg));
sendMailer.send();
JavaReadMailer readMailer = createGoogleReadMailer(gmailAccount, gmailPassword);
// See if search finds all 3 messages
SearchTerm st = new OrTerm(new SubjectTerm(".*" + term1 + " #.*"), new SubjectTerm(".*" + term2 + " #.*"));
st = new OrTerm(st, new SubjectTerm("*." + term3 + " #.*"));
List<Message> msgs = null;
try {
msgs = readMailer.retrieveMessages(st);
} catch (JavaMailerException e) {
e.printStackTrace();
}
Assert.assertEquals(3, msgs.size());
st = new OrTerm(new SubjectTerm(".*" + term1 + " #.*"), new SubjectTerm(".*" + term2 + " #.*"));
try {
msgs = readMailer.retrieveMessages(st);
} catch (JavaMailerException e) {
e.printStackTrace();
}
// Should find only term1 and term2 messages
Assert.assertNotNull(msgs);
Assert.assertEquals(2, msgs.size());
// Delete the term1 and term2 messages
for (Message msg : msgs) {
msg.setFlag(Flag.DELETED, true);
}
// Find and delete the term3 messages
st = new SubjectTerm("*." + term3 + " #.*");
try {
msgs = readMailer.retrieveMessages(st);
} catch (JavaMailerException e) {
e.printStackTrace();
}
Assert.assertNotNull(msgs);
Assert.assertTrue(msgs.size() >= 1);
for (Message eventMsg : msgs) {
eventMsg.setFlag(Flag.DELETED, true);
}
// Make sure they're all gone
st = new OrTerm(new SubjectTerm(".*" + term1 + " #.*"), new SubjectTerm(".*" + term2 + " #.*"));
st = new OrTerm(st, new SubjectTerm("*." + term3 + " #.*"));
try {
msgs = readMailer.retrieveMessages(st);
} catch (JavaMailerException e) {
e.printStackTrace();
}
Assert.assertTrue(msgs.isEmpty());
}
use of javax.mail.Message in project hub-alert by blackducksoftware.
the class EmailMessagingService method createMessage.
private Message createMessage(final String emailAddress, final String subjectLine, final Session session, final MimeMultipart mimeMultipart, final EmailProperties emailProperties) throws MessagingException {
final List<InternetAddress> addresses = new ArrayList<>();
try {
addresses.add(new InternetAddress(emailAddress));
} catch (final AddressException e) {
logger.warn(String.format("Could not create the address from %s: %s", emailAddress, e.getMessage()));
}
if (addresses.isEmpty()) {
throw new RuntimeException("There were no valid email addresses supplied.");
}
final Message message = new MimeMessage(session);
message.setContent(mimeMultipart);
message.setFrom(new InternetAddress(emailProperties.getJavamailOption(EmailProperties.JAVAMAIL_FROM_KEY)));
message.setRecipients(Message.RecipientType.TO, addresses.toArray(new Address[addresses.size()]));
message.setSubject(subjectLine);
return message;
}
use of javax.mail.Message in project Lucee by lucee.
the class MailClient method markRead.
public void markRead(String folderName) throws MessagingException, ApplicationException {
if (StringUtil.isEmpty(folderName))
folderName = "INBOX";
Folder folder = null;
try {
folder = getFolder(folderName, true, true, false);
folder.open(2);
Message[] msgs = folder.getMessages();
folder.setFlags(msgs, new Flags(Flags.Flag.SEEN), true);
} finally {
IOUtil.closeEL(folder);
}
}
use of javax.mail.Message in project Lucee by lucee.
the class MailClient method getMessages.
/**
* gets all messages from given Folder that match given criteria
* @param qry
* @param folder
* @param uIds
* @param messageNumbers
* @param all
* @param startrow
* @param maxrows
* @return
* @return matching Messages
* @throws MessagingException
*/
private Map<String, Message> getMessages(Query qry, Folder folder, String[] uids, String[] messageNumbers, int startRow, int maxRow, boolean all) throws MessagingException {
Message[] messages = folder.getMessages();
Map<String, Message> map = qry == null ? new HashMap<String, Message>() : null;
int k = 0;
if (uids != null || messageNumbers != null) {
startRow = 0;
maxRow = -1;
}
Message message;
for (int l = startRow; l < messages.length; l++) {
if (maxRow != -1 && k == maxRow) {
break;
}
message = messages[l];
int messageNumber = message.getMessageNumber();
String id = getId(folder, message);
if (uids == null ? messageNumbers == null || contains(messageNumbers, messageNumber) : contains(uids, id)) {
k++;
if (qry != null) {
toQuery(qry, message, id, all);
} else
map.put(id, message);
}
}
return map;
}
Aggregations