Example usage for javax.mail Message isSet

List of usage examples for javax.mail Message isSet

Introduction

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

Prototype

public boolean isSet(Flags.Flag flag) throws MessagingException 

Source Link

Document

Check whether the flag specified in the flag argument is set in this message.

Usage

From source file:org.gcaldaemon.core.GmailEntry.java

public final GmailMessage[] receive(String title) throws Exception {

    // Open 'INBOX' folder
    Folder inbox = mailbox.getFolder("INBOX");
    inbox.open(Folder.READ_WRITE);/*from  ww  w  .j  a v  a 2 s  .c  o  m*/
    Message[] messages = inbox.getMessages();
    if (messages == null || messages.length == 0) {
        return new GmailMessage[0];
    }

    // Loop on messages
    LinkedList list = new LinkedList();
    for (int i = 0; i < messages.length; i++) {
        Message msg = messages[i];
        if (!msg.isSet(Flag.SEEN)) {
            String subject = msg.getSubject();
            if (title == null || title.length() == 0 || title.equals(subject)) {
                GmailMessage gm = new GmailMessage();
                Address[] from = msg.getFrom();
                msg.setFlag(Flag.SEEN, true);
                if (from == null || from.length == 0) {
                    continue;
                }
                gm.subject = subject;
                gm.from = from[0].toString();
                gm.memo = String.valueOf(msg.getContent());
                list.addLast(gm);
            }
        }
    }
    inbox.close(true);

    // Return the array of the messages
    GmailMessage[] array = new GmailMessage[list.size()];
    list.toArray(array);
    return array;
}

From source file:com.googlecode.gmail4j.javamail.ImapGmailClient.java

/**
 * Mark a given {@link GmailMessage} as unread.
 *
 * @param messageNumber the message number ex:{@code gmailMessage.getMessageNumber()}
 * @throws GmailException if unable to mark {@link GmailMessage} as unread
 *///from  ww w  .  j  av  a  2 s  .  c o m
public void markAsUnread(int messageNumber) {
    if (messageNumber <= 0) {
        throw new GmailException("ImapGmailClient invalid " + "GmailMessage number");
    }
    Folder folder = null;

    try {
        final Store store = openGmailStore();
        folder = getFolder(this.srcFolder, store);
        folder.open(Folder.READ_WRITE);
        Message message = folder.getMessage(messageNumber);
        if (message.isSet(Flags.Flag.SEEN)) {
            message.setFlag(Flags.Flag.SEEN, false);
        }
    } catch (Exception e) {
        throw new GmailException(
                "ImapGmailClient failed marking" + " GmailMessage as unread : " + messageNumber, e);
    } finally {
        closeFolder(folder);
    }
}

From source file:com.googlecode.gmail4j.javamail.ImapGmailClient.java

/**
 * Removes Star Flag of a given Starred {@link GmailMessage}.
 *
 * @param messageNumber the message number ex:{@code gmailMessage.getMessageNumber()}
 * @throws GmailException if unable to remove star flag from {@link GmailMessage}
 *//*from w  w w .  j ava 2s  . co  m*/
public void removeStar(int messageNumber) {
    if (messageNumber <= 0) {
        throw new GmailException("ImapGmailClient invalid " + "GmailMessage number");
    }
    Folder folder = null;

    try {
        final Store store = openGmailStore();
        folder = getFolder(ImapGmailLabel.STARRED.getName(), store);
        folder.open(Folder.READ_WRITE);
        Message message = folder.getMessage(messageNumber);
        if (message.isSet(Flags.Flag.FLAGGED)) {
            message.setFlag(Flags.Flag.FLAGGED, false);
        }
    } catch (Exception e) {
        throw new GmailException(
                "ImapGmailClient failed removing" + " GmailMessage star flag : " + messageNumber, e);
    } finally {
        closeFolder(folder);
    }
}

From source file:com.googlecode.gmail4j.javamail.ImapGmailClient.java

/**
 * Mark a given {@link GmailMessage} as read.
 *
 * @param messageNumber the message number ex:{@code gmailMessage.getMessageNumber()}
 * @throws GmailException if unable to mark {@link GmailMessage} as read
 *//*from  ww w  . j a v a  2 s  .com*/
public void markAsRead(int messageNumber) {
    if (messageNumber <= 0) {
        throw new GmailException("ImapGmailClient invalid " + "GmailMessage number");
    }
    Folder folder = null;

    try {
        final Store store = openGmailStore();
        folder = getFolder(this.srcFolder, store);
        folder.open(Folder.READ_WRITE);
        Message message = folder.getMessage(messageNumber);
        if (!message.isSet(Flags.Flag.SEEN)) {
            message.setFlag(Flags.Flag.SEEN, true);
        }
    } catch (Exception e) {
        throw new GmailException("ImapGmailClient failed marking" + " GmailMessage as read : " + messageNumber,
                e);
    } finally {
        closeFolder(folder);
    }
}

From source file:com.googlecode.gmail4j.javamail.ImapGmailClient.java

/**
 * Flag a given {@link GmailMessage} as Starred.
 *
 * @param messageNumber the message number ex:{@code gmailMessage.getMessageNumber()}
 * @throws GmailException if unable to flag {@link GmailMessage} as starred
 *//*from w  w  w  .  j  av a 2s  .  c  o  m*/
public void addStar(int messageNumber) {
    if (messageNumber <= 0) {
        throw new GmailException("ImapGmailClient invalid " + "GmailMessage number");
    }
    Folder folder = null;

    try {
        final Store store = openGmailStore();
        folder = getFolder(this.srcFolder, store);
        folder.open(Folder.READ_WRITE);
        Message message = folder.getMessage(messageNumber);
        if (!message.isSet(Flags.Flag.FLAGGED)) {
            message.setFlag(Flags.Flag.FLAGGED, true);
        }
    } catch (Exception e) {
        throw new GmailException(
                "ImapGmailClient failed flagging" + " GmailMessage as starred : " + messageNumber, e);
    } finally {
        closeFolder(folder);
    }
}

From source file:org.openadaptor.auxil.connector.mail.MailReadConnector.java

/**
 * @return true if the supplied message is to be processed - ie. if it has
 * not been expunged, marked for deletion or is null. Also takes into account
 * the [ProcessReadMessages] property which, if set to false, means that read
 * messages should not be processed.//from ww  w  .j  a  v  a 2s  .c  om
 *
 * @throws MessagingException if there was a comms error
 */
protected boolean isToBeProcessed(Message msg) throws MessagingException {
    // Note: isExpunged() is the only allowable call on an expunged message
    // or we get an exception so we need to check this first before checking
    // if it has been read.
    if (msg == null || msg.isExpunged() || msg.isSet(Flags.Flag.DELETED))
        return false;

    if (!processReadMessages && msg.isSet(Flags.Flag.SEEN))
        return false;

    return true;
}

From source file:com.liferay.mail.imap.IMAPAccessor.java

protected String getFlags(Message jxMessage) throws MessagingException {
    StringBundler sb = new StringBundler();

    if (jxMessage.isSet(Flags.Flag.FLAGGED)) {
        sb.append(MailConstants.FLAG_FLAGGED);
        sb.append(StringPool.COMMA);/*from  w w  w.j  a va 2 s  .  c  o m*/
    }

    if (jxMessage.isSet(Flags.Flag.SEEN)) {
        sb.append(MailConstants.FLAG_SEEN);
        sb.append(StringPool.COMMA);
    }

    return sb.toString();
}

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

@Override
public EmailMessage getMessage(MailStoreConfiguration config, String messageId) {
    Authenticator auth = credentialsProvider.getAuthenticator();
    Folder inbox = null;/*  w  w  w.j  av  a2 s.co  m*/
    try {
        int mode = config.getMarkMessagesAsRead() ? Folder.READ_WRITE : Folder.READ_ONLY;

        // Retrieve user's inbox
        Session session = openMailSession(config, auth);
        inbox = getUserInbox(session, config.getInboxFolderName());
        inbox.open(mode);

        Message message;
        if (inbox instanceof UIDFolder) {
            message = ((UIDFolder) inbox).getMessageByUID(Long.parseLong(messageId));
        } else {
            message = inbox.getMessage(Integer.parseInt(messageId));
        }
        boolean unread = !message.isSet(Flags.Flag.SEEN);
        if (config.getMarkMessagesAsRead()) {
            message.setFlag(Flag.SEEN, true);
        }
        EmailMessage emailMessage = wrapMessage(message, true, session);
        if (!config.getMarkMessagesAsRead()) {
            // NOTE:  This is more than a little bit annoying.  Apparently
            // the mere act of accessing the body content of a message in
            // Javamail flags the in-memory representation of that message
            // as SEEN.  It does *nothing* to the mail server (the message
            // is still unread in the SOR), but it wreaks havoc on local
            // functions that key off that value and expect it to be
            // accurate.  We're obligated, therefore, to restore the value
            // to what it was before the call to wrapMessage().
            emailMessage.setUnread(unread);
        }

        return emailMessage;
    } catch (MessagingException e) {
        log.error("Messaging exception while retrieving individual message", e);
    } catch (IOException e) {
        log.error("IO exception while retrieving individual message", e);
    } catch (ScanException e) {
        log.error("AntiSamy scanning exception while retrieving individual message", e);
    } catch (PolicyException e) {
        log.error("AntiSamy policy exception while retrieving individual message", e);
    } finally {
        if (inbox != null) {
            try {
                inbox.close(false);
            } catch (Exception e) {
                log.warn("Can't close correctly javamail inbox connection");
            }
            try {
                inbox.getStore().close();
            } catch (Exception e) {
                log.warn("Can't close correctly javamail store connection");
            }
        }
    }

    return null;
}

From source file:org.pentaho.di.job.entries.getpop.MailConnection.java

public boolean isMessageNew(Message msg) {
    try {//  w ww  .j  ava  2 s.  co m
        return msg.isSet(Flag.RECENT);
    } catch (MessagingException e) {
        return false;
    }
}

From source file:org.pentaho.di.job.entries.getpop.MailConnection.java

public boolean isMessageRead(Message msg) {
    try {//from   w  w w . j  a va2  s.c  o m
        return msg.isSet(Flag.SEEN);
    } catch (MessagingException e) {
        return false;
    }
}