Search in sources :

Example 71 with Message

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;
}
Also used : Message(javax.mail.Message) MessagingException(javax.mail.MessagingException) Store(javax.mail.Store) ReadmailHost(org.opennms.netmgt.config.javamail.ReadmailHost) Folder(javax.mail.Folder) UserAuth(org.opennms.netmgt.config.javamail.UserAuth) NoSuchProviderException(javax.mail.NoSuchProviderException)

Example 72 with Message

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());
}
Also used : OrTerm(javax.mail.search.OrTerm) Message(javax.mail.Message) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) SearchTerm(javax.mail.search.SearchTerm) SubjectTerm(javax.mail.search.SubjectTerm) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 73 with Message

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;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) ArrayList(java.util.ArrayList)

Example 74 with 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);
    }
}
Also used : Message(javax.mail.Message) Flags(javax.mail.Flags) Folder(javax.mail.Folder)

Example 75 with Message

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;
}
Also used : Message(javax.mail.Message)

Aggregations

Message (javax.mail.Message)149 MimeMessage (javax.mail.internet.MimeMessage)81 MessagingException (javax.mail.MessagingException)53 InternetAddress (javax.mail.internet.InternetAddress)48 Folder (javax.mail.Folder)41 Test (org.junit.Test)40 Session (javax.mail.Session)37 Properties (java.util.Properties)36 Store (javax.mail.Store)28 Date (java.util.Date)19 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)19 MimeMultipart (javax.mail.internet.MimeMultipart)18 Mailbox (org.jvnet.mock_javamail.Mailbox)18 MimeBodyPart (javax.mail.internet.MimeBodyPart)16 PasswordAuthentication (javax.mail.PasswordAuthentication)15 IOException (java.io.IOException)13 ArrayList (java.util.ArrayList)13 Multipart (javax.mail.Multipart)13 Address (javax.mail.Address)11 HashMap (java.util.HashMap)8