📄 myconnection.java
字号:
package maildemo;/** * Title: * Description: * Copyright: Copyright (c) 2001 * Company: * @author * @version 1.0 */import java.io.*;import java.net.*;public class MyConnection{ private Socket socket; private BufferedReader in; private PrintWriter out; private boolean m_bConnected; private String m_sResponse; private int m_nState=NONE; private String m_sFrom=""; private String m_sTo=""; private String m_sContent=""; private String m_sTitle=""; private int m_nTotalMessages=-1; private int m_nTotalOctets = -1; //未连接 private static final int NONE = 0; //已经验证 private static final int TRANSACTION = 1; public static final int DEFAULT_PORT = 110; public MyConnection() { } public boolean open(String host) throws IOException { return open(host,DEFAULT_PORT); } public boolean open(String host, int port) throws IOException { socket = new Socket(host, port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); m_bConnected = true; m_sResponse = in.readLine(); if(m_sResponse.startsWith("+OK")) { return true; } else { socket.close(); return false; } } private void sendCommand(String s) throws IOException { out.print(s + "\r\n"); out.flush(); } private boolean getResponse() throws IOException { m_sResponse = in.readLine(); return (m_sResponse.startsWith("+OK")); } public boolean userPass(String user, String pass) throws IOException { sendCommand("USER " + user); if (getResponse()) { sendCommand("PASS " + pass); if (getResponse()) { m_nState = TRANSACTION; return true; } } return false; } public boolean noOp() throws IOException { sendCommand("NOOP"); return getResponse(); } /**取得一共有多少个邮件**/ public boolean getMailStatus() throws IOException { m_nTotalMessages =-1; m_nTotalOctets = -1; sendCommand("STAT"); if (getResponse()) { String s=m_sResponse.substring(4); try { System.out.println(s); m_nTotalMessages =Integer.parseInt(s.substring(0, s.indexOf(' '))); s=s.substring(s.indexOf(' ')+1); s+=" "; s = s.substring(0,s.indexOf(' ')); m_nTotalOctets =Integer.parseInt(s); return true; } catch(Exception e){e.printStackTrace();return false;} } else { return false; } } /**取得第几个邮件的内容 * @return 是否成功取得 * 内容存在response里头 */ public boolean retrieve(int message) throws IOException { m_sContent=""; sendCommand("RETR " + message); if(getResponse()) { String sContent=""; boolean bRun=true; //读发信人 do { sContent=readLine(); if(sContent==null)return false; }while(!sContent.startsWith("From:")); if(sContent.startsWith("From:")) { m_sFrom=sContent.substring(6); } //读收信人 do { sContent=readLine(); if(sContent==null)return false; }while(!sContent.startsWith("To:")); if(sContent.startsWith("To:")) { m_sTo=sContent.substring(4); } //标题 do { sContent=readLine(); if(sContent==null)return false; }while(!sContent.startsWith("Subject:")); if(sContent.startsWith("Subject:")) { m_sTitle=sContent.substring(9); } while(bRun) { sContent=readLine(); if(sContent==null)return true; m_sContent+=sContent+"<br>"; } } return true; } /**用来取得邮件的内容**/ private String readLine() throws IOException { String s = in.readLine(); if (s.length() > 0) { if (s.charAt(0) == '.') { if (s.length() > 1) { s = s.substring(1); } else { s = null; } } } return s; } public String getFrom() { return m_sFrom; } public String getTo() { return m_sTo; } public String getContent() { return m_sContent; } public String getTitle() { return m_sTitle; } /**getMailStatus()**/ public int getMailNum() { return m_nTotalMessages; } public static void main(String[] args) { MyConnection myConnection1 = new MyConnection(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -