List of usage examples for org.apache.commons.net.imap IMAPSClient IMAPSClient
public IMAPSClient(SSLContext context)
From source file:Main.java
/** * Parse the URI and use the details to connect to the IMAP(S) server and login. * * @param uri the URI to use, e.g. imaps://user:pass@imap.mail.yahoo.com/folder * or imaps://user:pass@imap.googlemail.com/folder * @param defaultTimeout initial timeout (in milliseconds) * @param listener for tracing protocol IO (may be null) * @return the IMAP client - connected and logged in * @throws IOException if any problems occur *///from w w w. j av a 2s .c o m static IMAPClient imapLogin(URI uri, int defaultTimeout, ProtocolCommandListener listener) throws IOException { final String userInfo = uri.getUserInfo(); if (userInfo == null) { throw new IllegalArgumentException("Missing userInfo details"); } String[] userpass = userInfo.split(":"); if (userpass.length != 2) { throw new IllegalArgumentException("Invalid userInfo details: '" + userpass + "'"); } String username = userpass[0]; String password = userpass[1]; // TODO enable reading this secretly final IMAPClient imap; final String scheme = uri.getScheme(); if ("imaps".equalsIgnoreCase(scheme)) { System.out.println("Using secure protocol"); imap = new IMAPSClient(true); // implicit } else if ("imap".equalsIgnoreCase(scheme)) { imap = new IMAPClient(); } else { throw new IllegalArgumentException("Invalid protocol: " + scheme); } final int port = uri.getPort(); if (port != -1) { imap.setDefaultPort(port); } imap.setDefaultTimeout(defaultTimeout); if (listener != null) { imap.addProtocolCommandListener(listener); } final String server = uri.getHost(); System.out.println("Connecting to server " + server + " on " + imap.getDefaultPort()); try { imap.connect(server); System.out.println("Successfully connected"); } catch (IOException e) { throw new RuntimeException("Could not connect to server.", e); } if (!imap.login(username, password)) { imap.disconnect(); throw new RuntimeException("Could not login to server. Check login details."); } return imap; }
From source file:com.clustercontrol.port.protocol.ReachAddressIMAPS.java
/** * IMAPS????????/* ww w . ja v a 2 s .co m*/ * * @param addressText * @return IMAPS */ @Override protected boolean isRunning(String addressText) { m_message = ""; m_messageOrg = ""; m_response = -1; boolean isReachable = false; try { long start = 0; // long end = 0; // boolean retry = true; // ????(true:??false:???) StringBuffer bufferOrg = new StringBuffer(); // String result = ""; InetAddress address = InetAddress.getByName(addressText); bufferOrg.append("Monitoring the IMAPS Service of " + address.getHostName() + "[" + address.getHostAddress() + "]:" + m_portNo + ".\n\n"); IMAPSClient client = new IMAPSClient(true); for (int i = 0; i < m_sentCount && retry; i++) { try { bufferOrg.append(HinemosTime.getDateString() + " Tried to Connect: "); client.setDefaultTimeout(m_timeout); start = HinemosTime.currentTimeMillis(); client.connect(address, m_portNo); end = HinemosTime.currentTimeMillis(); m_response = end - start; result = client.getReplyString(); if (m_response > 0) { if (m_response < m_timeout) { result = result + ("\n" + "Response Time = " + m_response + "ms"); } else { m_response = m_timeout; result = result + ("\n" + "Response Time = " + m_response + "ms"); } } else { result = result + ("\n" + "Response Time < 1ms"); } retry = false; isReachable = true; } catch (SocketException e) { result = (e.getMessage() + "[SocketException]"); retry = true; isReachable = false; } catch (IOException e) { result = (e.getMessage() + "[IOException]"); retry = true; isReachable = false; } finally { bufferOrg.append(result + "\n"); if (client.isConnected()) { try { client.disconnect(); } catch (IOException e) { m_log.warn("isRunning(): " + "socket disconnect failed: " + e.getMessage(), e); } } } if (i < m_sentCount - 1 && retry) { try { Thread.sleep(m_sentInterval); } catch (InterruptedException e) { break; } } } m_message = result + "(IMAPS/" + m_portNo + ")"; m_messageOrg = bufferOrg.toString(); return isReachable; } catch (UnknownHostException e) { m_log.debug("isRunning(): " + MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage() + e.getMessage()); m_message = MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage() + " (" + e.getMessage() + ")"; return false; } }
From source file:examples.mail.IMAPUtils.java
/** * Parse the URI and use the details to connect to the IMAP(S) server and login. * * @param uri the URI to use, e.g. imaps://user:pass@imap.mail.yahoo.com/folder * or imaps://user:pass@imap.googlemail.com/folder * @param defaultTimeout initial timeout (in milliseconds) * @param listener for tracing protocol IO (may be null) * @return the IMAP client - connected and logged in * @throws IOException if any problems occur *///from w w w.j a v a 2 s. c om static IMAPClient imapLogin(URI uri, int defaultTimeout, ProtocolCommandListener listener) throws IOException { final String userInfo = uri.getUserInfo(); if (userInfo == null) { throw new IllegalArgumentException("Missing userInfo details"); } String[] userpass = userInfo.split(":"); if (userpass.length != 2) { throw new IllegalArgumentException("Invalid userInfo details: '" + userInfo + "'"); } String username = userpass[0]; String password = userpass[1]; /* * If the initial password is: * '*' - replace it with a line read from the system console * '-' - replace it with next line from STDIN * 'ABCD' - if the input is all upper case, use the field as an environment variable name * * Note: there are no guarantees that the password cannot be snooped. * * Even using the console may be subject to memory snooping, * however it should be safer than the other methods. * * STDIN may require creating a temporary file which could be read by others * Environment variables may be visible by using PS */ if ("-".equals(password)) { // stdin BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); password = in.readLine(); } else if ("*".equals(password)) { // console Console con = System.console(); // Java 1.6 if (con != null) { char[] pwd = con.readPassword("Password for " + username + ": "); password = new String(pwd); } else { throw new IOException("Cannot access Console"); } } else if (password.equals(password.toUpperCase(Locale.ROOT))) { // environment variable name final String tmp = System.getenv(password); if (tmp != null) { // don't overwrite if variable does not exist (just in case password is all uppers) password = tmp; } } final IMAPClient imap; final String scheme = uri.getScheme(); if ("imaps".equalsIgnoreCase(scheme)) { System.out.println("Using secure protocol"); imap = new IMAPSClient(true); // implicit } else if ("imap".equalsIgnoreCase(scheme)) { imap = new IMAPClient(); } else { throw new IllegalArgumentException("Invalid protocol: " + scheme); } final int port = uri.getPort(); if (port != -1) { imap.setDefaultPort(port); } imap.setDefaultTimeout(defaultTimeout); if (listener != null) { imap.addProtocolCommandListener(listener); } final String server = uri.getHost(); System.out.println("Connecting to server " + server + " on " + imap.getDefaultPort()); try { imap.connect(server); System.out.println("Successfully connected"); } catch (IOException e) { throw new RuntimeException("Could not connect to server.", e); } if (!imap.login(username, password)) { imap.disconnect(); throw new RuntimeException("Could not login to server. Check login details."); } return imap; }
From source file:me.schiz.jmeter.protocol.imap.sampler.IMAPSampler.java
private SampleResult sampleConnect(SampleResult sr) { IMAPClient client;//from ww w . java2 s . co m if (getUseSSL()) { client = new IMAPSClient(true); } else { client = new IMAPClient(); } try { String request = "CONNECT \n"; request += "Host : " + getHostname() + ":" + getPort() + "\n"; request += "Default Timeout : " + getDefaultTimeout() + "\n"; request += "Connect Timeout : " + getConnectionTimeout() + "\n"; request += "So Timeout : " + getSoTimeout() + "\n"; request += "Client : " + getClient() + "\n"; sr.setRequestHeaders(request); sr.sampleStart(); client.setDefaultTimeout(getDefaultTimeout()); client.setConnectTimeout(getConnectionTimeout()); if (getLocalAddr().isEmpty()) client.connect(getHostname(), getPort()); else client.connect(getHostname(), getPort(), InetAddress.getByName(getLocalAddr()), 0); if (client.isConnected()) { log.info("imap client " + getClient() + " connected from " + client.getLocalAddress() + ":" + client.getLocalPort()); SessionStorage.proto_type protoType = SessionStorage.proto_type.PLAIN; if (getUseSSL()) protoType = SessionStorage.proto_type.SSL; SessionStorage.getInstance().putClient(getSOClient(), client, protoType); client.setSoTimeout(getSoTimeout()); sr.setSuccessful(true); sr.setResponseCodeOK(); sr.setResponseData(client.getReplyString().getBytes()); } else { client.close(); sr.setSuccessful(false); sr.setResponseCode("java.net.ConnectException"); sr.setResponseMessage("Not connected"); } } catch (SocketException se) { sr.setResponseMessage(se.toString()); sr.setSuccessful(false); sr.setResponseCode(se.getClass().getName()); log.error("client `" + getClient() + "` ", se); removeClient(); } catch (IOException ioe) { sr.setResponseMessage(ioe.toString()); sr.setSuccessful(false); sr.setResponseCode(ioe.getClass().getName()); log.error("client `" + getClient() + "` ", ioe); removeClient(); } finally { sr.sampleEnd(); } return sr; }
From source file:org.apache.commons.net.examples.mail.IMAPUtils.java
/** * Parse the URI and use the details to connect to the IMAP(S) server and login. * * @param uri the URI to use, e.g. imaps://user:pass@imap.mail.yahoo.com/folder * or imaps://user:pass@imap.googlemail.com/folder * @param defaultTimeout initial timeout (in milliseconds) * @param listener for tracing protocol IO (may be null) * @return the IMAP client - connected and logged in * @throws IOException if any problems occur *///from w w w . ja v a 2 s. c om static IMAPClient imapLogin(URI uri, int defaultTimeout, ProtocolCommandListener listener) throws IOException { final String userInfo = uri.getUserInfo(); if (userInfo == null) { throw new IllegalArgumentException("Missing userInfo details"); } String[] userpass = userInfo.split(":"); if (userpass.length != 2) { throw new IllegalArgumentException("Invalid userInfo details: '" + userInfo + "'"); } String username = userpass[0]; String password = userpass[1]; // prompt for the password if necessary password = Utils.getPassword(username, password); final IMAPClient imap; final String scheme = uri.getScheme(); if ("imaps".equalsIgnoreCase(scheme)) { System.out.println("Using secure protocol"); imap = new IMAPSClient(true); // implicit } else if ("imap".equalsIgnoreCase(scheme)) { imap = new IMAPClient(); } else { throw new IllegalArgumentException("Invalid protocol: " + scheme); } final int port = uri.getPort(); if (port != -1) { imap.setDefaultPort(port); } imap.setDefaultTimeout(defaultTimeout); if (listener != null) { imap.addProtocolCommandListener(listener); } final String server = uri.getHost(); System.out.println("Connecting to server " + server + " on " + imap.getDefaultPort()); try { imap.connect(server); System.out.println("Successfully connected"); } catch (IOException e) { throw new RuntimeException("Could not connect to server.", e); } if (!imap.login(username, password)) { imap.disconnect(); throw new RuntimeException("Could not login to server. Check login details."); } return imap; }