有发送有接收才显得完整一点。
同样分两部分写:
一:POP3命令介绍(抄的);二:实例。
一:POP3命令介绍
telnet 119.119.119.212 110 ----------------------------- 使用 telnet 命令连接服务器 110 端口
Trying 119.119.119.212... ------------------------------ 正在连接服务器 110 端口
Connected to 119.119.119.212. -------------------------- 连接服务器 110 端口成功
+OK Winmail Mail Server POP3 ready
user username ------------------------------------------ 输入用户名, username 为具体的用户名
+OK ---------------------------------------------------- 执行命令成功
pass password ------------------------------------------ 输入用户密码,password 为具体的密码
+OK 2 messages ----------------------------------------- 密码认证通过
(-ERR authorization failed ----------------------------- 密码认证失败)
stat --------------------------------------------------- 邮箱状态
+OK 2 6415 --------------------------------------------- 2 为该信箱总邮件数,6415 为总字节数
list --------------------------------------------------- 列出每封邮件的字节数
+OK ---------------------------------------------------- 执行命令成功,开始显示,左边为邮件的序号,右边为该邮件的大小
1 537 -------------------------------------------------- 第 1 封邮件,大小为 537 字节
2 5878 ------------------------------------------------- 第 2 封邮件,大小为 5878 字节
.
top 1 -------------------------------------------------- 接收第 1 封邮件
+OK ---------------------------------------------------- 接收成功, 返回第 1 封邮件头
Return-Path: <test1@look.com>
Delivered-To: test2@look.com
Received: (winmail server invoked for smtp delivery); Mon, 25 Oct 2004 14:24:27 +0800
From: test1@look.com
To: test2@look.com
Date: Mon, 25 Oct 2004 14:24:27 +0800
Subject: test mail
.
retr 1 ------------------------------------------------- 接收第 1 封邮件
+OK ---------------------------------------------------- 接收成功, 返回第 1 封邮件全部内容
Return-Path: <test1@look.com>
Delivered-To: test2@look.com
Received: (winmail server invoked for smtp delivery); Mon, 25 Oct 2004 14:24:27 +0800
From: test1@look.com
To: test2@look.com
Date: Mon, 25 Oct 2004 14:24:27 +0800
Subject: test mail
Hi, test2
This is a test mail, you don't reply it.
.
dele 1 ------------------------------------------------- 删除第 1 封邮件
+OK ---------------------------------------------------- 删除成功
dele 2 ------------------------------------------------- 删除第 2 封邮件
+OK ---------------------------------------------------- 删除成功
quit --------------------------------------------------- 结束会话
+OK ---------------------------------------------------- 执行命令成功
二:发送邮件实例
package mail;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.StringTokenizer;
public class POP3Client {
private Socket socket;
private boolean debug=true;
public static void main(String[] args) throws UnknownHostException, IOException {
String server="pop3.126.com";//POP3服务器地址
String user="wasingmon";//用户名
String password="";//密码
POP3Client pop3Client=new POP3Client(server,110);
pop3Client.recieveMail(user,password);
}
public POP3Client(String server,int port) throws UnknownHostException, IOException{
try{
socket=new Socket(server,port);
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("建立连接!");
}
}
//得到服务器返回的一行命令
public String getReturn(BufferedReader in){
String line="";
try{
line=in.readLine();
if(debug){
System.out.println("服务器返回状态:"+line);
}
}catch(Exception e){
e.printStackTrace();
}
return line;
}
//从返回的命令中得到第一个字段,也就是服务器的返回状态码(+OK或者-ERR)
public String getResult(String line){
StringTokenizer st=new StringTokenizer(line," ");
return st.nextToken();
}
//发送命令
private String sendServer(String str,BufferedReader in,BufferedWriter out) throws IOException{
out.write(str);
out.newLine();
out.flush();
if(debug)
{
System.out.println("已发送命令:"+str);
}
return getReturn(in);
}
//user命令
public void user(String user,BufferedReader in,BufferedWriter out) throws IOException{
String result;
result=getResult(getReturn(in));
if(!"+OK".equals(result)){
throw new IOException("连接服务器失败!");
}
result=getResult(sendServer("user "+user,in,out));
if(!"+OK".equals(result)){
throw new IOException("用户名错误!");
}
}
//pass命令
public void pass(String password,BufferedReader in,BufferedWriter out) throws IOException{
String result;
result=getResult(sendServer("pass "+password,in,out));
if(!"+OK".equals(result)){
throw new IOException("密码错误!");
}
}
//stat命令
public int stat(BufferedReader in,BufferedWriter out) throws IOException{
String result;
String line;
int mailNum;
line=sendServer("stat",in,out);
StringTokenizer st=new StringTokenizer(line," ");
result=st.nextToken();
if(st.hasMoreTokens())
mailNum=Integer.parseInt(st.nextToken());
else
mailNum=0;
if(!"+OK".equals(result)){
throw new IOException("查看邮箱状态出错!");
}
System.out.println("共有邮件"+mailNum+"封");
return mailNum;
}
//得到邮件详细信息
public String getMessagedetail(BufferedReader in){
String message="";
String line;
try{
line=in.readLine();
while(!".".equalsIgnoreCase(line)){
message=message+line+"/n";
line=in.readLine();
}
}catch(Exception e){
e.printStackTrace();
}
return message;
}
//retr命令
public void retr(int mailNum,BufferedReader in,BufferedWriter out) throws IOException{
String result;
for(int i=1;i<=mailNum;i++){
result=getResult(sendServer("retr "+i,in,out));
if(!"+OK".equals(result)){
throw new IOException("接收邮件出错!");
}
System.out.println("第"+i+"封");
System.out.println(getMessagedetail(in));
}
}
//退出
public void quit(BufferedReader in,BufferedWriter out) throws IOException{
String result;
result=getResult(sendServer("QUIT",in,out));
if(!"+OK".equals(result)){
throw new IOException("未能正确退出");
}
}
//接收邮件程序
public boolean recieveMail(String user,String password){
try{
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
user(user,in,out);
pass(password,in,out);
int mailNum;
mailNum=stat(in,out);
retr(mailNum,in,out);
quit(in,out);
}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
}