📄 jfmpop3.java
字号:
/* * Created on 2004.08.16 * JFreeMail - Java mail component * Copyright (C) 2004 Dalibor Krleza * * 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.1 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 */ package org.jfreemail.pop3;import org.jfreemail.core.*;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.net.InetAddress;import java.net.Socket;import java.util.Vector;/** * Basic class for reading E-mails from POP3 server. */public class JfmPOP3 { private String __pop_server=null; private InetAddress __pop_server_address=null; private int __pop_server_port=110; private Socket __pop_socket=null; private String __pop_hash=null; private int __pop_auth_type=JfmConsts.POP3_PLAIN; /** * Constructor for pop3 object. Need immediately authentication. So we * are sure to have connection with POP3 server when using object * constructed from this class. * @param hostname Hostname of POP3 server. * @param port POP3 port. Pass 0 for default POP3 port. * @param auth_type Authentication type. See JfmConsts. * @param username Username for authentication. * @param password Password for authentication. * @throws POP3Exception Thrown on protocol error. * @throws AuthException Thrown on authentication error. */ public JfmPOP3(String hostname,int port,int auth_type,String username, String password) throws POP3Exception,AuthException { if (hostname==null) { throw new POP3Exception("POP3_001:No defined hostname"); } else __pop_server=hostname; if (port>0) __pop_server_port=port; if (auth_type>=0) __pop_auth_type=auth_type; try { __pop_socket=new Socket(__pop_server,__pop_server_port); } catch(Exception exc) { throw new POP3Exception("POP3_002:Can't connect to "+__pop_server+":"+String.valueOf(__pop_server_port)+", "+exc.getMessage()); } String in=null; try { in=JfmSocketCore.getLine(__pop_socket)[0]; } catch(IOException exc) { try { __pop_socket.close(); } catch(IOException exc_sub) { } throw new POP3Exception("POP3_100:Communication error"); } if (in.indexOf(JfmConsts.POP3_OK)<0) throw new POP3Exception("POP3_100:Communication error"); int index1=in.indexOf(new String("<")); int index2=in.indexOf(new String(">")); __pop_hash=in.substring(index1,index2+1); JfmAuth au=new JfmAuth(__pop_socket,__pop_auth_type, username,password,__pop_hash); } /** * Constructor for pop3 object. Authentication immediately, so we can * use object when successfully creted. * @param addr IP address of POP3 server * @param port POP3 port. Pass 0 for default POP3 port. * @param auth_type Authetication type. See JfmConsts. * @param username Username for authentication. * @param password Password for authentication. * @throws POP3Exception Thrown on POP3 protocol error. * @throws AuthException Thrown on authentication error. */ public JfmPOP3(InetAddress addr,int port,int auth_type,String username, String password) throws POP3Exception,AuthException { if (addr==null) { throw new POP3Exception("POP3_001:No defined IP address"); } else __pop_server_address=addr; if (port>0) __pop_server_port=port; if (auth_type>=0) __pop_auth_type=auth_type; try { __pop_socket=new Socket(__pop_server_address,__pop_server_port); } catch(Exception exc) { throw new POP3Exception("POP3_002:Can't connect to "+__pop_server_address.getHostAddress()+":"+String.valueOf(__pop_server_port)+", "+exc.getMessage()); } String in=null; try { in=JfmSocketCore.getLine(__pop_socket)[0]; } catch(IOException exc) { try { __pop_socket.close(); } catch(IOException exc_sub) { } throw new POP3Exception("POP3_100:Communication error"); } if (in.indexOf(JfmConsts.POP3_OK)<0) throw new POP3Exception("POP3_100:Communication error"); int index1=in.indexOf(new String("<")); int index2=in.indexOf(new String(">")); __pop_hash=in.substring(index1,index2+1); JfmAuth au=new JfmAuth(__pop_socket,__pop_auth_type, username,password,__pop_hash); } /** * Method for deleting E-mail. You need to pass E-mail's header.<br> * <strong>Warning:</strong> Do not use old E-mail headers from older * POP3 session. Use E-mail haders readed in this session from this * object instance. Do not attempt to change message index in E-mail * header. * @param header Header of E-mail which should be deleted. * @throws POP3Exception Thrown on POP3 protocol error. * @throws CoreException Thrown on comparison error. Unique message * identifier from passed header and header from POP3 server are * compared. If unique message indetifier doesn't match, this exception * is thrown. This is method to prevent deleting wrong E-mail, with wrong * header. */ public void deleteEmail(JfmEmailHeader header) throws POP3Exception,CoreException { try { JfmEmailHeader new_header=getMailboxHeader(header.getMsgNo()); if (!new_header.getMsgId().equals(header.getMsgId())) throw new POP3Exception("POP3_008:Can't delete E-mail. New and old ID doesn't match"); OutputStream os=__pop_socket.getOutputStream(); String out="DELE "+String.valueOf(header.getMsgNo())+"\r\n"; os.write(out.getBytes()); String in=JfmSocketCore.getLine(__pop_socket)[0]; if (in.indexOf(JfmConsts.POP3_OK)<0) throw new POP3Exception("POP3_007:Error deleting E-mail"); } catch(IOException exc) { throw new POP3Exception("POP3_100:Communication error"); } } private JfmEmailHeader getMailboxHeader(int id) throws POP3Exception,CoreException { try { OutputStream os=__pop_socket.getOutputStream(); String out="TOP "+String.valueOf(id)+" 0\r\n"; os.write(out.getBytes()); String[] in_list=JfmSocketCore.getLine(__pop_socket); JfmEmailHeader eh=new JfmEmailHeader(); eh.setMsgNo(id); parseHeader(eh,in_list); return eh; } catch(IOException exc) { throw new POP3Exception("POP3_100:Communication error"); } } /** * Method for retrieving all E-mail headers from POP3 server. This method * reads all E-mail headers stored in POP3 server. You must identify * headers for E-mails you have read before. This could be done with * comparing unique E-mail idnetifier. * @return E-mail header list. * @throws POP3Exception Thrown on communication error. * @throws CoreException Thrown on header parsing error. */ public JfmEmailHeader[] getMailboxHeaders() throws POP3Exception,CoreException { try { OutputStream os=__pop_socket.getOutputStream(); String out="LIST\r\n"; os.write(out.getBytes()); String[] in_list=JfmSocketCore.getLine(__pop_socket); if (in_list[0].indexOf(JfmConsts.POP3_OK)<0) throw new POP3Exception("POP3_005:Error retrieving messages list"); int ind=1; int[] msg_list=new int[in_list.length-2]; for(int i=1;i<(in_list.length-1);i++) { int index=in_list[i].indexOf(" "); msg_list[i-1]=Integer.parseInt(in_list[i].substring(0,index)); } JfmEmailHeader[] eh=new JfmEmailHeader[msg_list.length]; for(int i=0;i<msg_list.length;i++) { eh[i]=getMailboxHeader(msg_list[i]); } return eh; } catch(IOException exc) { throw new POP3Exception("POP3_100:Comunication error"); } } /** * Method for reading E-mail. Use E-mail headers downloaded in this * session, from this object. * @param header E-mail header. * @param delete Delete E-mail on POP3 server after reading. * @return E-mail Java bean. * @throws POP3Exception Thrown on communication error. * @throws CoreException Thrown on E-mail parsing error. */ public JfmEmail getEmail(JfmEmailHeader header,boolean delete) throws POP3Exception,CoreException { if (header==null) throw new POP3Exception("POP3_006:No message header"); try { JfmEmail email=new JfmEmail(header); OutputStream os=__pop_socket.getOutputStream(); String out="RETR "+String.valueOf(header.getMsgNo())+"\r\n"; os.write(out.getBytes()); byte[] mail_buffer=JfmSocketCore.getBinary(__pop_socket); parseMessage(email,mail_buffer); if (delete) { out="DELE "+String.valueOf(header.getMsgNo())+"\r\n"; os.write(out.getBytes()); String in=JfmSocketCore.getLine(__pop_socket)[0]; if (in.indexOf(JfmConsts.POP3_OK)<0) throw new POP3Exception("POP3_007:Error deleting E-mail"); } return email; } catch(IOException exc) { throw new POP3Exception("POP3_100:Communication error"); } } /** * Method for retrieving Mailbox statistics. * @return Java bean with Mailbox statistics. * @throws POP3Exception Thrown on communication error. */ public JfmPOP3Stat getMailboxStats() throws POP3Exception { try { OutputStream os=__pop_socket.getOutputStream(); String out="STAT\r\n"; os.write(out.getBytes()); String in=JfmSocketCore.getLine(__pop_socket)[0]; in=in.substring(0,in.length()-2); if (in.indexOf(JfmConsts.POP3_OK)<0) throw new POP3Exception("POP3_004:Error retrieving mailbox statistics"); int index1=in.indexOf(" "); in=in.substring(index1+1,in.length()); index1=in.indexOf(" "); String in1=in.substring(0,index1); String in2=in.substring(index1+1,in.length()); JfmPOP3Stat stat=new JfmPOP3Stat(); stat.setNoOfMsgs(Integer.parseInt(in1)); stat.setMsgsSize(Long.parseLong(in2)); return stat; } catch(IOException exc) { throw new POP3Exception("POP3_100:Communication error"); } } /** * Closing connection with POP3 server. * @throws POP3Exception Thrown on communication error. */ public void close() throws POP3Exception { try { String out="QUIT\r\n"; OutputStream os=__pop_socket.getOutputStream(); os.write(out.getBytes()); String in=JfmSocketCore.getLine(__pop_socket)[0]; } catch(IOException exc1) { throw new POP3Exception("POP3_100:Communication error"); } } /* * Following method is used to parse E-mail. First checking, and then * calling other methods to parse E-mail with multiple parts or single * part. */ private void parseMessage(JfmEmail email,byte[] buffer) throws CoreException { Vector list=parseBuffer(buffer); if (email.getEmailHeader()==null) throw new CoreException("COR_001:No defined email header"); if (email.getEmailHeader().getContentType()==null) throw new CoreException("COR_001:No defined email content type"); if (email.getEmailHeader().getContentType().toUpperCase().indexOf("MULTIPART")<0) beginParsePlain(email,list); else if (email.getEmailHeader().getContentType().toUpperCase().indexOf("MULTIPART")>=0) beginParseMultipart(email,list,email.getEmailHeader().getContentTypeDelimiter()); else throw new CoreException("COR_004:Unknown email header content type"); } /* * Following method is used to separate single byte array into list of * byte arrays. Separation is performed on CRLF (#13 #10) bytes. * This method is used as precondition for string characterset decoding. */ private Vector parseBuffer(byte[] buffer) { int f1=0; Vector r=new Vector(); for(int i=0;i<buffer.length;i++) { if (i>0 && buffer[i]==10 && buffer[i-1]==13) { byte[] sub=new byte[(i+1)-f1]; for(int j=f1;j<(i+1);j++) sub[j-f1]=buffer[j]; r.add(sub); f1=i+1; } } return r; } /* * Method for parsing E-mail when single text part. */ private void beginParsePlain(JfmEmail email,Vector list) throws CoreException { JfmEmailTextPart part=new JfmEmailTextPart(); part.setContentType(email.getEmailHeader().getContentType()); part.setContentCharset(email.getEmailHeader().getContentCharset()); for(int i=0;i<email.getEmailHeader().getHeaderLength();i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -