List of usage examples for javax.mail Message setFlag
public void setFlag(Flags.Flag flag, boolean set) throws MessagingException
From source file:com.liferay.mail.imap.IMAPAccessor.java
public void updateFlags(long folderId, long[] messageIds, int flag, boolean value, boolean deleteMissingMessages) throws PortalException { Folder jxFolder = null;/*from w w w . ja va 2 s . co m*/ try { jxFolder = openFolder(folderId); List<Message> jxMessages = getMessages(jxFolder, messageIds, deleteMissingMessages); for (Message jxMessage : jxMessages) { if (flag == MailConstants.FLAG_FLAGGED) { jxMessage.setFlag(Flags.Flag.FLAGGED, value); } else if (flag == MailConstants.FLAG_SEEN) { jxMessage.setFlag(Flags.Flag.SEEN, value); } else { throw new MailException(MailException.MESSAGE_INVALID_FLAG); } } } catch (MessagingException me) { throw new MailException(me); } finally { closeFolder(jxFolder, true); } }
From source file:com.liferay.mail.imap.IMAPAccessor.java
public void moveMessages(long sourceFolderId, long destinationFolderId, long[] messageIds, boolean deleteMissingMessages) throws PortalException { Folder sourceJxFolder = null;/* ww w . j a v a 2 s . com*/ Folder destinationJxFolder = null; try { sourceJxFolder = openFolder(sourceFolderId); sourceJxFolder.addMessageCountListener(new IMAPMessageCountListener(_user, _account, _password)); destinationJxFolder = openFolder(destinationFolderId); destinationJxFolder.addMessageCountListener(new IMAPMessageCountListener(_user, _account, _password)); List<Message> jxMessages = getMessages(sourceJxFolder, messageIds, deleteMissingMessages); for (Message jxMessage : jxMessages) { destinationJxFolder.appendMessages(new Message[] { jxMessage }); jxMessage.setFlag(Flags.Flag.DELETED, true); } } catch (MessagingException me) { throw new MailException(me); } finally { closeFolder(sourceJxFolder, true); closeFolder(destinationJxFolder, false); } }
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;/* www. j av a 2 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:com.sonicle.webtop.mail.MailAccount.java
public void deleteByHeaderValue(String header, String value) throws MessagingException { FolderCache fc = getFolderCache(getFolderDrafts()); fc.open();/*from w ww .j a va 2s . c o m*/ Folder folder = fc.getFolder(); Message[] oldmsgs = folder.search(new HeaderTerm(header, value)); if (oldmsgs != null && oldmsgs.length > 0) { for (Message m : oldmsgs) m.setFlag(Flags.Flag.DELETED, true); folder.expunge(); } }
From source file:org.apache.hupa.server.handler.AbstractSendMessageHandler.java
/** * Save the message in the sent folder/*from w w w.j a v a2s . co m*/ * * @param user * @param message * @throws MessagingException * @throws IOException */ protected void saveSentMessage(User user, Message message) throws MessagingException, IOException { IMAPStore iStore = cache.get(user); IMAPFolder folder = (IMAPFolder) iStore.getFolder(user.getSettings().getSentFolderName()); if (folder.exists() || folder.create(IMAPFolder.READ_WRITE)) { if (folder.isOpen() == false) { folder.open(Folder.READ_WRITE); } // It is necessary to copy the message, before putting it // in the sent folder. If not, it is not guaranteed that it is // stored in ascii and is not possible to get the attachments // size. message.saveChanges() doesn't fix the problem. // There are tests which demonstrate this. message = new MimeMessage((MimeMessage) message); message.setFlag(Flag.SEEN, true); folder.appendMessages(new Message[] { message }); try { folder.close(false); } catch (MessagingException e) { // we don't care on close } } }
From source file:org.apache.hupa.server.service.SendMessageBaseServiceImpl.java
/** * Save the message in the sent folder// w ww. j a va 2s.co m * * @param user * @param message * @throws MessagingException * @throws IOException */ protected void saveSentMessage(User user, Message message) throws MessagingException, IOException { IMAPStore iStore = cache.get(user); IMAPFolder folder = (IMAPFolder) iStore.getFolder(user.getSettings().getSentFolderName()); if (folder.exists() || folder.create(IMAPFolder.READ_WRITE)) { if (folder.isOpen() == false) { folder.open(Folder.READ_WRITE); } // It is necessary to copy the message, before putting it // in the sent folder. If not, it is not guaranteed that it is // stored in ascii and is not possible to get the attachments // size. message.saveChanges() doesn't fix the problem. // There are tests which demonstrate this. message = new MimeMessage((MimeMessage) message); message.setFlag(Flag.SEEN, true); folder.appendMessages(new Message[] { message }); try { folder.close(false); } catch (MessagingException e) { // we don't care on close } } }
From source file:org.springframework.integration.mail.AbstractMailReceiver.java
private void setMessageFlags(Message[] filteredMessages) throws MessagingException { boolean recentFlagSupported = false; Flags flags = this.getFolder().getPermanentFlags(); if (flags != null) { recentFlagSupported = flags.contains(Flags.Flag.RECENT); }/*from w w w .ja va 2 s . c om*/ for (Message message : filteredMessages) { if (!recentFlagSupported) { if (flags != null && flags.contains(Flags.Flag.USER)) { if (logger.isDebugEnabled()) { logger.debug("USER flags are supported by this mail server. Flagging message with '" + SI_USER_FLAG + "' user flag"); } Flags siFlags = new Flags(); siFlags.add(SI_USER_FLAG); message.setFlags(siFlags, true); } else { if (logger.isDebugEnabled()) { logger.debug( "USER flags are not supported by this mail server. Flagging message with system flag"); } message.setFlag(Flags.Flag.FLAGGED, true); } } this.setAdditionalFlags(message); } }
From source file:com.midori.confluence.plugin.mail2news.Mail2NewsJob.java
/** * Move a given message from one IMAP folder to another. It will be flagged as DELETED * in the originating folder and thus be deleted the next time EXPUNGE is called. * * @param m The message to be moved.//from ww w . j a v a 2 s. c om * @param from The folder from which the message has to be moved. * @param to The folder to where to move the message. */ private void moveMessage(Message m, Folder from, Folder to) { try { /* copy the message to the destination folder */ from.copyMessages(new Message[] { m }, to); /* delete the message from the originating folder */ /* this sets the DELETED flag, the message will be deleted * when expunging the folder */ m.setFlag(Flags.Flag.DELETED, true); } catch (Exception e) { this.log.error("Could not copy message: " + e.getMessage(), e); try { /* cannot move the message. mark it read so we will not look at it again */ m.setFlag(Flags.Flag.SEEN, true); } catch (MessagingException me) { /* could not set SEEN on the message */ this.log.error("Could not set SEEN on message.", me); } } }
From source file:com.ikon.util.MailUtils.java
/** * Import messages/*w w w . ja v a2 s . c o m*/ * http://www.jguru.com/faq/view.jsp?EID=26898 * * == Using Unique Identifier (UIDL) == * Mail server assigns an unique identifier for every email in the same account. You can get as UIDL * for every email by MailInfo.UIDL property. To avoid receiving the same email twice, the best way is * storing the UIDL of email retrieved to a text file or database. Next time before you retrieve email, * compare your local uidl list with remote uidl. If this uidl exists in your local uidl list, don't * receive it; otherwise receive it. * * == Different property of UIDL in POP3 and IMAP4 == * UIDL is always unique in IMAP4 and it is always an incremental integer. UIDL in POP3 can be any valid * asc-ii characters, and an UIDL may be reused by POP3 server if email with this UIDL has been deleted * from the server. Hence you are advised to remove the uidl from your local uidl list if that uidl is * no longer exist on the POP3 server. * * == Remarks == * You should create different local uidl list for different email account, because the uidl is only * unique for the same account. */ public static String importMessages(String token, MailAccount ma) throws PathNotFoundException, ItemExistsException, VirusDetectedException, AccessDeniedException, RepositoryException, DatabaseException, UserQuotaExceededException, ExtensionException, AutomationException { log.debug("importMessages({}, {})", new Object[] { token, ma }); Session session = Session.getDefaultInstance(getProperties()); String exceptionMessage = null; try { // Open connection Store store = session.getStore(ma.getMailProtocol()); store.connect(ma.getMailHost(), ma.getMailUser(), ma.getMailPassword()); Folder folder = store.getFolder(ma.getMailFolder()); folder.open(Folder.READ_WRITE); Message messages[] = null; if (folder instanceof IMAPFolder) { // IMAP folder UIDs begins at 1 and are supposed to be sequential. // Each folder has its own UIDs sequence, not is a global one. messages = ((IMAPFolder) folder).getMessagesByUID(ma.getMailLastUid() + 1, UIDFolder.LASTUID); } else { messages = folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false)); } for (int i = 0; i < messages.length; i++) { Message msg = messages[i]; log.info(i + ": " + msg.getFrom()[0] + " " + msg.getSubject() + " " + msg.getContentType()); log.info("Received: " + msg.getReceivedDate()); log.info("Sent: " + msg.getSentDate()); log.debug("{} -> {} - {}", new Object[] { i, msg.getSubject(), msg.getReceivedDate() }); com.ikon.bean.Mail mail = messageToMail(msg); if (ma.getMailFilters().isEmpty()) { log.debug("Import in compatibility mode"); String mailPath = getUserMailPath(ma.getUser()); importMail(token, mailPath, true, folder, msg, ma, mail); } else { for (MailFilter mf : ma.getMailFilters()) { log.debug("MailFilter: {}", mf); if (checkRules(mail, mf.getFilterRules())) { String mailPath = mf.getPath(); importMail(token, mailPath, mf.isGrouping(), folder, msg, ma, mail); } } } // Set message as seen if (ma.isMailMarkSeen()) { msg.setFlag(Flags.Flag.SEEN, true); } else { msg.setFlag(Flags.Flag.SEEN, false); } // Delete read mail if requested if (ma.isMailMarkDeleted()) { msg.setFlag(Flags.Flag.DELETED, true); } // Set lastUid if (folder instanceof IMAPFolder) { long msgUid = ((IMAPFolder) folder).getUID(msg); log.info("Message UID: {}", msgUid); ma.setMailLastUid(msgUid); MailAccountDAO.update(ma); } } // Close connection log.debug("Expunge: {}", ma.isMailMarkDeleted()); folder.close(ma.isMailMarkDeleted()); store.close(); } catch (NoSuchProviderException e) { log.error(e.getMessage(), e); exceptionMessage = e.getMessage(); } catch (MessagingException e) { log.error(e.getMessage(), e); exceptionMessage = e.getMessage(); } catch (IOException e) { log.error(e.getMessage(), e); exceptionMessage = e.getMessage(); } log.debug("importMessages: {}", exceptionMessage); return exceptionMessage; }
From source file:com.openkm.util.MailUtils.java
/** * Import messages//w w w . ja va 2 s .c om * http://www.jguru.com/faq/view.jsp?EID=26898 * * == Using Unique Identifier (UIDL) == * Mail server assigns an unique identifier for every email in the same account. You can get as UIDL * for every email by MailInfo.UIDL property. To avoid receiving the same email twice, the best way is * storing the UIDL of email retrieved to a text file or database. Next time before you retrieve email, * compare your local uidl list with remote uidl. If this uidl exists in your local uidl list, don't * receive it; otherwise receive it. * * == Different property of UIDL in POP3 and IMAP4 == * UIDL is always unique in IMAP4 and it is always an incremental integer. UIDL in POP3 can be any valid * asc-ii characters, and an UIDL may be reused by POP3 server if email with this UIDL has been deleted * from the server. Hence you are advised to remove the uidl from your local uidl list if that uidl is * no longer exist on the POP3 server. * * == Remarks == * You should create different local uidl list for different email account, because the uidl is only * unique for the same account. */ public static String importMessages(String token, MailAccount ma) throws PathNotFoundException, ItemExistsException, VirusDetectedException, AccessDeniedException, RepositoryException, DatabaseException, UserQuotaExceededException, ExtensionException, AutomationException { log.debug("importMessages({}, {})", new Object[] { token, ma }); Session session = Session.getDefaultInstance(getProperties()); String exceptionMessage = null; try { // Open connection Store store = session.getStore(ma.getMailProtocol()); store.connect(ma.getMailHost(), ma.getMailUser(), ma.getMailPassword()); Folder folder = store.getFolder(ma.getMailFolder()); folder.open(Folder.READ_WRITE); Message messages[] = null; if (folder instanceof IMAPFolder) { // IMAP folder UIDs begins at 1 and are supposed to be sequential. // Each folder has its own UIDs sequence, not is a global one. messages = ((IMAPFolder) folder).getMessagesByUID(ma.getMailLastUid() + 1, UIDFolder.LASTUID); } else { messages = folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false)); } for (int i = 0; i < messages.length; i++) { Message msg = messages[i]; log.info("======= ======= {} ======= =======", i); log.info("Subject: {}", msg.getSubject()); log.info("From: {}", msg.getFrom()); log.info("Received: {}", msg.getReceivedDate()); log.info("Sent: {}", msg.getSentDate()); com.openkm.bean.Mail mail = messageToMail(msg); if (ma.getMailFilters().isEmpty()) { log.debug("Import in compatibility mode"); String mailPath = getUserMailPath(ma.getUser()); importMail(token, mailPath, true, folder, msg, ma, mail); } else { for (MailFilter mf : ma.getMailFilters()) { log.debug("MailFilter: {}", mf); if (checkRules(mail, mf.getFilterRules())) { String mailPath = mf.getPath(); importMail(token, mailPath, mf.isGrouping(), folder, msg, ma, mail); } } } // Set message as seen if (ma.isMailMarkSeen()) { msg.setFlag(Flags.Flag.SEEN, true); } else { msg.setFlag(Flags.Flag.SEEN, false); } // Delete read mail if requested if (ma.isMailMarkDeleted()) { msg.setFlag(Flags.Flag.DELETED, true); } // Set lastUid if (folder instanceof IMAPFolder) { long msgUid = ((IMAPFolder) folder).getUID(msg); log.info("Message UID: {}", msgUid); ma.setMailLastUid(msgUid); MailAccountDAO.update(ma); } } // Close connection log.debug("Expunge: {}", ma.isMailMarkDeleted()); folder.close(ma.isMailMarkDeleted()); store.close(); } catch (NoSuchProviderException e) { log.error(e.getMessage(), e); exceptionMessage = e.getMessage(); } catch (MessagingException e) { log.error(e.getMessage(), e); exceptionMessage = e.getMessage(); } catch (IOException e) { log.error(e.getMessage(), e); exceptionMessage = e.getMessage(); } log.debug("importMessages: {}", exceptionMessage); return exceptionMessage; }