Example usage for javax.mail Message getSubject

List of usage examples for javax.mail Message getSubject

Introduction

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

Prototype

public abstract String getSubject() throws MessagingException;

Source Link

Document

Get the subject of this message.

Usage

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

/**
 * Filter the messages./* w w w.  j ava2  s .c o  m*/
 * 
 * @param msgs
 * @param searchFields
 * @param searchText
 * @return
 */
public static Message[] quickFilterMessages(Message[] msgs, String searchFields, String searchText) {

    if (!StringUtils.isEmpty(searchFields) && !StringUtils.isEmpty(searchText)) {
        searchFields = StringUtils.remove(searchFields, '[');
        searchFields = StringUtils.remove(searchFields, ']');
        searchFields = StringUtils.remove(searchFields, '\"');
        String[] fields = StringUtils.split(searchFields, ',');
        List<Message> filteredMsgs = new ArrayList<Message>();

        Date searchDate = null;
        try {
            searchDate = DateUtils.parseDate(searchText, new String[] { "dd.MM.yyyy" });
        } catch (Exception e) {
            // do nothing
        }

        try {
            for (Message message : msgs) {
                boolean contains = false;
                for (String searchField : fields) {
                    if (MessageListFields.SUBJECT.name().equals(searchField)) {
                        String subject = message.getSubject();
                        contains = StringUtils.containsIgnoreCase(subject, searchText);
                    } else if (MessageListFields.FROM.name().equals(searchField)) {
                        String from = MessageUtils.getMailAdressString(message.getFrom(),
                                AddressStringType.COMPLETE);
                        contains = StringUtils.containsIgnoreCase(from, searchText) || contains;
                    } else if (searchDate != null && MessageListFields.DATE.name().equals(searchField)) {
                        Date sendDate = message.getSentDate();
                        contains = (sendDate != null && DateUtils.isSameDay(searchDate, sendDate)) || contains;
                    }
                }
                if (contains) {
                    filteredMsgs.add(message);
                }
            }
        } catch (MessagingException ex) {
            log.warn(ex.getMessage(), ex);
        }

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

    return msgs;
}

From source file:uidmsgshow.java

public static void dumpEnvelope(Message m) throws Exception {
    System.out.println("This is the message envelope");
    System.out.println("---------------------------");
    Address[] a;//from   w  ww . j a v a 2 s  .  c  o m
    // FROM
    if ((a = m.getFrom()) != null) {
        for (int j = 0; j < a.length; j++)
            System.out.println("FROM: " + a[j].toString());
    }

    // TO
    if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
        for (int j = 0; j < a.length; j++)
            System.out.println("TO: " + a[j].toString());
    }

    // SUBJECT
    System.out.println("SUBJECT: " + m.getSubject());

    // DATE
    Date d = m.getSentDate();
    System.out.println("SendDate: " + (d != null ? d.toString() : "UNKNOWN"));

    // SIZE
    System.out.println("Size: " + m.getSize());

    // FLAGS:
    Flags flags = m.getFlags();
    StringBuffer sb = new StringBuffer();
    Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags

    boolean first = true;
    for (int i = 0; i < sf.length; i++) {
        String s;
        Flags.Flag f = sf[i];
        if (f == Flags.Flag.ANSWERED)
            s = "\\Answered";
        else if (f == Flags.Flag.DELETED)
            s = "\\Deleted";
        else if (f == Flags.Flag.DRAFT)
            s = "\\Draft";
        else if (f == Flags.Flag.FLAGGED)
            s = "\\Flagged";
        else if (f == Flags.Flag.RECENT)
            s = "\\Recent";
        else if (f == Flags.Flag.SEEN)
            s = "\\Seen";
        else
            continue; // skip it
        if (first)
            first = false;
        else
            sb.append(' ');
        sb.append(s);
    }

    String[] uf = flags.getUserFlags(); // get the user flag strings
    for (int i = 0; i < uf.length; i++) {
        if (first)
            first = false;
        else
            sb.append(' ');
        sb.append(uf[i]);
    }
    System.out.println("FLAGS = " + sb.toString());

    // X-MAILER
    String[] hdrs = m.getHeader("X-Mailer");
    if (hdrs != null)
        System.out.println("X-Mailer: " + hdrs[0]);
    else
        System.out.println("X-Mailer NOT available");
}

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

/**
 * @param folder//from   w ww  .  j av  a2 s.  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.cognifide.qa.bb.email.SubjectSearchTerm.java

private String getSubjectFromMessage(Message msg) {
    String result = null;/*  ww  w .j  av  a2  s  .  c o m*/
    try {
        result = msg.getSubject();
    } catch (MessagingException e) {
        LOGGER.error("error when getting email subject", e);
    }
    return result;
}

From source file:com.adaptris.mail.SubjectFilter.java

@Override
List<String> getHeaders(Message m) throws MessagingException {
    return Arrays.asList(new String[] { StringUtils.defaultIfEmpty(m.getSubject(), "") });
}

From source file:nl.ordina.bag.etl.mail.handler.TestMessageHandler.java

@Override
public void handle(Message message)
        throws FileNotFoundException, IOException, MessagingException, JAXBException {
    logger.info(message.getSubject());
}

From source file:org.nuxeo.ecm.platform.mail.action.StoreMessageAction.java

@SuppressWarnings("unchecked")
public boolean execute(ExecutionContext context) throws MessagingException {
    PathSegmentService pss = Framework.getService(PathSegmentService.class);
    Message message = context.getMessage();
    String title = message.getSubject();
    if (log.isDebugEnabled()) {
        log.debug("Storing message: " + message.getSubject());
    }/*from   w  w w  . ja  v a 2  s  .  c  o m*/
    Thread.currentThread().setContextClassLoader(Framework.class.getClassLoader());
    try (CoreSession session = CoreInstance.openCoreSessionSystem(null)) {
        DocumentModel doc = session.createDocumentModel(getMailDocumentType());
        doc.setProperty("dublincore", "title", title + System.currentTimeMillis());
        doc.setPathInfo(parentPath, pss.generatePathSegment(doc));
        doc.setProperty("dublincore", "title", title);
        doc = session.createDocument(doc);
        Map<String, Map<String, Object>> schemas = (Map<String, Map<String, Object>>) context
                .get("transformed");
        for (Map.Entry<String, Map<String, Object>> entry : schemas.entrySet()) {
            doc.setProperties(entry.getKey(), entry.getValue());
        }
        doc = session.saveDocument(doc);
        ACL acl = (ACL) context.get("acl");
        if (acl != null) {
            ACP acp = doc.getACP();
            acp.addACL(acl);
            doc.setACP(acp, true);
        }
        session.save();
        context.put("document", doc);
    }
    return true;
}

From source file:org.jahia.modules.gateway.decoders.NewsMailDecoderImpl.java

public String decode(Pattern matchingPattern, MailContent parsedMailContent, Message originalMessage)
        throws Exception {
    String title = originalMessage.getSubject();
    String nodepath = retrieveToken(parsedMailContent, matchingPattern, 2);

    nodepath = StringUtils.isNotBlank(nodepath) && paths.containsKey(nodepath) ? paths.get(nodepath)
            : "/sites/systemsite/contents/news";

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("nodetype", "jnt:news");
    jsonObject.put("name", StringUtils.defaultIfBlank(title, "Unknown"));
    jsonObject.put("locale", "en");
    jsonObject.put("workspace", Constants.EDIT_WORKSPACE);
    jsonObject.put("path", nodepath);
    Map<String, String> properties = new LinkedHashMap<String, String>();
    properties.put("desc", parsedMailContent.getBody());
    properties.put("jcr:title", StringUtils.defaultIfBlank(title, "Unknown"));
    properties.put("date", DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(System.currentTimeMillis()));
    if (!parsedMailContent.getFiles().isEmpty()) {
        properties.put("image", parsedMailContent.getFiles().get(0).getFile().getAbsolutePath());
    }//from   w  w  w . ja v a2s  . c o m
    jsonObject.put("properties", properties);

    JahiaUser sender = getSender(originalMessage);
    if (sender != null) {
        properties.put("username", sender.getName());
    }

    return jsonObject.toString();
}

From source file:com.yfiton.notifiers.email.EmailNotifierTest.java

private boolean checkEmailReception(String subject, String host, String username, String password)
        throws MessagingException {
    Properties properties = new Properties();
    properties.put("mail.store.protocol", "imaps");
    Session session = Session.getInstance(properties);

    Store store = null;/*from  w w  w  . ja v a2s .  c  o m*/

    try {
        store = session.getStore("imaps");
        store.connect(host, username, password);

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

        Message[] messages = inbox.getMessages();

        for (Message message : messages) {
            if (message.getSubject().equals(subject)) {
                message.setFlag(Flags.Flag.DELETED, true);
                return true;
            }
        }

        inbox.close(true);
    } finally {
        try {
            if (store != null) {
                store.close();
            }
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    return false;
}

From source file:org.nuxeo.ecm.platform.mail.action.SendMailAction.java

public boolean execute(ExecutionContext context) throws MessagingException {
    Message message = context.getMessage();
    if (log.isDebugEnabled()) {
        log.debug("Sending mail because of message: " + message.getSubject());
    }/*from w  w  w .j av  a2  s  .  co m*/
    Message sentMessage = new MimeMessage(session);
    if (message.getReplyTo() == null || message.getReplyTo().length == 0) {
        return true;
    }
    Address address = message.getReplyTo()[0];
    sentMessage.setRecipient(Message.RecipientType.TO, address);
    message.setText(textMessage);
    Transport.send(sentMessage);
    return true;
}