当前位置: 技术问答>java相关
急......收Email问题
来源: 互联网 发布时间:2015-01-08
本文导语: 小弟最近正在写一个收邮件的程序,但是在发送Mail From:时返回的信息是553,好象是需要身份验证吧,这是怎么回事,请教,不胜感激。 | //password,username //都要用base64编码。 //这是我写的一个...
小弟最近正在写一个收邮件的程序,但是在发送Mail From:时返回的信息是553,好象是需要身份验证吧,这是怎么回事,请教,不胜感激。
|
//password,username
//都要用base64编码。
//这是我写的一个乱七八糟的东西。
//你看看,smtp,pop3的基本命令都在了,参考参考。
//base64编码看看下面的就行了。
import java.io.*;
import java.net.*;
import java.util.*;
public class AutoReplyEmail {
public static final int SMTP_PORT =25;
public static final int POP3_PORT =110;
protected static final String CRLF ="n";
protected String _sender_host ="bootcool";
protected String _last_response;
public String username ;
public String password ;
protected StringBuffer _body = new StringBuffer();
protected int mail_numbers;
protected Vector _from = new Vector();
public AutoReplyEmail(String user,String pass) {
username =user;
password =pass;
}
public void CheckFrom(String host)throws IOException,Exception {
String _host = host;
InetAddress r_ip = InetAddress.getByName(_host);
Socket socket =new Socket(r_ip,POP3_PORT);
BufferedReader in =new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter out =new PrintWriter(socket.getOutputStream(),true);
String sender_host = _sender_host;
readAndCheck("+OK", in);
send(out,"HELO "+sender_host);
readAndCheck("+OK", in);
send(out,"USER "+username);
readAndCheck("+OK", in);
send(out,"PASS "+password);
readAndCheck("+OK", in);
send(out,"STAT ");
mail_numbers = checkNumber(in);
Receive(out,in,mail_numbers);
send(out,"QUIT");
socket.close();
}
public void ReplyTo(String host,String file_name,boolean auth)
throws IOException,Exception {
String _host = host;
InetAddress r_ip = InetAddress.getByName(_host);
addBodyFromFile(file_name);
Socket socket =new Socket(r_ip,SMTP_PORT);
BufferedReader in =new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter out =new PrintWriter(socket.getOutputStream(),true);
String sender_host = _sender_host;
readAndCheck("220", in);
send(out,"HELO "+sender_host);
readAndCheck("250", in);
if(auth)
{ send(out,"AUTH LOGIN");
readAndCheck("334", in);
send(out,"base64编码后的用户名");
readAndCheck("334", in);
send(out,"base64编码后的密码");
readAndCheck("235", in);
sendMail(out,in);
}
else sendMail(out,in);
}
private void send (PrintWriter out, String command)
throws IOException{
out.println(command);
}
private int checkNumber(BufferedReader in)throws IOException{
String result = in.readLine();
int endspace =result.lastIndexOf(" ");
int beginspace =result.indexOf(" ");
int number =Integer.parseInt(result.substring(beginspace+1,endspace));
return number;
}
private void Receive(PrintWriter out,BufferedReader in,int num)
throws IOException {
String result ="no_matter_what";
for(int i=1;i
//都要用base64编码。
//这是我写的一个乱七八糟的东西。
//你看看,smtp,pop3的基本命令都在了,参考参考。
//base64编码看看下面的就行了。
import java.io.*;
import java.net.*;
import java.util.*;
public class AutoReplyEmail {
public static final int SMTP_PORT =25;
public static final int POP3_PORT =110;
protected static final String CRLF ="n";
protected String _sender_host ="bootcool";
protected String _last_response;
public String username ;
public String password ;
protected StringBuffer _body = new StringBuffer();
protected int mail_numbers;
protected Vector _from = new Vector();
public AutoReplyEmail(String user,String pass) {
username =user;
password =pass;
}
public void CheckFrom(String host)throws IOException,Exception {
String _host = host;
InetAddress r_ip = InetAddress.getByName(_host);
Socket socket =new Socket(r_ip,POP3_PORT);
BufferedReader in =new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter out =new PrintWriter(socket.getOutputStream(),true);
String sender_host = _sender_host;
readAndCheck("+OK", in);
send(out,"HELO "+sender_host);
readAndCheck("+OK", in);
send(out,"USER "+username);
readAndCheck("+OK", in);
send(out,"PASS "+password);
readAndCheck("+OK", in);
send(out,"STAT ");
mail_numbers = checkNumber(in);
Receive(out,in,mail_numbers);
send(out,"QUIT");
socket.close();
}
public void ReplyTo(String host,String file_name,boolean auth)
throws IOException,Exception {
String _host = host;
InetAddress r_ip = InetAddress.getByName(_host);
addBodyFromFile(file_name);
Socket socket =new Socket(r_ip,SMTP_PORT);
BufferedReader in =new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter out =new PrintWriter(socket.getOutputStream(),true);
String sender_host = _sender_host;
readAndCheck("220", in);
send(out,"HELO "+sender_host);
readAndCheck("250", in);
if(auth)
{ send(out,"AUTH LOGIN");
readAndCheck("334", in);
send(out,"base64编码后的用户名");
readAndCheck("334", in);
send(out,"base64编码后的密码");
readAndCheck("235", in);
sendMail(out,in);
}
else sendMail(out,in);
}
private void send (PrintWriter out, String command)
throws IOException{
out.println(command);
}
private int checkNumber(BufferedReader in)throws IOException{
String result = in.readLine();
int endspace =result.lastIndexOf(" ");
int beginspace =result.indexOf(" ");
int number =Integer.parseInt(result.substring(beginspace+1,endspace));
return number;
}
private void Receive(PrintWriter out,BufferedReader in,int num)
throws IOException {
String result ="no_matter_what";
for(int i=1;i