Example usage for javax.mail Message setFlag

List of usage examples for javax.mail Message setFlag

Introduction

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

Prototype

public void setFlag(Flags.Flag flag, boolean set) throws MessagingException 

Source Link

Document

Set the specified flag on this message to the specified value.

Usage

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

/**
 * Moves given {@link GmailMessage}'s to {@link ImapGmailLabel.TRASH} folder.
 *
 * @param gmailMessages {@link GmailMessage} message(s)
 * @throws GmailException if unable to move {@link GmailMessage}'s to
 * the Trash Folder/*w  ww  .  j av a  2  s. c om*/
 */
public void moveToTrash(final GmailMessage[] gmailMessages) {
    if (gmailMessages == null || gmailMessages.length <= 0) {
        LOG.warn("ImapGmailClient requires GmailMessage(s) to move" + " to move messages to trash folder");
        return;
    }
    Folder folder = null;

    try {
        final Store store = openGmailStore();
        folder = getFolder(this.srcFolder, store);
        if (!folder.isOpen()) {
            folder.open(Folder.READ_WRITE);
        }

        List<Message> markedMsgList = new ArrayList<Message>();
        for (GmailMessage gmailMessage : gmailMessages) {
            // get only messages that match to the specified message number
            Message message = folder.getMessage(gmailMessage.getMessageNumber());
            message.setFlag(Flags.Flag.SEEN, true);
            // mark message as delete
            message.setFlag(Flags.Flag.DELETED, true);
            markedMsgList.add(message);
        }

        Folder trash = getFolder(ImapGmailLabel.TRASH.getName(), store);
        if (folder.getURLName().equals(trash.getURLName())) {
            LOG.warn("ImapGmailClient trying to move GmailMessage(s) within"
                    + " same folder(ImapGmailLabel.TRASH.getName())");
        }
        // move the marked messages to trash folder
        if (!markedMsgList.isEmpty()) {
            folder.copyMessages(markedMsgList.toArray(new Message[0]), trash);
        }
    } catch (Exception e) {
        throw new GmailException("ImapGmailClient failed moving GmailMessage(s)" + " to trash folder: " + e);
    } finally {
        closeFolder(folder);
    }
}

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
 *//* www  . 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

/**
 * 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  va  2s  . c o m
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

/**
 * 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  a  va  2 s.  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

/**
 * 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 .  java2 s .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.apache.synapse.transport.mail.MailEchoRawXMLTest.java

private Object getMessage(String requestMsgId) {
    Session session = Session.getInstance(props, null);
    session.setDebug(log.isTraceEnabled());
    Store store = null;/*from   w  w  w  .  j  av  a 2 s. co m*/

    try {
        store = session.getStore("pop3");
        store.connect(username, password);
        Folder folder = store.getFolder(MailConstants.DEFAULT_FOLDER);
        folder.open(Folder.READ_WRITE);
        Message[] msgs = folder.getMessages();
        log.debug(msgs.length + " replies in reply mailbox");
        for (Message m : msgs) {
            String[] inReplyTo = m.getHeader(MailConstants.MAIL_HEADER_IN_REPLY_TO);
            log.debug("Got reply to : " + Arrays.toString(inReplyTo));
            if (inReplyTo != null && inReplyTo.length > 0) {
                for (int j = 0; j < inReplyTo.length; j++) {
                    if (requestMsgId.equals(inReplyTo[j])) {
                        m.setFlag(Flags.Flag.DELETED, true);
                        return m.getContent();
                    }
                }
            }
            m.setFlag(Flags.Flag.DELETED, true);
        }
        folder.close(true);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (store != null) {
            try {
                store.close();
            } catch (MessagingException ignore) {
            }
            store = null;
        }
    }
    return null;
}

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 .  com
    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:ru.codemine.ccms.mail.EmailAttachmentExtractor.java

public void saveAllAttachment() {
    String protocol = ssl ? "imaps" : "imap";

    Properties props = new Properties();
    props.put("mail.store.protocol", protocol);

    try {//from  ww  w .  j  av  a2  s.  c  om
        Session session = Session.getInstance(props);
        Store store = session.getStore();
        store.connect(host, port, username, password);

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

        for (Message msg : inbox.getMessages()) {
            if (!msg.isSet(Flags.Flag.SEEN)) {
                String fileNamePrefix = settingsService.getStorageEmailPath() + "msg-" + msg.getMessageNumber();
                Multipart multipart = (Multipart) msg.getContent();
                for (int i = 0; i < multipart.getCount(); i++) {
                    BodyPart bodyPart = multipart.getBodyPart(i);
                    if (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())
                            && StringUtils.isNotEmpty(bodyPart.getFileName())) {
                        MimeBodyPart mimimi = (MimeBodyPart) bodyPart; // =(^.^)= 
                        mimimi.saveFile(fileNamePrefix + MimeUtility.decodeText(bodyPart.getFileName()));
                        msg.setFlag(Flags.Flag.SEEN, true);
                    }
                }
            }
        }
    } catch (IOException | MessagingException e) {
        log.error("? ? ?,  : "
                + e.getMessage());
    }

}

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

@Override
public boolean execute(ExecutionContext context) throws Exception {
    try {/*w  w  w  .ja v  a2s  .  co  m*/
        Message message = context.getMessage();
        // erase marker: mail has been treated
        // VDU it could be nice to have a field in schema 'protocol' that says
        // messages stay in server or not. This only work with IMAP* protocols, as POP3*
        // protocols does not support flags other than DELETED.
        //            message.setFlag(Flag.FLAGGED, false);
        boolean leaveOnServer = (Boolean) context.getInitialContext().get(LEAVE_ON_SERVER_KEY);
        String protocolType = (String) context.getInitialContext().get(PROTOCOL_TYPE_KEY);
        //            log.debug(PROTOCOL_TYPE_KEY + ": " + protocolType);
        //            log.debug(LEAVE_ON_SERVER_KEY + ": " + leaveOnServer);
        if ((IMAP.equals(protocolType) || IMAPS.equals(protocolType)) && leaveOnServer) {
            message.setFlag(Flag.SEEN, true);
        } else {
            message.setFlag(Flag.DELETED, true);
        }
        return true;
    } catch (Exception e) {
        return false;
    }
}

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

public void deleteMessages(long folderId, long[] messageIds) throws PortalException {

    Folder jxFolder = null;//from  www  .  ja va  2  s.  c o m

    try {
        jxFolder = openFolder(folderId);

        jxFolder.addMessageCountListener(new IMAPMessageCountListener(_user, _account, _password));

        List<Message> jxMessages = getMessages(jxFolder, messageIds, true);

        for (Message jxMessage : jxMessages) {
            jxMessage.setFlag(Flags.Flag.DELETED, true);
        }
    } catch (MessagingException me) {
        throw new MailException(me);
    } finally {
        closeFolder(jxFolder, true);
    }
}