List of usage examples for org.apache.commons.net.pop3 POP3Client disconnect
@Override public void disconnect() throws IOException
DISCONNECTED_STATE
. From source file:org.apache.common.net.examples.mail.POP3Mail.java
public static final void main(String[] args) { if (args.length < 3) { System.err/* w w w . j a va 2s.c o m*/ .println("Usage: POP3Mail <pop3 server hostname> <username> <password> [TLS [true=implicit]]"); System.exit(1); } String server = args[0]; String username = args[1]; String password = args[2]; String proto = args.length > 3 ? args[3] : null; boolean implicit = args.length > 4 ? Boolean.parseBoolean(args[4]) : false; POP3Client pop3; if (proto != null) { System.out.println("Using secure protocol: " + proto); pop3 = new POP3SClient(proto, implicit); } else { pop3 = new POP3Client(); } System.out.println("Connecting to server " + server + " on " + pop3.getDefaultPort()); // We want to timeout if a response takes longer than 60 seconds pop3.setDefaultTimeout(60000); // suppress login details pop3.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true)); try { pop3.connect(server); } catch (IOException e) { System.err.println("Could not connect to server."); e.printStackTrace(); System.exit(1); } try { if (!pop3.login(username, password)) { System.err.println("Could not login to server. Check password."); pop3.disconnect(); System.exit(1); } POP3MessageInfo[] messages = pop3.listMessages(); if (messages == null) { System.err.println("Could not retrieve message list."); pop3.disconnect(); return; } else if (messages.length == 0) { System.out.println("No messages"); pop3.logout(); pop3.disconnect(); return; } for (POP3MessageInfo msginfo : messages) { BufferedReader reader = (BufferedReader) pop3.retrieveMessageTop(msginfo.number, 0); if (reader == null) { System.err.println("Could not retrieve message header."); pop3.disconnect(); System.exit(1); } printMessageInfo(reader, msginfo.number); } pop3.logout(); pop3.disconnect(); } catch (IOException e) { e.printStackTrace(); return; } }
From source file:org.apache.commons.net.examples.mail.POP3ExportMbox.java
public static void main(String[] args) { int argIdx;/*from w w w. j av a 2 s. co m*/ String file = null; for (argIdx = 0; argIdx < args.length; argIdx++) { if (args[argIdx].equals("-F")) { file = args[++argIdx]; } else { break; } } final int argCount = args.length - argIdx; if (argCount < 3) { System.err.println( "Usage: POP3Mail [-F file/directory] <server[:port]> <username> <password|-|*|VARNAME> [TLS [true=implicit]]"); System.exit(1); } String arg0[] = args[argIdx++].split(":"); String server = arg0[0]; String username = args[argIdx++]; String password = args[argIdx++]; // prompt for the password if necessary try { password = Utils.getPassword(username, password); } catch (IOException e1) { System.err.println("Could not retrieve password: " + e1.getMessage()); return; } String proto = argCount > 3 ? args[argIdx++] : null; boolean implicit = argCount > 4 ? Boolean.parseBoolean(args[argIdx++]) : false; POP3Client pop3; if (proto != null) { System.out.println("Using secure protocol: " + proto); pop3 = new POP3SClient(proto, implicit); } else { pop3 = new POP3Client(); } int port; if (arg0.length == 2) { port = Integer.parseInt(arg0[1]); } else { port = pop3.getDefaultPort(); } System.out.println("Connecting to server " + server + " on " + port); // We want to timeout if a response takes longer than 60 seconds pop3.setDefaultTimeout(60000); try { pop3.connect(server); } catch (IOException e) { System.err.println("Could not connect to server."); e.printStackTrace(); return; } try { if (!pop3.login(username, password)) { System.err.println("Could not login to server. Check password."); pop3.disconnect(); return; } POP3MessageInfo status = pop3.status(); if (status == null) { System.err.println("Could not retrieve status."); pop3.logout(); pop3.disconnect(); return; } System.out.println("Status: " + status); int count = status.number; if (file != null) { System.out.println("Getting messages: " + count); File mbox = new File(file); if (mbox.isDirectory()) { System.out.println("Writing dir: " + mbox); // Currently POP3Client uses iso-8859-1 for (int i = 1; i <= count; i++) { OutputStreamWriter fw = new OutputStreamWriter( new FileOutputStream(new File(mbox, i + ".eml")), Charset.forName("iso-8859-1")); writeFile(pop3, fw, i); fw.close(); } } else { System.out.println("Writing file: " + mbox); // Currently POP3Client uses iso-8859-1 OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(mbox), Charset.forName("iso-8859-1")); for (int i = 1; i <= count; i++) { writeMbox(pop3, fw, i); } fw.close(); } } pop3.logout(); pop3.disconnect(); } catch (IOException e) { e.printStackTrace(); return; } }
From source file:org.apache.commons.net.examples.mail.POP3Mail.java
public static void main(String[] args) { if (args.length < 3) { System.err.println(/* w w w .j a va 2 s. c om*/ "Usage: POP3Mail <server[:port]> <username> <password|-|*|VARNAME> [TLS [true=implicit]]"); System.exit(1); } String arg0[] = args[0].split(":"); String server = arg0[0]; String username = args[1]; String password = args[2]; // prompt for the password if necessary try { password = Utils.getPassword(username, password); } catch (IOException e1) { System.err.println("Could not retrieve password: " + e1.getMessage()); return; } String proto = args.length > 3 ? args[3] : null; boolean implicit = args.length > 4 ? Boolean.parseBoolean(args[4]) : false; POP3Client pop3; if (proto != null) { System.out.println("Using secure protocol: " + proto); pop3 = new POP3SClient(proto, implicit); } else { pop3 = new POP3Client(); } int port; if (arg0.length == 2) { port = Integer.parseInt(arg0[1]); } else { port = pop3.getDefaultPort(); } System.out.println("Connecting to server " + server + " on " + port); // We want to timeout if a response takes longer than 60 seconds pop3.setDefaultTimeout(60000); // suppress login details pop3.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true)); try { pop3.connect(server); } catch (IOException e) { System.err.println("Could not connect to server."); e.printStackTrace(); return; } try { if (!pop3.login(username, password)) { System.err.println("Could not login to server. Check password."); pop3.disconnect(); return; } POP3MessageInfo status = pop3.status(); if (status == null) { System.err.println("Could not retrieve status."); pop3.logout(); pop3.disconnect(); return; } System.out.println("Status: " + status); POP3MessageInfo[] messages = pop3.listMessages(); if (messages == null) { System.err.println("Could not retrieve message list."); pop3.logout(); pop3.disconnect(); return; } else if (messages.length == 0) { System.out.println("No messages"); pop3.logout(); pop3.disconnect(); return; } System.out.println("Message count: " + messages.length); for (POP3MessageInfo msginfo : messages) { BufferedReader reader = (BufferedReader) pop3.retrieveMessageTop(msginfo.number, 0); if (reader == null) { System.err.println("Could not retrieve message header."); pop3.logout(); pop3.disconnect(); return; } printMessageInfo(reader, msginfo.number); } pop3.logout(); pop3.disconnect(); } catch (IOException e) { e.printStackTrace(); return; } }
From source file:org.apache.james.pop3server.POP3ServerTest.java
/** * Test for JAMES-1202 - This was failing before as the more then one connection to the same * mailbox was not handled the right way *///from w w w. ja v a2 s.c o m @Test @Ignore public void testStatUidlListTwoConnections() throws Exception { finishSetUp(pop3Configuration); pop3Client = new POP3Client(); pop3Client.connect("127.0.0.1", pop3Port); usersRepository.addUser("foo2", "bar2"); MailboxPath mailboxPath = new MailboxPath(MailboxConstants.USER_NAMESPACE, "foo2", "INBOX"); MailboxSession session = mailboxManager.login("foo2", "bar2", LoggerFactory.getLogger("Test")); if (!mailboxManager.mailboxExists(mailboxPath, session)) { mailboxManager.createMailbox(mailboxPath, session); } int msgCount = 100; for (int i = 0; i < msgCount; i++) { mailboxManager.getMailbox(mailboxPath, session).appendMessage( new ByteArrayInputStream(("Subject: test\r\n\r\n" + i).getBytes()), new Date(), session, true, new Flags()); } pop3Client.login("foo2", "bar2"); assertEquals(1, pop3Client.getState()); POP3MessageInfo[] listEntries = pop3Client.listMessages(); POP3MessageInfo[] uidlEntries = pop3Client.listUniqueIdentifiers(); POP3MessageInfo statInfo = pop3Client.status(); assertEquals(msgCount, listEntries.length); assertEquals(msgCount, uidlEntries.length); assertEquals(msgCount, statInfo.number); POP3Client m_pop3Protocol2 = new POP3Client(); m_pop3Protocol2.connect("127.0.0.1", pop3Port); m_pop3Protocol2.login("foo2", "bar2"); assertEquals(1, m_pop3Protocol2.getState()); POP3MessageInfo[] listEntries2 = m_pop3Protocol2.listMessages(); POP3MessageInfo[] uidlEntries2 = m_pop3Protocol2.listUniqueIdentifiers(); POP3MessageInfo statInfo2 = m_pop3Protocol2.status(); assertEquals(msgCount, listEntries2.length); assertEquals(msgCount, uidlEntries2.length); assertEquals(msgCount, statInfo2.number); pop3Client.deleteMessage(1); listEntries = pop3Client.listMessages(); uidlEntries = pop3Client.listUniqueIdentifiers(); statInfo = pop3Client.status(); assertEquals(msgCount - 1, listEntries.length); assertEquals(msgCount - 1, uidlEntries.length); assertEquals(msgCount - 1, statInfo.number); // even after the message was deleted it should get displayed in the // second connection listEntries2 = m_pop3Protocol2.listMessages(); uidlEntries2 = m_pop3Protocol2.listUniqueIdentifiers(); statInfo2 = m_pop3Protocol2.status(); assertEquals(msgCount, listEntries2.length); assertEquals(msgCount, uidlEntries2.length); assertEquals(msgCount, statInfo2.number); assertTrue(pop3Client.logout()); pop3Client.disconnect(); // even after the message was deleted and the session was quit it should // get displayed in the second connection listEntries2 = m_pop3Protocol2.listMessages(); uidlEntries2 = m_pop3Protocol2.listUniqueIdentifiers(); statInfo2 = m_pop3Protocol2.status(); assertEquals(msgCount, listEntries2.length); assertEquals(msgCount, uidlEntries2.length); assertEquals(msgCount, statInfo2.number); // This both should error and so return null assertNull(m_pop3Protocol2.retrieveMessageTop(1, 100)); assertNull(m_pop3Protocol2.retrieveMessage(1)); m_pop3Protocol2.sendCommand("quit"); m_pop3Protocol2.disconnect(); mailboxManager.deleteMailbox(mailboxPath, session); }