| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| examples.messages |
|
|
| 1 | /* |
|
| 2 | * Copyright 2001-2005 The Apache Software Foundation |
|
| 3 | * |
|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 5 | * you may not use this file except in compliance with the License. |
|
| 6 | * You may obtain a copy of the License at |
|
| 7 | * |
|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 9 | * |
|
| 10 | * Unless required by applicable law or agreed to in writing, software |
|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 13 | * See the License for the specific language governing permissions and |
|
| 14 | * limitations under the License. |
|
| 15 | */ |
|
| 16 | package examples; |
|
| 17 | ||
| 18 | import java.io.BufferedReader; |
|
| 19 | import java.io.IOException; |
|
| 20 | import java.io.Reader; |
|
| 21 | import org.apache.commons.net.pop3.POP3Client; |
|
| 22 | import org.apache.commons.net.pop3.POP3MessageInfo; |
|
| 23 | ||
| 24 | /*** |
|
| 25 | * This is an example program demonstrating how to use the POP3Client class. |
|
| 26 | * This program connects to a POP3 server and retrieves the message |
|
| 27 | * headers of all the messages, printing the From: and Subject: header |
|
| 28 | * entries for each message. |
|
| 29 | * <p> |
|
| 30 | * Usage: messages <pop3 server hostname> <username> <password> |
|
| 31 | * <p> |
|
| 32 | ***/ |
|
| 33 | 0 | public final class messages |
| 34 | { |
|
| 35 | ||
| 36 | public static final void printMessageInfo(BufferedReader reader, int id) |
|
| 37 | throws IOException |
|
| 38 | { |
|
| 39 | String line, lower, from, subject; |
|
| 40 | ||
| 41 | 0 | from = ""; |
| 42 | 0 | subject = ""; |
| 43 | ||
| 44 | 0 | while ((line = reader.readLine()) != null) |
| 45 | { |
|
| 46 | 0 | lower = line.toLowerCase(); |
| 47 | 0 | if (lower.startsWith("from: ")) |
| 48 | 0 | from = line.substring(6).trim(); |
| 49 | 0 | else if (lower.startsWith("subject: ")) |
| 50 | 0 | subject = line.substring(9).trim(); |
| 51 | } |
|
| 52 | ||
| 53 | 0 | System.out.println(Integer.toString(id) + " From: " + from + |
| 54 | " Subject: " + subject); |
|
| 55 | 0 | } |
| 56 | ||
| 57 | public static final void main(String[] args) |
|
| 58 | { |
|
| 59 | int message; |
|
| 60 | String server, username, password; |
|
| 61 | POP3Client pop3; |
|
| 62 | Reader reader; |
|
| 63 | POP3MessageInfo[] messages; |
|
| 64 | ||
| 65 | 0 | if (args.length < 3) |
| 66 | { |
|
| 67 | 0 | System.err.println( |
| 68 | "Usage: messages <pop3 server hostname> <username> <password>"); |
|
| 69 | 0 | System.exit(1); |
| 70 | } |
|
| 71 | ||
| 72 | 0 | server = args[0]; |
| 73 | 0 | username = args[1]; |
| 74 | 0 | password = args[2]; |
| 75 | ||
| 76 | 0 | pop3 = new POP3Client(); |
| 77 | // We want to timeout if a response takes longer than 60 seconds |
|
| 78 | 0 | pop3.setDefaultTimeout(60000); |
| 79 | ||
| 80 | try |
|
| 81 | { |
|
| 82 | 0 | pop3.connect(server); |
| 83 | } |
|
| 84 | 0 | catch (IOException e) |
| 85 | { |
|
| 86 | 0 | System.err.println("Could not connect to server."); |
| 87 | 0 | e.printStackTrace(); |
| 88 | 0 | System.exit(1); |
| 89 | 0 | } |
| 90 | ||
| 91 | try |
|
| 92 | { |
|
| 93 | 0 | if (!pop3.login(username, password)) |
| 94 | { |
|
| 95 | 0 | System.err.println("Could not login to server. Check password."); |
| 96 | 0 | pop3.disconnect(); |
| 97 | 0 | System.exit(1); |
| 98 | } |
|
| 99 | ||
| 100 | 0 | messages = pop3.listMessages(); |
| 101 | ||
| 102 | 0 | if (messages == null) |
| 103 | { |
|
| 104 | 0 | System.err.println("Could not retrieve message list."); |
| 105 | 0 | pop3.disconnect(); |
| 106 | 0 | System.exit(1); |
| 107 | } |
|
| 108 | 0 | else if (messages.length == 0) |
| 109 | { |
|
| 110 | 0 | System.out.println("No messages"); |
| 111 | 0 | pop3.logout(); |
| 112 | 0 | pop3.disconnect(); |
| 113 | 0 | System.exit(1); |
| 114 | } |
|
| 115 | ||
| 116 | 0 | for (message = 0; message < messages.length; message++) |
| 117 | { |
|
| 118 | 0 | reader = pop3.retrieveMessageTop(messages[message].number, 0); |
| 119 | ||
| 120 | 0 | if (reader == null) |
| 121 | { |
|
| 122 | 0 | System.err.println("Could not retrieve message header."); |
| 123 | 0 | pop3.disconnect(); |
| 124 | 0 | System.exit(1); |
| 125 | } |
|
| 126 | ||
| 127 | 0 | printMessageInfo(new BufferedReader(reader), messages[message].number); |
| 128 | } |
|
| 129 | ||
| 130 | 0 | pop3.logout(); |
| 131 | 0 | pop3.disconnect(); |
| 132 | } |
|
| 133 | 0 | catch (IOException e) |
| 134 | { |
|
| 135 | 0 | e.printStackTrace(); |
| 136 | 0 | System.exit(1); |
| 137 | 0 | } |
| 138 | 0 | } |
| 139 | } |
|
| 140 |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |