package com.skyzoo.Jutil;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintStream;

import java.net.Socket;

import java.util.Vector;

public class Jpop3 {

// 用户名

private String username = "";

// 密码

private String password = "";

// Pop服务器名

private String pop3Host = "";

// 设置标志位

public Vector recvMails = new Vector();

private boolean delete = true;

private boolean secure = false;

public void setPop3Host(String pop3Host) throws Exception
{

this.pop3Host = pop3Host.trim();

};

public void setUser(String username, String password) throws
Exception {

this.username = username.trim();

this.password = password.trim();

};

private void log(String log) {

System.out.println(log);

}

public void setDelete(boolean delete) {

this.delete = delete;

}

public static void main(String argv[]) {

Jpop3 jpop3 = new Jpop3();

try {

jpop3.setPop3Host("pop.ym.163.com");

jpop3.setUser("saicmotor@mynms.cn", "abcd1234");

jpop3.setDelete(false);

jpop3.recvin();

} catch (Exception e) {

e.printStackTrace();

}

}

public boolean recvin() {

BufferedReader in; // 从套接字中读取文本

PrintStream out; // 将文本写入套接字

Socket s;

String line;

String numberMessages;

try {

if (pop3Host.equals("")) {

return false;

}

if (username.equals("")) {

return false;

}

log("Connecting to " + pop3Host + " ...");

// Pop3服务默认使用110端口

// 建立TCP连接

s = new Socket(pop3Host, 110);

log("Connected\nAuthentication in progress...");

// 连接建立成功,获得关联的输入流和输出流

in = new BufferedReader(new
InputStreamReader(s.getInputStream()));

out = new PrintStream(s.getOutputStream());

// 着一行响应是服务器的欢迎信息,可抛弃

line = readFromInputStream(in);

// 发送用户名

printToOutputStream(out, "USER " + username);

// 如果用户不存在,则响应信息开头有+OK字样。否则退出

if (!readFromInputStream(in).startsWith("+OK")) {

log("Wrong user name, disconnecting");

s.close();

return false;

}

// 用户名存在,则发送密码

out.println("PASS " + password);

log("R: PASS ********");

// 读服务器响应信息

line = readFromInputStream(in);

// 如果密码核对成功,响应信息中开头+OK字样

if (!line.startsWith("+OK")) {

// 密码不正确,无法继续

log("Wrong password, disconnecting");

s.close();

return false;

}

// 命令STAT

printToOutputStream(out, "STAT");

// 读取响应

line = readFromInputStream(in);

if (!line.startsWith("+OK")) {

// 如果命令执行失败,则退出

System.out.println("Error:" + line);

s.close();

return false;

}

// 从统计信息中抽取消息个数

int i = line.lastIndexOf(' ');

numberMessages = line.substring(4, i);

log("You have " + numberMessages + " message(s) in your
mailbox");

// 获取消息个数

int n = Integer.parseInt(numberMessages);

int numberBytes;

// 取出服务器上的每一个消息

for (int msg = 1; msg <= n; msg++) {

mail_t one_mail = new mail_t();

log("Retreaving message " + msg);

// 命令RETR 用于收取消息

printToOutputStream(out, "RETR " + msg);

// 从服务器读取消息

line = readFromInputStream(in);

if (!line.startsWith("+OK")) {

System.out.println("Error: " + line);

s.close();

return false;

}

// 消息已“.”号结束,一行中连续两个"."代表"."

log(line);

line = "";

log("**************---------------------------");

line = in.readLine();

while (line.compareTo(".") != 0) {

if (line.compareTo("..") == 0)

log(".");

else {

log(line);

processLine(one_mail, line);

log("---------------------------**************");

}

// 读取下一行

line = in.readLine();

}

// 消息已读取到本地,从服务器删除消息

// 删除消息的命令是DELE [messagenumber]

if (this.delete == true) {

printToOutputStream(out, "DELE " + msg);

readFromInputStream(in);

}

one_mail.echo();

recvMails.add(one_mail);

}

// 准备退出,使用QUIT命令

printToOutputStream(out, "QUIT");

readFromInputStream(in);

// 关闭套接字

s.close();

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

return true;

}

void processLine(mail_t one_mail, String line) {

line = line.trim();

if (line.startsWith("Received:")) {

one_mail.Received = line.substring(9).trim();

} else if (line.startsWith("Date:")) {

one_mail.Date = line.substring(5).trim();

} else if (line.startsWith("From:")) {

one_mail.From = line.substring(5).trim();

} else if (line.startsWith("To:")) {

one_mail.To = line.substring(3).trim();

} else if (line.startsWith("Message-ID:")) {

} else if (line.startsWith("Subject:")) {

one_mail.Subject = line.substring(8).trim();

} else if (line.startsWith("MIME-Version:")) {

} else if (line.startsWith("Content-Type:")) {

}

}

// 方法printToOutputStream()向SMTP服务器发送命令

void printToOutputStream(PrintStream out, String s) throws
IOException {

log("S : " + s);

out.println(s);

return;

}

// 方法readFromInputStream()接收从SMTP服务器发回的响应信息

String readFromInputStream(BufferedReader in) throws
IOException {

String s = in.readLine();

if (s != null)

log("R :" + s);

return s;

}

public boolean isContainSubject(String subject){

for(int i=0;i

if(this.recvMails.get(i).Subject.equals(subject)){

return true;

}

}

return false;

}

}

class mail_t {

String Received = "";

String Date = "";

String From = "";

String To = "";

String Subject = "";

String Text = "";

String Html = "";

String all = "";

public void echo() {

String line = "";

line += "Received==='" + Received + "'\n";

line += "Date==='" + Date + "'\n";

line += "From==='" + From + "'\n";

line += "To==='" + To + "'\n";

line += "Subject==='" + Subject + "'\n";

System.out.println(line);

}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

最新文章

  1. php随机生成验证码代码
  2. ckplayer.js视频播放插件
  3. 1021 玛丽卡 - Wikioi
  4. new[]上面居然有一个内存计数,怪不得delete[]从来不出错
  5. ssh非交互式密码输入
  6. Google的Protocol Buffer格式分析
  7. Linux上夏时令的应对
  8. 改善C#公共程序类库质量的10种方法和工具
  9. python学习之爬虫(一) ——————爬取网易云歌词
  10. 《团队-手机app便签-开发文档》
  11. MongoDB知识点拾遗梳理
  12. C# 结构体和List&lt;T&gt;类型数据转Json数据保存和读取
  13. 牛客练习赛35-背单词-线性DP
  14. 将VSCode添加到右键
  15. Spring Boot Admin 日志查看功能
  16. mybatis源码解析2---SqlSessionFactoryBuilder、SqlSessionFactory解析
  17. 页面适应电脑和手机屏幕initial-scale 1:0 user-scalable=yes
  18. Django 之单个mysql表使用
  19. Scala 学习笔记(2)之类和对象
  20. Class与Style绑定

热门文章

  1. ios -- 延迟3秒触发performSelector
  2. win本地配置docker环境
  3. SPOJ LCS2 - Longest Common Substring II 后缀自动机 多个串的LCS
  4. Intellij IDEA创建的Web项目配置Tomcat并启动Maven项目(转)
  5. Jeff Dean 排序时间计算
  6. mysqldump的--master-data参数
  7. &quot;静态方法里仅仅能调用静态变量和静态方法&quot;具体解释
  8. 设置ubuntu默认输入python进入python3
  9. ZOJ - 4016 Mergeable Stack 【LIST】
  10. joomla搬家之后打不开 首页404错误