001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.net.examples.mail; 019 020import java.io.BufferedReader; 021import java.io.IOException; 022import java.io.PrintWriter; 023import java.util.Locale; 024 025import org.apache.commons.net.PrintCommandListener; 026import org.apache.commons.net.pop3.POP3Client; 027import org.apache.commons.net.pop3.POP3MessageInfo; 028import org.apache.commons.net.pop3.POP3SClient; 029 030/** 031 * This is an example program demonstrating how to use the POP3[S]Client class. This program connects to a POP3[S] server and retrieves the message headers of 032 * all the messages, printing the From: and Subject: header entries for each message. 033 * <p> 034 * See main() method for usage details 035 */ 036public final class POP3Mail { 037 038 public static void main(final String[] args) { 039 if (args.length < 3) { 040 System.err.println("Usage: POP3Mail <server[:port]> <username> <password|-|*|VARNAME> [TLS [true=implicit]]"); 041 System.exit(1); 042 } 043 044 final String arg0[] = args[0].split(":"); 045 final String server = arg0[0]; 046 final String username = args[1]; 047 String password = args[2]; 048 // prompt for the password if necessary 049 try { 050 password = Utils.getPassword(username, password); 051 } catch (final IOException e1) { 052 System.err.println("Could not retrieve password: " + e1.getMessage()); 053 return; 054 } 055 056 final String proto = args.length > 3 ? args[3] : null; 057 final boolean implicit = args.length > 4 && Boolean.parseBoolean(args[4]); 058 059 final POP3Client pop3; 060 061 if (proto != null) { 062 System.out.println("Using secure protocol: " + proto); 063 pop3 = new POP3SClient(proto, implicit); 064 } else { 065 pop3 = new POP3Client(); 066 } 067 068 final int port; 069 if (arg0.length == 2) { 070 port = Integer.parseInt(arg0[1]); 071 } else { 072 port = pop3.getDefaultPort(); 073 } 074 System.out.println("Connecting to server " + server + " on " + port); 075 076 // We want to timeout if a response takes longer than 60 seconds 077 pop3.setDefaultTimeout(60000); 078 079 // suppress login details 080 pop3.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true)); 081 082 try { 083 pop3.connect(server); 084 } catch (final IOException e) { 085 System.err.println("Could not connect to server."); 086 e.printStackTrace(); 087 return; 088 } 089 090 try { 091 if (!pop3.login(username, password)) { 092 System.err.println("Could not login to server. Check password."); 093 pop3.disconnect(); 094 return; 095 } 096 097 final POP3MessageInfo status = pop3.status(); 098 if (status == null) { 099 System.err.println("Could not retrieve status."); 100 pop3.logout(); 101 pop3.disconnect(); 102 return; 103 } 104 105 System.out.println("Status: " + status); 106 107 final POP3MessageInfo[] messages = pop3.listMessages(); 108 109 if (messages == null) { 110 System.err.println("Could not retrieve message list."); 111 pop3.logout(); 112 pop3.disconnect(); 113 return; 114 } 115 if (messages.length == 0) { 116 System.out.println("No messages"); 117 pop3.logout(); 118 pop3.disconnect(); 119 return; 120 } 121 122 System.out.println("Message count: " + messages.length); 123 124 for (final POP3MessageInfo msginfo : messages) { 125 final BufferedReader reader = (BufferedReader) pop3.retrieveMessageTop(msginfo.number, 0); 126 127 if (reader == null) { 128 System.err.println("Could not retrieve message header."); 129 pop3.logout(); 130 pop3.disconnect(); 131 return; 132 } 133 printMessageInfo(reader, msginfo.number); 134 } 135 136 pop3.logout(); 137 pop3.disconnect(); 138 } catch (final IOException e) { 139 e.printStackTrace(); 140 return; 141 } 142 } 143 144 public static void printMessageInfo(final BufferedReader reader, final int id) throws IOException { 145 String from = ""; 146 String subject = ""; 147 String line; 148 while ((line = reader.readLine()) != null) { 149 final String lower = line.toLowerCase(Locale.ENGLISH); 150 if (lower.startsWith("from: ")) { 151 from = line.substring(6).trim(); 152 } else if (lower.startsWith("subject: ")) { 153 subject = line.substring(9).trim(); 154 } 155 } 156 157 System.out.println(Integer.toString(id) + " From: " + from + " Subject: " + subject); 158 } 159}