/** * Asserts that a connection to the given mailbox is possible. * * @param protocol * The protocol. * @param isStartTls * True, if StartTLS ist possible. * @param host * The host. * @param port * The port. * @param login * The login. * @param password * The password. * @param mailbox * The mailbox. * @throws ConnectionException * Exception. * @throws MessagingException * Exception. */ private void assertConnection(String protocol, boolean isStartTls, String host, String port, String login, String password, String mailbox) throws ConnectionException, MessagingException { String propertiesProtocolPrefix = "mail." + protocol + "."; Properties properties = new Properties(); properties.putAll(System.getProperties()); properties.put(propertiesProtocolPrefix + "starttls.enable", isStartTls); Session session = Session.getInstance(properties); Store store = session.getStore(protocol); try { store.connect(host, NumberUtils.toInt(port, -1), login, password); Folder folder = store.getFolder(mailbox); if (!folder.exists()) { throw new FolderNotFoundException(); } } finally { store.close(); } }
private void ensureIsConnected() throws MessagingException { MailConfiguration config = getEndpoint().getConfiguration(); boolean connected = false; try { if (store != null && store.isConnected()) { connected = true; } } catch (Exception e) { LOG.debug("Exception while testing for is connected to MailStore: " + getEndpoint().getConfiguration().getMailStoreLogInformation() + ". Caused by: " + e.getMessage(), e); } if (!connected) { // ensure resources get recreated on reconnection store = null; folder = null; if (LOG.isDebugEnabled()) { LOG.debug("Connecting to MailStore: {}", getEndpoint().getConfiguration().getMailStoreLogInformation()); } store = sender.getSession().getStore(config.getProtocol()); store.connect(config.getHost(), config.getPort(), config.getUsername(), config.getPassword()); serverCanSort = hasSortCapability(store); } if (folder == null) { if (LOG.isDebugEnabled()) { LOG.debug("Getting folder {}", config.getFolderName()); } folder = store.getFolder(config.getFolderName()); if (folder == null || !folder.exists()) { throw new FolderNotFoundException(folder, "Folder not found or invalid: " + config.getFolderName()); } } }
@Override public void open(int mode) throws MessagingException { this.mode = mode; try { if (!exists()) { throw new FolderNotFoundException(); } ItemView view = new ItemView(ITEM_VIEW_MAX_ITEMS); folder = Folder.bind(getService(), folder.getId()); if (prefetchItems) { FindItemsResults<Item> lFindResults = getService().findItems(folder.getId(), view); messages = new ArrayList<>(lFindResults.getTotalCount()); unreadMessages = new ArrayList<>(); for (Item aItem : lFindResults) { if (aItem instanceof EmailMessage) { logger.info("Fetching content of item {}", aItem.getId()); EmailMessage aEmailMessage = (EmailMessage) aItem; EwsMailConverter aConverter = new EwsMailConverter(this, aEmailMessage, messages.size() + 1); messages.add(aConverter.convert()); } else { logger.warn("Skipping item {} as it is a {}", aItem.getId(), aItem.getClass()); } } } else { } timestamp = new Date(); getStore().notifyConnectionListeners(ConnectionEvent.OPENED); } catch (Exception e) { throw new MessagingException(e.getMessage(), e); } }
@Override public synchronized void copyMessages(final Message[] msgs, final Folder folder) throws MessagingException { abortIdle(); checkOpened(); checkExists(); if (msgs == null || folder == null || msgs.length == 0) { return; } if (!folder.exists()) { throw new FolderNotFoundException(folder.getFullName() + " does not exist", folder); } folder.appendMessages(msgs); }
@Override protected void checkExists() throws MessagingException { if (!exists()) { throw new FolderNotFoundException(this, getFullName() + " not found"); } }