Example usage for javax.mail MessagingException MessagingException

List of usage examples for javax.mail MessagingException MessagingException

Introduction

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

Prototype

public MessagingException(String s, Exception e) 

Source Link

Document

Constructs a MessagingException with the specified Exception and detail message.

Usage

From source file:org.sourceforge.net.javamail4ews.store.EwsFolder.java

@Override
public EwsFolder getParent() throws MessagingException {
    try {// w  w w .  j a v a2  s.c  o  m
        if (!exists()) {
            throw new IllegalStateException("Folder does not exist!");
        }
        // Strange equals method in FolderId...
        if (folder.getId().getUniqueId().equals(INBOX.getId().getUniqueId())) {
            return null;
        } else {
            return new EwsFolder(getStore(), folder.getParentFolderId());
        }
    } catch (ServiceLocalException e) {
        throw new MessagingException(e.getMessage(), e);
    }
}

From source file:org.sourceforge.net.javamail4ews.store.EwsFolder.java

@Override
public boolean hasNewMessages() throws MessagingException {
    ItemView view = new ItemView(ITEM_VIEW_MAX_ITEMS);
    SearchFilter.SearchFilterCollection search = new SearchFilter.SearchFilterCollection();
    search.add(new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, timestamp));
    FindItemsResults<Item> lFindResults;
    try {/*from   www .  java2s. com*/
        lFindResults = getService().findItems(folder.getId(), search, view);

        if (lFindResults.getTotalCount() > 0) {
            return true;
        } else {
            return false;
        }
    } catch (Exception e) {
        throw new MessagingException(e.getMessage(), e);
    }
}

From source file:org.sourceforge.net.javamail4ews.store.EwsFolder.java

@Override
public EwsFolder[] list(String pattern) throws MessagingException {
    FolderView lFolderView = new FolderView(ITEM_VIEW_MAX_ITEMS);
    FindFoldersResults lFindFoldersResults;
    try {/*w  ww .  ja v  a2s. c  om*/
        SearchFilter.SearchFilterCollection lSearchFilter = new SearchFilter.SearchFilterCollection();
        if (!pattern.equals("%")) {
            // TODO incomplete implementation - wildcards are not implemented yet
            lSearchFilter.add(new SearchFilter.IsEqualTo(FolderSchema.DisplayName, pattern));
        }
        lFindFoldersResults = folder.findFolders(lSearchFilter, lFolderView);
        List<Folder> lFolders = lFindFoldersResults.getFolders();

        EwsFolder[] retValue = new EwsFolder[lFolders.size()];
        for (int i = 0; i < retValue.length; i++) {
            retValue[i] = new EwsFolder(getStore(), lFolders.get(i).getId());
        }

        return retValue;
    } catch (Exception e) {
        throw new MessagingException(e.getMessage(), e);
    }
}

From source file:org.sourceforge.net.javamail4ews.store.EwsFolder.java

@Override
public void open(int mode) throws MessagingException {
    this.mode = mode;
    try {//from ww  w.  j ava 2  s  .  c om
        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);
    }
}

From source file:org.sourceforge.net.javamail4ews.store.EwsFolder.java

@Override
public boolean renameTo(javax.mail.Folder f) throws MessagingException {
    if (isOpen()) {
        throw new IllegalStateException("Folder must be closed!");
    }/*w w w  .j  av  a 2  s . c o  m*/
    FolderId targetFolderId;
    if (f instanceof EwsFolder) {
        targetFolderId = ((EwsFolder) f).folder.getId();
    } else {
        targetFolderId = getFolder(f.getFullName()).folder.getId();
    }
    try {
        folder.move(targetFolderId);
        getStore().notifyFolderListeners(FolderEvent.RENAMED, this);
        return true;
    } catch (Exception e) {
        throw new MessagingException(e.getMessage(), e);
    }
}

From source file:org.sourceforge.net.javamail4ews.store.EwsFolder.java

public synchronized Message[] search(SearchTerm paramSearchTerm) throws MessagingException {
    if (paramSearchTerm instanceof FlagTerm) {
        FlagTerm flagTerm = (FlagTerm) paramSearchTerm;
        Flags flags = flagTerm.getFlags();
        if (flags.equals(new Flags(Flags.Flag.SEEN))) {
            try {
                return getUnreadMessage(0);
            } catch (Exception e) {
                throw new MessagingException(e.getMessage(), e);
            }//  w w w .  j  av  a2s .c  o  m
        }
    }
    throw new MessagingException("Ews folder don't support search with search term " + paramSearchTerm);
}

From source file:org.sourceforge.net.javamail4ews.store.EwsStore.java

@Override
public EwsFolder getFolder(String name) throws MessagingException {
    try {//  w  w  w. j a  v a  2  s  .  co  m
        return new EwsFolder(this, name, new FolderId(WellKnownFolderName.Inbox));
    } catch (Exception e) {
        throw new MessagingException(e.getMessage(), e);
    }
}

From source file:org.sourceforge.net.javamail4ews.transport.EwsTransport.java

public void sendMessage(Message pMessage, Address[] addresses, Address[] ccaddresses, Address[] bccaddresses)
        throws MessagingException {
    try {/* w w  w. j ava  2s .c o m*/
        EmailMessage msg = new EmailMessage(getService());
        createHeaders(msg, pMessage);

        createAddresses(msg, pMessage, addresses, ccaddresses, bccaddresses);
        createSubject(msg, pMessage);
        createBody(msg, pMessage);

        sendMessage(msg);

    } catch (MessagingException e) {
        throw e;
    } catch (Exception e) {
        String message = e.getMessage();
        if (message != null && message.contains(
                "The user account which was used to submit this request does not have the right to send mail"
                        + " on behalf of the specified sending account")) {
            SMTPSendFailedException ex = new SMTPSendFailedException("send", 551,
                    "Could not send : insufficient right to send on behalf of '" + pMessage.getFrom()[0] + "'",
                    e, null, pMessage.getAllRecipients(), null);
            // (
            // "Could not send : insufficient right to send on behalf of " + pMessage.getFrom()[0], e);
            throw ex;
        } else if (message != null)
            throw new MessagingException(message, e);
        else
            throw new MessagingException("no detailed message provided", e);
    }
}

From source file:org.sourceforge.net.javamail4ews.util.Util.java

public static ExchangeService getExchangeService(String host, int port, String user, String password,
        Session pSession) throws MessagingException {
    if (user == null) {
        return null;
    }/*from   w ww. jav  a  2 s .c  om*/
    if (password == null) {
        return null;
    }

    String version = getConfiguration(pSession).getString("org.sourceforge.net.javamail4ews.ExchangeVersion",
            "");
    ExchangeVersion serverVersion = null;
    if (!version.isEmpty()) {
        try {
            serverVersion = Enum.valueOf(ExchangeVersion.class, version);
        } catch (IllegalArgumentException e) {
            logger.info("Unknown version for exchange server: '" + version
                    + "' using default : no version specified");
        }
    }
    boolean enableTrace = getConfiguration(pSession)
            .getBoolean("org.sourceforge.net.javamail4ews.util.Util.EnableServiceTrace");
    ExchangeService service = null;
    if (serverVersion != null) {
        service = new ExchangeService(serverVersion);
    } else {
        service = new ExchangeService();
    }
    Integer connectionTimeout = getConnectionTimeout(pSession);
    Integer protocolTimeout = getProtocolTimeout(pSession);
    if (connectionTimeout != null) {
        logger.debug("setting timeout to {} using connection timeout value", connectionTimeout);
        service.setTimeout(connectionTimeout.intValue());
    }
    if (protocolTimeout != null) {
        logger.debug("setting protocol timeout to {} is ignored", protocolTimeout);
    }
    service.setTraceEnabled(enableTrace);

    ExchangeCredentials credentials = new WebCredentials(user, password);
    service.setCredentials(credentials);

    try {
        service.setUrl(new URI(host));
    } catch (URISyntaxException e) {
        throw new MessagingException(e.getMessage(), e);
    }

    try {
        //Bind to check if connection parameters are valid
        if (getConfiguration(pSession)
                .getBoolean("org.sourceforge.net.javamail4ews.util.Util.VerifyConnectionOnConnect")) {
            logger.debug("Connection settings : trying to verify them");
            Folder.bind(service, WellKnownFolderName.Inbox);
            logger.info("Connection settings verified.");
        } else {
            logger.info("Connection settings not verified yet.");
        }
        return service;
    } catch (Exception e) {
        Throwable cause = e.getCause();
        if (cause != null) {
            if (cause instanceof ConnectException) {
                Exception nested = (ConnectException) cause;
                throw new MessagingException(nested.getMessage(), nested);
            }
        }
        throw new AuthenticationFailedException(e.getMessage());
    }
}

From source file:org.xwiki.mail.internal.AttachmentMimeBodyPartFactory.java

private DataSource createTemporaryAttachmentDataSource(Attachment attachment) throws MessagingException {
    File temporaryAttachmentFile;
    FileOutputStream fos = null;/*from  w  ww.  j a va 2  s  .  c  o m*/
    try {
        temporaryAttachmentFile = File.createTempFile("attachment", ".tmp", this.temporaryDirectory);
        temporaryAttachmentFile.deleteOnExit();
        fos = new FileOutputStream(temporaryAttachmentFile);
        fos.write(attachment.getContent());
    } catch (Exception e) {
        throw new MessagingException(
                String.format("Failed to save attachment [%s] to the file system", attachment.getFilename()),
                e);
    } finally {
        try {
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e) {
            // Only an error at closing, we continue
            this.logger.warn("Failed to close the temporary file attachment when sending an email. "
                    + "Root reason: [{}]", ExceptionUtils.getRootCauseMessage(e));
        }
    }
    return new FileDataSource(temporaryAttachmentFile);
}