Example usage for javax.mail Message getRecipients

List of usage examples for javax.mail Message getRecipients

Introduction

In this page you can find the example usage for javax.mail Message getRecipients.

Prototype

public abstract Address[] getRecipients(RecipientType type) throws MessagingException;

Source Link

Document

Get all the recipient addresses of the given type.

Usage

From source file:gov.nih.nci.firebird.service.messages.email.EmailServiceImplTest.java

@Test
public void testSendMessageEmptyCC() throws MessagingException {
    Collection<String> cc = new HashSet<String>();
    bean.sendMessage(TEST_TO_ADDRESS, cc, null, testMessage);
    Message message = mailbox.get(0);
    assertNull(message.getRecipients(RecipientType.CC));
}

From source file:com.cubusmail.server.services.ConvertUtil.java

/**
 * @param folder// w  w w. j a  v a 2s  . c o  m
 * @param msg
 * @param result
 * @throws MessagingException
 */
private static void convertToStringArray(IMAPFolder folder, Message msg, GWTMessageRecord result,
        DateFormat dateFormat, NumberFormat decimalFormat) throws MessagingException {

    result.setId(Long.toString(folder.getUID(msg)));
    try {
        result.setAttachmentImage(MessageUtils.hasAttachments(msg) ? ImageProvider.MSG_ATTACHMENT : null);
    } catch (IOException e) {
        // do nothing
    }
    GWTMessageFlags flags = new GWTMessageFlags();
    flags.setDeleted(msg.isSet(Flags.Flag.DELETED));
    flags.setUnread(!msg.isSet(Flags.Flag.SEEN));
    flags.setAnswered(msg.isSet(Flags.Flag.ANSWERED));
    flags.setDraft(msg.isSet(Flags.Flag.DRAFT));
    result.setFlags(flags);

    result.setProrityImage(getPriorityImage(msg));
    result.setSubject(HtmlUtils.htmlEscape(msg.getSubject()));

    result.setFrom(
            HtmlUtils.htmlEscape(MessageUtils.getMailAdressString(msg.getFrom(), AddressStringType.PERSONAL)));
    result.setTo(HtmlUtils.htmlEscape(
            MessageUtils.getMailAdressString(msg.getRecipients(RecipientType.TO), AddressStringType.PERSONAL)));
    if (msg.getSentDate() != null) {
        result.setSendDateString(HtmlUtils.htmlEscape(dateFormat.format(msg.getSentDate())));
        result.setSendDate(msg.getSentDate());
    }

    int msgSize = MessageUtils.calculateAttachmentSize(msg.getSize());
    result.setSizeString(HtmlUtils.htmlEscape(MessageUtils.formatPartSize(msgSize, decimalFormat)));
    result.setSize(msgSize);
}

From source file:com.cubusmail.mail.util.MessageUtils.java

/**
 * @param mailFolder//  w  w w  .  j  a va 2s  .  com
 * @param msgs
 * @param extendedSearchFields
 * @param params
 * @return
 */
public static Message[] filterMessages(IMailFolder mailFolder, Message[] msgs, String extendedSearchFields,
        String[][] params) {

    if (!StringUtils.isEmpty(extendedSearchFields)) {
        String[] fields = StringUtils.split(extendedSearchFields, ',');

        List<Message> filteredMsgs = new ArrayList<Message>();
        String fromValue = getParamValue(params, SearchFields.FROM.name());
        String toValue = getParamValue(params, SearchFields.TO.name());
        String ccValue = getParamValue(params, SearchFields.CC.name());
        String subjectValue = getParamValue(params, SearchFields.SUBJECT.name());
        String contentValue = getParamValue(params, SearchFields.CONTENT.name());
        String dateFromValue = getParamValue(params, SearchFields.DATE_FROM.name());
        String dateToValue = getParamValue(params, SearchFields.DATE_TO.name());

        try {
            // Body search
            if (StringUtils.contains(extendedSearchFields, SearchFields.CONTENT.name())) {
                BodyTerm term = new BodyTerm(contentValue);
                msgs = mailFolder.search(term, msgs);
                if (msgs == null) {
                    msgs = new Message[0];
                }
            }

            for (Message message : msgs) {
                boolean contains = true;
                for (String searchField : fields) {
                    if (SearchFields.FROM.name().equals(searchField)) {
                        String from = MessageUtils.getMailAdressString(message.getFrom(),
                                AddressStringType.COMPLETE);
                        contains = StringUtils.containsIgnoreCase(from, fromValue);
                    }
                    if (contains && SearchFields.TO.name().equals(searchField)) {
                        String to = MessageUtils.getMailAdressString(
                                message.getRecipients(Message.RecipientType.TO), AddressStringType.COMPLETE);
                        if (!StringUtils.isEmpty(to)) {
                            contains = StringUtils.containsIgnoreCase(to, toValue);
                        } else {
                            contains = false;
                        }
                    }
                    if (contains && SearchFields.CC.name().equals(searchField)) {
                        String cc = MessageUtils.getMailAdressString(
                                message.getRecipients(Message.RecipientType.CC), AddressStringType.COMPLETE);
                        if (!StringUtils.isEmpty(cc)) {
                            contains = StringUtils.containsIgnoreCase(cc, ccValue);
                        } else {
                            contains = false;
                        }
                    }
                    if (contains && SearchFields.SUBJECT.name().equals(searchField)) {
                        if (!StringUtils.isEmpty(message.getSubject())) {
                            contains = StringUtils.containsIgnoreCase(message.getSubject(), subjectValue);
                        } else {
                            contains = false;
                        }
                    }
                    if (contains && SearchFields.DATE_FROM.name().equals(searchField)) {
                        Date dateFrom = new Date(Long.parseLong(dateFromValue));
                        if (message.getSentDate() != null) {
                            contains = !message.getSentDate().before(dateFrom);
                        } else {
                            contains = false;
                        }
                    }
                    if (contains && SearchFields.DATE_TO.name().equals(searchField)) {
                        Date dateTo = new Date(Long.parseLong(dateToValue));
                        if (message.getSentDate() != null) {
                            contains = !message.getSentDate().after(dateTo);
                        } else {
                            contains = false;
                        }
                    }
                }
                if (contains) {
                    filteredMsgs.add(message);
                }
            }
        } catch (MessagingException ex) {
            log.warn(ex.getMessage());
        }

        return filteredMsgs.toArray(new Message[0]);
    }

    return msgs;
}

From source file:com.opinionlab.woa.Awesome.java

public Awesome(Message message) {
    try {//w  w  w.  j av  a 2s. c om
        this.messageID = ((MimeMessage) message).getMessageID();
        this.id = ID_PTN.matcher(this.messageID).replaceAll("_");

        this.receivedDate = message.getReceivedDate();
        this.monthDay = DATE_FORMATTER
                .format(LocalDateTime.ofInstant(receivedDate.toInstant(), systemDefault()));
        this.to = new Person((InternetAddress) //todo Do this smarter
        message.getRecipients(Message.RecipientType.TO)[0]);
        this.from = new Person((InternetAddress) message.getFrom()[0]);
        this.subject = message.getSubject();

        final MimeMessageParser parser = new MimeMessageParser((MimeMessage) message).parse();
        if (!parser.hasPlainContent()) {
            LOGGER.error(format("Unable to parse message '%s'; has no plain content", this.messageID));
            this.comment = "Unknown content.";
        } else {
            this.comment = parseContent(parser.getPlainContent());
        }
    } catch (Throwable t) {
        throw new IllegalArgumentException(t);
    }
}

From source file:gov.nih.nci.firebird.service.messages.email.EmailServiceImplTest.java

@Test
public void testSendMessage() throws MessagingException, IOException {
    bean.sendMessage(TEST_TO_ADDRESS, TEST_CC_ADDRESSES, TEST_BOUNCE_ADDRESS, testMessage);
    Message message = mailbox.get(0);
    InternetAddress fromAddress = (InternetAddress) message.getFrom()[0];
    assertEquals(TEST_FROM_ADDRESS, fromAddress.getAddress());
    assertEquals(TEST_FROM_NAME, fromAddress.getPersonal());
    InternetAddress toAddress = (InternetAddress) message.getRecipients(RecipientType.TO)[0];
    assertEquals(TEST_TO_ADDRESS, toAddress.getAddress());
    assertEquals(2, message.getRecipients(RecipientType.CC).length);
    InternetAddress ccAddress1 = (InternetAddress) message.getRecipients(RecipientType.CC)[0];
    InternetAddress ccAddress2 = (InternetAddress) message.getRecipients(RecipientType.CC)[1];
    assertEquals(TEST_CC_ADDRESSES.get(0), ccAddress1.getAddress());
    assertEquals(TEST_CC_ADDRESSES.get(1), ccAddress2.getAddress());
    assertEquals(TEST_SUBJECT, message.getSubject());
    assertEquals(TEST_CONTENT, message.getContent());
}

From source file:gov.nih.nci.firebird.service.messages.email.EmailServiceImplTest.java

private String doSendMessageTest(List<String> cc) throws MessagingException, IOException {
    String overrideEmail = "overrideEmail@example.com";
    mailbox = Mailbox.get(overrideEmail);
    bean.setOverrideEmailAddress(overrideEmail);
    bean.sendMessage(TEST_TO_ADDRESS, cc, null, testMessage);
    Message message = mailbox.get(0);
    InternetAddress fromAddress = (InternetAddress) message.getFrom()[0];
    assertEquals(TEST_FROM_ADDRESS, fromAddress.getAddress());
    assertEquals(TEST_FROM_NAME, fromAddress.getPersonal());
    InternetAddress toAddress = (InternetAddress) message.getRecipients(RecipientType.TO)[0];
    assertEquals(overrideEmail, toAddress.getAddress());
    assertNull(message.getRecipients(RecipientType.CC));
    assertEquals(TEST_SUBJECT, message.getSubject());
    assertTrue(message.getContent().toString().contains(TEST_CONTENT));
    assertTrue(message.getContent().toString().contains(EmailServiceImpl.TO_OVERRIDE_HEADING));
    assertTrue(message.getContent().toString().contains(TEST_TO_ADDRESS));
    assertTrue(message.getContent().toString().contains(EmailServiceImpl.LINE_SEPARATOR));

    return message.getContent().toString();
}

From source file:nz.net.orcon.kanban.automation.actions.EmailReceiverAction.java

public void downloadEmails(String mailStoreProtocol, String mailStoreHost, String mailStoreUserName,
        String mailStorePassword) throws IOException {

    Session session = getMailStoreSession(mailStoreProtocol, mailStoreHost, mailStoreUserName,
            mailStorePassword);//from  ww  w.j  a v  a  2 s  .  co  m
    try {
        // connects to the message store
        Store store = session.getStore(mailStoreProtocol);
        store.connect(mailStoreHost, mailStoreUserName, mailStorePassword);

        logger.info("connected to message store");

        // opens the inbox folder
        Folder folderInbox = store.getFolder("INBOX");
        folderInbox.open(Folder.READ_ONLY);

        // fetches new messages from server
        Message[] messages = folderInbox.getMessages();

        for (int i = 0; i < messages.length; i++) {
            Message msg = messages[i];
            Address[] fromAddress = msg.getFrom();
            String from = fromAddress[0].toString();
            String subject = msg.getSubject();
            String toList = parseAddresses(msg.getRecipients(RecipientType.TO));
            String ccList = parseAddresses(msg.getRecipients(RecipientType.CC));
            String sentDate = msg.getSentDate().toString();

            String contentType = msg.getContentType();
            String messageContent = "";

            if (contentType.contains("text/plain") || contentType.contains("text/html")) {
                try {
                    Object content = msg.getContent();
                    if (content != null) {
                        messageContent = content.toString();
                    }
                } catch (Exception ex) {
                    messageContent = "[Error downloading content]";
                    ex.printStackTrace();
                }
            }

            // print out details of each message
            System.out.println("Message #" + (i + 1) + ":");
            System.out.println("\t From: " + from);
            System.out.println("\t To: " + toList);
            System.out.println("\t CC: " + ccList);
            System.out.println("\t Subject: " + subject);
            System.out.println("\t Sent Date: " + sentDate);
            System.out.println("\t Message: " + messageContent);
        }

        // disconnect
        folderInbox.close(false);
        store.close();
    } catch (NoSuchProviderException ex) {
        logger.warn("No provider for protocol: " + mailStoreProtocol + " " + ex);
    } catch (MessagingException ex) {
        logger.error("Could not connect to the message store" + ex);
    }
}

From source file:com.canoo.webtest.plugins.emailtest.AbstractSelectStep.java

boolean messageMatches(final Message message) throws MessagingException {
    if (!doMatch(getFrom(), message.getFrom()[0].toString())) {
        return false;
    }//from   ww w  .ja  v  a  2s. c  om
    if (!doMatch(getSubject(), message.getSubject())) {
        return false;
    }
    if (!doMatchMultiple(getReplyTo(), message.getReplyTo())) {
        return false;
    }
    if (!doMatchMultiple(getCc(), message.getRecipients(MimeMessage.RecipientType.CC))) {
        return false;
    }
    return doMatchMultiple(getTo(), message.getRecipients(MimeMessage.RecipientType.TO));
}

From source file:com.cubusmail.gwtui.server.services.ConvertUtil.java

/**
 * @param folder/*from w w w  . java 2s  .  c om*/
 * @param msg
 * @param result
 * @throws MessagingException
 */
public static void convertToStringArray(IMailFolder folder, Message msg, String[] result, DateFormat dateFormat,
        NumberFormat decimalFormat) throws MessagingException {

    result[MessageListFields.ID.ordinal()] = Long.toString(folder.getUID(msg));
    try {
        result[MessageListFields.ATTACHMENT_FLAG.ordinal()] = Boolean
                .toString(MessageUtils.hasAttachments(msg));
    } catch (IOException e) {
        // do nothing
    }
    result[MessageListFields.READ_FLAG.ordinal()] = Boolean.toString(msg.isSet(Flags.Flag.SEEN));
    result[MessageListFields.DELETED_FLAG.ordinal()] = Boolean.toString(msg.isSet(Flags.Flag.DELETED));
    result[MessageListFields.ANSWERED_FLAG.ordinal()] = Boolean.toString(msg.isSet(Flags.Flag.ANSWERED));
    result[MessageListFields.DRAFT_FLAG.ordinal()] = Boolean.toString(msg.isSet(Flags.Flag.DRAFT));
    result[MessageListFields.PRIORITY.ordinal()] = Integer.toString(MessageUtils.getMessagePriority(msg));
    if (!StringUtils.isEmpty(msg.getSubject())) {
        result[MessageListFields.SUBJECT.ordinal()] = msg.getSubject();
    }
    result[MessageListFields.FROM.ordinal()] = MessageUtils.getMailAdressString(msg.getFrom(),
            AddressStringType.PERSONAL);
    result[MessageListFields.TO.ordinal()] = MessageUtils
            .getMailAdressString(msg.getRecipients(RecipientType.TO), AddressStringType.PERSONAL);
    if (msg.getSentDate() != null) {
        result[MessageListFields.DATE.ordinal()] = dateFormat.format(msg.getSentDate());
    }
    result[MessageListFields.SIZE.ordinal()] = MessageUtils
            .formatPartSize(MessageUtils.calculateAttachmentSize(msg.getSize()), decimalFormat);
}

From source file:org.jasig.portlet.emailpreview.dao.javamail.JavamailAccountDaoImpl.java

private String getTo(Message message) throws MessagingException {
    Address[] toRecipients = message.getRecipients(RecipientType.TO);
    return getFormattedAddresses(toRecipients);
}