//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);
Socket socket =new Socket(r_ip,SMTP_PORT); BufferedReader in =new BufferedReader( new InputStreamReader(socket.getInputStream())); PrintWriter out =new PrintWriter(socket.getOutputStream(),true);
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<=num;i++){ send(out,"RETR "+i); while(result.indexOf(CRLF+"."+CRLF) == -1){ result = in.readLine(); if(result.startsWith("From: ")){ int begin = result.indexOf("<"); int end = result.lastIndexOf(">"); String temps = result.substring(begin+1,end); _from.addElement(temps); if(i<num){send(out,"DELE "+i); break; } else {send(out,"DELE "+i); return; } } } } }private void sendMail(PrintWriter out,BufferedReader in) throws IOException { String mail_address; send(out,"MAIL FROM: "+"[email protected]"); readAndCheck("250",in); for(int i=1;i<=mail_numbers;i++){ mail_address =(String)_from.elementAt(i-1); send(out, "RCPT TO: "+mail_address); readAndCheck("250",in); } send(out,"DATA "); readAndCheck("354",in); send(out,"Subject: "+"hi"); send(out,"From: "+"[email protected]"); send(out,"\n"); out.write(CRLF + _body.toString()); send(out,CRLF+"."+CRLF); send(out,"QUIT"); readAndCheck("221",in); }public void addBodyFromFile (String filename)throws IOException { File f = new File(filename); if (!f.exists())throw new FileNotFoundException(); if (!f.canRead())throw new SecurityException("file not readable"); BufferedReader in = new BufferedReader(new FileReader(f)); String line; while ((line = in.readLine()) != null) { _body.append(line + "\n"); } in.close(); }private void readAndCheck (String expected, BufferedReader in) throws IOException{ _last_response = in.readLine(); if(_last_response.startsWith(expected)) System.out.println(_last_response); else ; }public static void main(String[] args){ AutoReplyEmail test = new AutoReplyEmail("用户名","密码"); try{test.CheckFrom("pop.163.net"); test.ReplyTo("smtp.163.net","d:/test.txt",true); } catch(Exception e){ } } } ///// import java.io.*; public class Base64Encode { public Base64Encode(){ } public static void main(String args[]){ String userInfo = "用户名"; String encoding = new sun.misc.BASE64Encoder().encode(userInfo.getBytes()); String userInfo2 = "密码"; System.out.println(encoding); String encoding2 = new sun.misc.BASE64Encoder().encode (userInfo2.getBytes()); System.out.println(encoding2); } }
换个还不需要验证的吧,如21cn.com,要么就改一下程序,不过我还没搞清怎么改。
//都要用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<=num;i++){
send(out,"RETR "+i);
while(result.indexOf(CRLF+"."+CRLF) == -1){
result = in.readLine();
if(result.startsWith("From: ")){
int begin = result.indexOf("<");
int end = result.lastIndexOf(">");
String temps = result.substring(begin+1,end);
_from.addElement(temps);
if(i<num){send(out,"DELE "+i);
break;
}
else {send(out,"DELE "+i);
return;
}
}
}
}
}private void sendMail(PrintWriter out,BufferedReader in)
throws IOException {
String mail_address;
send(out,"MAIL FROM: "+"[email protected]");
readAndCheck("250",in);
for(int i=1;i<=mail_numbers;i++){
mail_address =(String)_from.elementAt(i-1);
send(out, "RCPT TO: "+mail_address);
readAndCheck("250",in);
}
send(out,"DATA ");
readAndCheck("354",in);
send(out,"Subject: "+"hi");
send(out,"From: "+"[email protected]");
send(out,"\n");
out.write(CRLF + _body.toString());
send(out,CRLF+"."+CRLF);
send(out,"QUIT");
readAndCheck("221",in);
}public void addBodyFromFile (String filename)throws IOException {
File f = new File(filename);
if (!f.exists())throw new FileNotFoundException();
if (!f.canRead())throw new SecurityException("file not readable");
BufferedReader in = new BufferedReader(new FileReader(f));
String line;
while ((line = in.readLine()) != null) {
_body.append(line + "\n");
}
in.close();
}private void readAndCheck (String expected, BufferedReader in)
throws IOException{
_last_response = in.readLine();
if(_last_response.startsWith(expected))
System.out.println(_last_response);
else ;
}public static void main(String[] args){
AutoReplyEmail test = new AutoReplyEmail("用户名","密码");
try{test.CheckFrom("pop.163.net");
test.ReplyTo("smtp.163.net","d:/test.txt",true);
}
catch(Exception e){
}
}
}
/////
import java.io.*;
public class Base64Encode {
public Base64Encode(){
}
public static void main(String args[]){
String userInfo = "用户名";
String encoding = new sun.misc.BASE64Encoder().encode(userInfo.getBytes());
String userInfo2 = "密码";
System.out.println(encoding);
String encoding2 = new sun.misc.BASE64Encoder().encode (userInfo2.getBytes());
System.out.println(encoding2);
}
}
代码都给你了,全都告诉你了,这么清楚。