1 22 package gnu.mail.providers.pop3; 23 import java.io.IOException ; 24 25 import java.io.InputStream ; 26 import java.net.Socket ; 27 import java.net.UnknownHostException ; 28 import javax.mail.Folder ; 29 import javax.mail.Message ; 30 import javax.mail.MessagingException ; 31 import javax.mail.Session ; 32 import javax.mail.Store ; 33 import javax.mail.URLName ; 34 35 import gnu.mail.util.CRLFInputStream; 36 import gnu.mail.util.CRLFOutputStream; 37 import gnu.mail.util.LineInputStream; 38 import gnu.mail.util.MessageInputStream; 39 40 47 public final class POP3Store 48 extends Store 49 { 50 51 52 public final static int DEFAULT_PORT = 110; 53 54 static int fetchsize = 1024; 55 56 static final int OK = 0, ERR = -1; 57 58 static final String CRLF = "\r\n"; 59 60 Socket socket; 61 LineInputStream in; 62 CRLFOutputStream out; 63 String hostname; String response; 66 POP3Folder root; 68 69 75 public POP3Store(Session session, URLName urlname) 76 { 77 super(session, urlname); 78 debug = session.getDebug(); 79 String ccs = session.getProperty("mail.pop3.fetchsize"); 80 if (ccs != null) { 81 try { 82 fetchsize = Math.max(Integer.parseInt(ccs), 1024); 83 } 84 catch (NumberFormatException e) { 85 } 86 } 87 } 88 89 90 92 103 protected boolean protocolConnect(String host, int port, String username, String password) 104 throws MessagingException 105 { 106 if (port < 0) { 107 port = DEFAULT_PORT; 108 } 109 if (host == null || username == null || password == null) { 110 return false; 111 } 112 if (socket != null) { 113 return true; 114 } 115 synchronized (this) { 116 try { 117 hostname = host; 118 socket = new Socket (host, port); 119 in = new LineInputStream(new CRLFInputStream(socket.getInputStream())); 120 out = new CRLFOutputStream(socket.getOutputStream()); 121 if (getResponse() != OK) { 122 throw new MessagingException ("Connect failed: " + response); 123 } 124 int index = response.indexOf(' '); 125 if (index > -1) { 126 hostname = response.substring(0, index); 127 } 128 send("USER " + username); 129 if (getResponse() != OK) { 130 return false; 131 } 132 send("PASS " + password); 133 if (getResponse() != OK) { 134 return false; 135 } 136 return true; 137 } 138 catch (UnknownHostException e) { 139 throw new MessagingException ("Connect failed", e); 140 } 141 catch (IOException e) { 142 throw new MessagingException ("Connect failed", e); 143 } 144 catch (NullPointerException e) { 145 throw new MessagingException ("Connect failed", e); 146 } 147 } 148 } 149 150 151 156 public synchronized void close() 157 throws MessagingException 158 { 159 if (socket != null) { 160 synchronized (this) { 161 try { 162 send("QUIT"); 163 if (getResponse() != OK) { 164 throw new MessagingException ("Close failed: " + response); 165 } 166 socket.close(); 167 socket = null; 168 } 169 catch (IOException e) { 170 } 173 } 174 } 175 super.close(); 176 177 } 178 179 180 189 private int getResponse() 190 throws IOException 191 { 192 String okstr = "+OK"; 193 String errstr = "-ERR"; 194 response = in.readLine(); 195 Session.log("POP< " + response); 196 if (response != null) { 197 if (response.indexOf(okstr) == 0) { 198 response = response.substring(okstr.length()).trim(); 199 return OK; 200 } 201 else if (response.indexOf(errstr) == 0) { 202 response = response.substring(errstr.length()).trim(); 203 } 204 } 205 return ERR; 206 } 207 208 209 218 private void send(String command) 219 throws IOException 220 { 221 final String PasswordCommandPrefix = "PASS "; 222 223 if (command.toUpperCase().startsWith(PasswordCommandPrefix)) { 224 Session.log("POP> " + PasswordCommandPrefix + "***"); 225 } 226 else 227 { 228 Session.log("POP> " + command); 229 } 230 231 out.write((command + CRLF).getBytes()); 232 out.flush(); 233 } 234 235 236 243 synchronized InputStream popRETR(int msgnum) 244 throws MessagingException 245 { 246 try { 247 send("RETR " + msgnum); 248 if (getResponse() != OK) { 249 throw new MessagingException ("Retrieve failed: " + response); 250 } 251 return new MessageInputStream(in); 252 } 253 catch (IOException e) { 254 throw new MessagingException ("Retrieve failed.", e); 255 } 256 catch (NumberFormatException e) { 257 throw new MessagingException ("Retrieve failed.", e); 258 } 259 } 260 261 262 280 synchronized InputStream popTOP(int msgnum) 281 throws MessagingException 282 { 283 try { 284 send("TOP " + msgnum + " 0"); 285 if (getResponse() != OK) { 286 throw new MessagingException ("Retrieve failed: " + response); 287 } 288 return new MessageInputStream(in); 289 } 290 catch (IOException e) { 291 throw new MessagingException ("Retrieve failed.", e); 292 } 293 catch (NumberFormatException e) { 294 throw new MessagingException ("Retrieve failed.", e); 295 } 296 } 297 298 299 302 String getHostName() 303 { 304 return hostname; 305 } 306 307 309 315 synchronized int getMessageCount() 316 throws MessagingException 317 { 318 try { 319 send("STAT"); 320 if (getResponse() != OK) { 321 throw new MessagingException ("Status failed: " + response); 322 } 323 try { 324 return Integer.parseInt(response.substring(0, response.indexOf(' '))); 325 } 326 catch (NumberFormatException e) { 327 throw new MessagingException ("Status failed: " + response); 328 } 329 } 330 catch (IOException e) { 331 throw new MessagingException ("Status failed.", e); 332 } 333 } 334 335 336 346 synchronized Message getMessage(POP3Folder folder, int msgnum) 347 throws MessagingException 348 { 349 Message message = null; 350 int size = -1; 351 try { 352 send("LIST " + msgnum); 353 if (getResponse() != OK) { 354 throw new MessagingException ("List failed: " + response); 355 } 356 String sizePart = response.substring(response.indexOf(' ') + 1); 358 size = Integer.parseInt(sizePart); 359 } 360 catch (Throwable t) { 361 try { 364 send("RETR " + msgnum); 365 if (getResponse() != OK) { 366 throw new MessagingException ("Retrieve failed: " + response); 367 } 368 } 369 catch (IOException e) { 370 throw new MessagingException ("Retrieve failed.", e); 371 } 372 } 373 374 if (size > -1) { 376 message = new POP3Message(folder, msgnum, size); 377 } 378 else { 379 message = new POP3Message(folder, new MessageInputStream(in), msgnum); 380 } 381 382 return message; 383 } 384 385 386 synchronized void delete(int msgnum) 387 throws MessagingException 388 { 389 try { 390 send("DELE " + msgnum); 391 if (getResponse() != OK) { 392 throw new MessagingException ("Delete failed: " + response); 393 } 394 } 395 catch (IOException e) { 396 throw new MessagingException ("Delete failed.", e); 397 } 398 } 399 400 401 407 public Folder getDefaultFolder() 408 throws MessagingException 409 { 410 synchronized (this) { 411 if (root == null) { 412 root = new POP3Folder(this, Folder.HOLDS_FOLDERS); 413 } 414 } 415 return root; 416 } 417 418 419 426 public Folder getFolder(String s) 427 throws MessagingException 428 { 429 return getDefaultFolder().getFolder(s); 430 } 431 432 433 440 public Folder getFolder(URLName urlname) 441 throws MessagingException 442 { 443 return getDefaultFolder().getFolder(urlname.getFile()); 444 } 445 446 } 447 448 | Popular Tags |