POP3Store.java
上传用户:huihesys
上传日期:2007-01-04
资源大小:3877k
文件大小:6k
- /*
- * POP3Store.java
- * Copyright (C) 1999 dog <dog@dog.net.uk>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * You may retrieve the latest version of this library from
- * http://www.dog.net.uk/knife/
- */
- package dog.mail.pop3;
- import java.io.*;
- import java.net.*;
- import javax.mail.*;
- import javax.mail.event.*;
- import dog.mail.util.*;
- /**
- * The storage class implementing the POP3 mail protocol.
- *
- * @author dog@dog.net.uk
- * @version 1.1
- */
- public class POP3Store extends Store {
- /**
- * The default POP3 port.
- */
- public static final int DEFAULT_PORT = 110;
- static int fetchsize = 1024;
-
- Socket socket;
- CRLFInputStream in;
- CRLFOutputStream out;
- String hostname; // the server hostname
- static int OK = 0, ERR = -1; // response codes
- String response; // last response
- POP3Folder root; // the root folder
- /**
- * Constructor.
- */
- public POP3Store(Session session, URLName urlname) {
- super(session, urlname);
- String ccs = session.getProperty("mail.pop3.fetchsize");
- if (ccs!=null) try { fetchsize = Math.max(Integer.parseInt(ccs), 1024); } catch (NumberFormatException e) {}
- }
-
- /**
- * Connects to the POP3 server and authenticates with the specified parameters.
- */
- protected boolean protocolConnect(String host, int port, String username, String password) throws MessagingException {
- if (port<0) port = DEFAULT_PORT;
- if (host==null || username==null || password==null)
- return false;
- if (socket!=null)
- return true;
- synchronized (this) {
- try {
- hostname = host;
- socket = new Socket(host, port);
- in = new CRLFInputStream(socket.getInputStream());
- out = new CRLFOutputStream(socket.getOutputStream());
- if (getResponse()!=OK)
- throw new MessagingException("Connect failed. Server responded: "+response);
- int index = response.indexOf(' ');
- if (index>-1) hostname = response.substring(0, index);
- send("USER "+username);
- if (getResponse()!=OK)
- throw new MessagingException("Connect failed. Server responded: "+response);
- send("PASS "+password);
- if (getResponse()!=OK)
- throw new MessagingException("Connect failed. Server responded: "+response);
- return true;
- } catch(UnknownHostException e) {
- throw new MessagingException("Connect failed", e);
- } catch(IOException e) {
- throw new MessagingException("Connect failed", e);
- }
- }
- }
- /**
- * Closes the connection.
- */
- public synchronized void close() throws MessagingException {
- if (socket!=null) {
- synchronized (this) {
- try {
- send("QUIT");
- if (getResponse()!=OK)
- throw new MessagingException("Close failed: "+response);
- socket.close();
- socket = null;
- } catch (IOException e) {
- // socket.close() always seems to throw an exception!
- //throw new MessagingException("Close failed", e);
- }
- }
- }
- super.close();
-
- }
- private int getResponse() throws IOException {
- String okstr = "+OK", errstr = "-ERR";
- response = in.readLine();
- if (response.indexOf(okstr)==0) {
- response = response.substring(okstr.length()).trim();
- return OK;
- } else if (response.indexOf(errstr)==0)
- response = response.substring(errstr.length()).trim();
- return ERR;
- }
- private void send(String command) throws IOException {
- out.write(command.getBytes());
- out.writeln();
- out.flush();
- }
- String getHostName() { return hostname; }
- synchronized int getMessageCount() throws MessagingException {
- try {
- send("STAT");
- if (getResponse()!=OK)
- throw new MessagingException("Status failed. Server responded: "+response);
- try {
- return Integer.parseInt(response.substring(0, response.indexOf(' ')));
- } catch(NumberFormatException e) {
- throw new MessagingException("Status failed. Server responded: "+response);
- }
- } catch (IOException e) {
- throw new MessagingException("Status failed.", e);
- }
- }
- synchronized Message getMessage(POP3Folder folder, int msgnum) throws MessagingException {
- //int length = -1;
- try {
- send("RETR "+msgnum);
- if (getResponse()!=OK)
- throw new MessagingException("Retrieve failed. Server responded: "+response);
- //length = Integer.parseInt(response.substring(0, response.indexOf(' '))); // sod all use, it's always wrong
- return new POP3Message(folder, new MessageInputStream(in), msgnum);
- } catch (IOException e) {
- throw new MessagingException("Retrieve failed.", e);
- } catch (NumberFormatException e) {
- throw new MessagingException("Retrieve failed.", e);
- }
- }
- synchronized void delete(int msgnum) throws MessagingException {
- try {
- send("DELE "+msgnum);
- if (getResponse()!=OK)
- throw new MessagingException("Delete failed. Server responded: "+response);
- } catch (IOException e) {
- throw new MessagingException("Delete failed.", e);
- }
- }
- /**
- * Returns the root folder.
- */
- public Folder getDefaultFolder() throws MessagingException {
- synchronized (this) {
- if (root==null) root = new POP3Folder(this, Folder.HOLDS_FOLDERS);
- }
- return root;
- }
- /**
- * Returns the folder with the specified name.
- */
- public Folder getFolder(String s) throws MessagingException {
- return getDefaultFolder().getFolder(s);
- }
- /**
- * Returns the folder whose name is the file part of the specified URLName.
- */
- public Folder getFolder(URLName urlname) throws MessagingException {
- return getDefaultFolder().getFolder(urlname.getFile());
- }
- }