Example usage for javax.mail Session getDebugOut

List of usage examples for javax.mail Session getDebugOut

Introduction

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

Prototype

public synchronized PrintStream getDebugOut() 

Source Link

Document

Returns the stream to be used for debugging output.

Usage

From source file:MailHandlerDemo.java

/**
 * Sets up the demos that will run./*  w  w w. java 2s. co  m*/
 *
 * @param l the list of arguments.
 * @return true if debug is on.
 */
private static boolean init(List<String> l) {
    l = new ArrayList<String>(l);
    Session session = Session.getInstance(System.getProperties());
    boolean all = l.remove("-all") || l.isEmpty();
    if (l.remove("-body") || all) {
        initBodyOnly();
    }

    if (l.remove("-custom") || all) {
        initCustomAttachments();
    }

    if (l.remove("-low") || all) {
        initLowCapacity();
    }

    if (l.remove("-pushfilter") || all) {
        initWithPushFilter();
    }

    if (l.remove("-pushlevel") || all) {
        initWithPushLevel();
    }

    if (l.remove("-pushnormal") || all) {
        initPushNormal();
    }

    if (l.remove("-pushonly") || all) {
        initPushOnly();
    }

    if (l.remove("-simple") || all) {
        initSimpleAttachment();
    }

    boolean fallback = applyFallbackSettings();
    boolean debug = l.remove("-debug") || session.getDebug();
    if (debug) {
        checkConfig(CLASS_NAME, session.getDebugOut());
    }

    if (!l.isEmpty()) {
        LOGGER.log(Level.SEVERE, "Unknown commands: {0}", l);
    }

    if (fallback) {
        LOGGER.info("Check your user temp dir for output.");
    }
    return debug;
}

From source file:org.exjello.mail.Exchange2003Connection.java

public static Exchange2003Connection createConnection(String protocol, Session session, String host, int port,
        String username, String password) throws Exception {
    String prefix = "mail." + protocol.toLowerCase() + ".";
    boolean debugPassword = Boolean.parseBoolean(session.getProperty(DEBUG_PASSWORD_PROPERTY));
    String pwd = (password == null) ? null : debugPassword ? password : "<password>";
    if (host == null || username == null || password == null) {
        if (session.getDebug()) {
            session.getDebugOut().println("Missing parameter; host=\"" + host + "\",username=\"" + username
                    + "\",password=\"" + pwd + "\"");
        }// ww  w  .  j  a  v  a  2 s.c  o m
        throw new IllegalStateException("Host, username, and password must be specified.");
    }
    boolean unfiltered = Boolean.parseBoolean(session.getProperty(UNFILTERED_PROPERTY));
    /* Mirco */
    String filterLastCheck = session.getProperty(ExchangeConstants.FILTER_LAST_CHECK);
    String filterFrom = session.getProperty(ExchangeConstants.FILTER_FROM_PROPERTY);
    String filterNotFrom = session.getProperty(ExchangeConstants.FILTER_NOT_FROM_PROPERTY);
    String filterTo = session.getProperty(ExchangeConstants.FILTER_TO_PROPERTY);
    boolean delete = Boolean.parseBoolean(session.getProperty(DELETE_PROPERTY));
    boolean secure = Boolean.parseBoolean(session.getProperty(prefix + SSL_PROPERTY));
    int limit = -1;
    String limitString = session.getProperty(LIMIT_PROPERTY);
    if (limitString != null) {
        try {
            limit = Integer.parseInt(limitString);
        } catch (NumberFormatException ex) {
            throw new NumberFormatException("Invalid limit specified: " + limitString);
        }
    }
    try {
        URL url = new URL(host);
        // if parsing succeeded, then strip out the components and use
        secure = "https".equalsIgnoreCase(url.getProtocol());
        host = url.getHost();
        int specifiedPort = url.getPort();
        if (specifiedPort != -1)
            port = specifiedPort;
    } catch (MalformedURLException ex) {
        if (session.getDebug()) {
            session.getDebugOut().println("Not parsing " + host + " as a URL; using explicit options for "
                    + "secure, host, and port.");
        }
    }
    if (port == -1) {
        try {
            port = Integer.parseInt(session.getProperty(prefix + PORT_PROPERTY));
        } catch (Exception ignore) {
        }
        if (port == -1)
            port = secure ? HTTPS_PORT : HTTP_PORT;
    }
    String server = (secure ? "https://" : "http://") + host;
    if (secure ? (port != HTTPS_PORT) : (port != HTTP_PORT)) {
        server += ":" + port;
    }
    String mailbox = session.getProperty(MAILBOX_PROPERTY);
    if (mailbox == null) {
        mailbox = session.getProperty(prefix + FROM_PROPERTY);
        if (mailbox == null) {
            mailbox = InternetAddress.getLocalAddress(session).getAddress();
        }
    }

    int index = username.indexOf(':');
    if (index != -1) {
        mailbox = username.substring(index + 1);
        username = username.substring(0, index);
        String mailboxOptions = null;
        index = mailbox.indexOf('[');
        if (index != -1) {
            mailboxOptions = mailbox.substring(index + 1);
            mailboxOptions = mailboxOptions.substring(0, mailboxOptions.indexOf(']'));
            mailbox = mailbox.substring(0, index);
        }
        if (mailboxOptions != null) {
            Properties props = null;
            try {
                props = parseOptions(mailboxOptions);
            } catch (Exception ex) {
                throw new IllegalArgumentException("Unable to parse mailbox options: " + ex.getMessage(), ex);
            }
            String value = props.getProperty("unfiltered");
            if (value != null)
                unfiltered = Boolean.parseBoolean(value);

            /* Mirco */
            value = props.getProperty("filterLastCheck");
            if (value != null)
                filterLastCheck = value;
            value = props.getProperty("filterTo");
            if (value != null)
                filterTo = value;
            value = props.getProperty("filterFrom");
            if (value != null)
                filterFrom = value;
            value = props.getProperty("filterNotFrom");
            if (value != null)
                filterNotFrom = value;

            value = props.getProperty("delete");
            if (value != null)
                delete = Boolean.parseBoolean(value);
            value = props.getProperty("limit");
            if (value != null) {
                try {
                    limit = Integer.parseInt(value);
                } catch (NumberFormatException ex) {
                    throw new NumberFormatException("Invalid limit specified: " + value);
                }
            }
        } else if (session.getDebug()) {
            session.getDebugOut().println(
                    "No mailbox options specified; " + "using explicit limit, unfiltered, and delete.");
        }
    } else if (session.getDebug()) {
        session.getDebugOut().println("No mailbox specified in username; "
                + "using explicit mailbox, limit, unfiltered, and delete.");
    }
    int timeout = -1;
    String timeoutString = session.getProperty(prefix + TIMEOUT_PROPERTY);
    if (timeoutString != null) {
        try {
            timeout = Integer.parseInt(timeoutString);
        } catch (NumberFormatException ex) {
            throw new NumberFormatException("Invalid timeout value: " + timeoutString);
        }
    }
    int connectionTimeout = -1;
    timeoutString = session.getProperty(prefix + CONNECTION_TIMEOUT_PROPERTY);
    if (timeoutString != null) {
        try {
            connectionTimeout = Integer.parseInt(timeoutString);
        } catch (NumberFormatException ex) {
            throw new NumberFormatException("Invalid connection timeout value: " + timeoutString);
        }
    }
    InetAddress localAddress = null;
    String localAddressString = session.getProperty(prefix + LOCAL_ADDRESS_PROPERTY);
    if (localAddressString != null) {
        try {
            localAddress = InetAddress.getByName(localAddressString);
        } catch (Exception ex) {
            throw new UnknownHostException("Invalid local address specified: " + localAddressString);
        }
    }
    if (mailbox == null) {
        throw new IllegalStateException("No mailbox specified.");
    }
    if (session.getDebug()) {
        PrintStream debugStream = session.getDebugOut();
        debugStream.println("Server:\t" + server);
        debugStream.println("Username:\t" + username);
        debugStream.println("Password:\t" + pwd);
        debugStream.println("Mailbox:\t" + mailbox);
        debugStream.print("Options:\t");
        debugStream.print((limit > 0) ? "Message Limit = " + limit : "Unlimited Messages");
        debugStream.print(unfiltered ? "; Unfiltered" : "; Filtered to Unread");
        debugStream.print(filterLastCheck == null || "".equals(filterLastCheck) ? "; NO filterLastCheck"
                : "; Filtered after " + filterLastCheck);
        debugStream.print(filterFrom == null || "".equals(filterFrom) ? "; NO filterFromDomain"
                : "; Filtered from " + filterFrom);
        debugStream.print(filterNotFrom == null || "".equals(filterNotFrom) ? "; NO filterNotFrom"
                : "; Filtered not from " + filterNotFrom);
        debugStream.print(
                filterTo == null || "".equals(filterTo) ? "; NO filterToEmail" : "; Filtered to " + filterTo);
        debugStream.println(delete ? "; Delete Messages on Delete" : "; Mark as Read on Delete");
        if (timeout > 0) {
            debugStream.println("Read timeout:\t" + timeout + " ms");
        }
        if (connectionTimeout > 0) {
            debugStream.println("Connection timeout:\t" + connectionTimeout + " ms");
        }
    }
    return new Exchange2003Connection(session, server, mailbox, username, password, timeout, connectionTimeout,
            localAddress, unfiltered, delete, limit, filterLastCheck, filterFrom, filterNotFrom, filterTo);
}

From source file:org.exjello.mail.ExchangeConnection.java

public static ExchangeConnection createConnection(String protocol, Session session, String host, int port,
        String username, String password) throws Exception {
    String prefix = "mail." + protocol.toLowerCase() + ".";
    boolean debugPassword = Boolean.parseBoolean(session.getProperty(DEBUG_PASSWORD_PROPERTY));
    String pwd = (password == null) ? null : debugPassword ? password : "<password>";
    if (host == null || username == null || password == null) {
        if (session.getDebug()) {
            session.getDebugOut().println("Missing parameter; host=\"" + host + "\",username=\"" + username
                    + "\",password=\"" + pwd + "\"");
        }//from w w  w  .  j  a  v  a  2  s .  com
        throw new IllegalStateException("Host, username, and password must be specified.");
    }
    boolean unfiltered = Boolean.parseBoolean(session.getProperty(UNFILTERED_PROPERTY));
    /* Mirco */
    String filterLastCheck = session.getProperty(ExchangeConstants.FILTER_LAST_CHECK);
    String filterFrom = session.getProperty(ExchangeConstants.FILTER_FROM_PROPERTY);
    String filterNotFrom = session.getProperty(ExchangeConstants.FILTER_NOT_FROM_PROPERTY);
    String filterTo = session.getProperty(ExchangeConstants.FILTER_TO_PROPERTY);
    boolean delete = Boolean.parseBoolean(session.getProperty(DELETE_PROPERTY));
    boolean secure = Boolean.parseBoolean(session.getProperty(prefix + SSL_PROPERTY));
    int limit = -1;
    String limitString = session.getProperty(LIMIT_PROPERTY);
    if (limitString != null) {
        try {
            limit = Integer.parseInt(limitString);
        } catch (NumberFormatException ex) {
            throw new NumberFormatException("Invalid limit specified: " + limitString);
        }
    }
    try {
        URL url = new URL(host);
        // if parsing succeeded, then strip out the components and use
        secure = "https".equalsIgnoreCase(url.getProtocol());
        host = url.getHost();
        int specifiedPort = url.getPort();
        if (specifiedPort != -1)
            port = specifiedPort;
    } catch (MalformedURLException ex) {
        if (session.getDebug()) {
            session.getDebugOut().println("Not parsing " + host + " as a URL; using explicit options for "
                    + "secure, host, and port.");
        }
    }
    if (port == -1) {
        try {
            port = Integer.parseInt(session.getProperty(prefix + PORT_PROPERTY));
        } catch (Exception ignore) {
        }
        if (port == -1)
            port = secure ? HTTPS_PORT : HTTP_PORT;
    }
    String server = (secure ? "https://" : "http://") + host;
    if (secure ? (port != HTTPS_PORT) : (port != HTTP_PORT)) {
        server += ":" + port;
    }
    String mailbox = session.getProperty(MAILBOX_PROPERTY);
    if (mailbox == null) {
        mailbox = session.getProperty(prefix + FROM_PROPERTY);
        if (mailbox == null) {
            mailbox = InternetAddress.getLocalAddress(session).getAddress();
        }
    }

    int index = username.indexOf(':');
    if (index != -1) {
        mailbox = username.substring(index + 1);
        username = username.substring(0, index);
        String mailboxOptions = null;
        index = mailbox.indexOf('[');
        if (index != -1) {
            mailboxOptions = mailbox.substring(index + 1);
            mailboxOptions = mailboxOptions.substring(0, mailboxOptions.indexOf(']'));
            mailbox = mailbox.substring(0, index);
        }
        if (mailboxOptions != null) {
            Properties props = null;
            try {
                props = parseOptions(mailboxOptions);
            } catch (Exception ex) {
                throw new IllegalArgumentException("Unable to parse mailbox options: " + ex.getMessage(), ex);
            }
            String value = props.getProperty("unfiltered");
            if (value != null)
                unfiltered = Boolean.parseBoolean(value);

            /* Mirco */
            value = props.getProperty("filterLastCheck");
            if (value != null)
                filterLastCheck = value;
            value = props.getProperty("filterTo");
            if (value != null)
                filterTo = value;
            value = props.getProperty("filterFrom");
            if (value != null)
                filterFrom = value;
            value = props.getProperty("filterNotFrom");
            if (value != null)
                filterNotFrom = value;

            value = props.getProperty("delete");
            if (value != null)
                delete = Boolean.parseBoolean(value);
            value = props.getProperty("limit");
            if (value != null) {
                try {
                    limit = Integer.parseInt(value);
                } catch (NumberFormatException ex) {
                    throw new NumberFormatException("Invalid limit specified: " + value);
                }
            }
        } else if (session.getDebug()) {
            session.getDebugOut().println(
                    "No mailbox options specified; " + "using explicit limit, unfiltered, and delete.");
        }
    } else if (session.getDebug()) {
        session.getDebugOut().println("No mailbox specified in username; "
                + "using explicit mailbox, limit, unfiltered, and delete.");
    }
    int timeout = -1;
    String timeoutString = session.getProperty(prefix + TIMEOUT_PROPERTY);
    if (timeoutString != null) {
        try {
            timeout = Integer.parseInt(timeoutString);
        } catch (NumberFormatException ex) {
            throw new NumberFormatException("Invalid timeout value: " + timeoutString);
        }
    }
    int connectionTimeout = -1;
    timeoutString = session.getProperty(prefix + CONNECTION_TIMEOUT_PROPERTY);
    if (timeoutString != null) {
        try {
            connectionTimeout = Integer.parseInt(timeoutString);
        } catch (NumberFormatException ex) {
            throw new NumberFormatException("Invalid connection timeout value: " + timeoutString);
        }
    }
    InetAddress localAddress = null;
    String localAddressString = session.getProperty(prefix + LOCAL_ADDRESS_PROPERTY);
    if (localAddressString != null) {
        try {
            localAddress = InetAddress.getByName(localAddressString);
        } catch (Exception ex) {
            throw new UnknownHostException("Invalid local address specified: " + localAddressString);
        }
    }
    if (mailbox == null) {
        throw new IllegalStateException("No mailbox specified.");
    }
    if (session.getDebug()) {
        PrintStream debugStream = session.getDebugOut();
        debugStream.println("Server:\t" + server);
        debugStream.println("Username:\t" + username);
        debugStream.println("Password:\t" + pwd);
        debugStream.println("Mailbox:\t" + mailbox);
        debugStream.print("Options:\t");
        debugStream.print((limit > 0) ? "Message Limit = " + limit : "Unlimited Messages");
        debugStream.print(unfiltered ? "; Unfiltered" : "; Filtered to Unread");
        debugStream.print(filterLastCheck == null || "".equals(filterLastCheck) ? "; NO filterLastCheck"
                : "; Filtered after " + filterLastCheck);
        debugStream.print(filterFrom == null || "".equals(filterFrom) ? "; NO filterFromDomain"
                : "; Filtered from " + filterFrom);
        debugStream.print(filterNotFrom == null || "".equals(filterNotFrom) ? "; NO filterNotFrom"
                : "; Filtered not from " + filterNotFrom);
        debugStream.print(
                filterTo == null || "".equals(filterTo) ? "; NO filterToEmail" : "; Filtered to " + filterTo);
        debugStream.println(delete ? "; Delete Messages on Delete" : "; Mark as Read on Delete");
        if (timeout > 0) {
            debugStream.println("Read timeout:\t" + timeout + " ms");
        }
        if (connectionTimeout > 0) {
            debugStream.println("Connection timeout:\t" + connectionTimeout + " ms");
        }
    }
    return new ExchangeConnection(session, server, mailbox, username, password, timeout, connectionTimeout,
            localAddress, unfiltered, delete, limit, filterLastCheck, filterFrom, filterNotFrom, filterTo);
}