java POP3

java

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);


}

}


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

以上是 java POP3 的全部内容, 来源链接: utcz.com/z/392197.html

回到顶部