java的网络功能非常强大,开发和使用也非常简单,难怪microsoft极力要争回程序语言的霸主地位。根据smtp协议使用java socket写了一个发送邮件的程序,
实现的原理非常简单,首先建立和邮件服务器的socket连接,然后进行和服务器握手,然后发送smtp指令,并封装邮件体,然后发送即可。
import java.net.*;
import java.io.*;
import java.util.*;
public class smtpsender{
socket socket=null;
printwriter outdata=null;
bufferedreader indata=null;
string smtpserver="";
string user="";
string pass="";
string from="";
string linefeed="/r/n";
boolean isneedauthlogin=false;
vector to=new vector();
public static void main(string[] args){
smtpsender smtp=new smtpsender();
smtp.setmailserver("mail.ehawa.com");
smtp.setmailfrom("root@ehawa.com","???","???");
smtp.addmailto("root@ehawa.com");
if(smtp.send("hello","这是一个测试!")){
system.out.println("邮件发送成功!");
}else system.out.println("邮件发送失败!");
}
public void setmailserver(string s){
smtpserver=s;
}
public void setmailfrom(string s,string uid,string pwd){
this.from=s;
this.user=uid;
this.pass=pwd;
this.isneedauthlogin=(this.user!=null&&this.pass!=null&&!this.user.equals("")&&!this.pass.equals(""));
}
public boolean addmailto(string mailaddr){
to.addelement(mailaddr);
return true;
}
public boolean send(string subject,string content){
try{
if(smtpserver==null||smtpserver.equals(""))return false;
if(from==null||from.equals(""))return false;
if(to.size()<1)return false;
socket=new socket(smtpserver,25);
outdata=new printwriter(socket.getoutputstream());
indata=new bufferedreader(new inputstreamreader(socket.getinputstream()));
//与邮件服务器连接成功
readresponse("220");
//helo host
sendrequest("helo "+smtpserver+linefeed);
readresponse("250");
if(isneedauthlogin){
//auth login
sendrequest("auth login"+linefeed);
readresponse("334");
//username:
sendrequest(new string(base64.encodestring(user))+linefeed);
readresponse("334");
//password:
sendrequest(new string(base64.encodestring(pass))+linefeed);
readresponse("235");
}
//mail from:<..>
sendrequest("mail from:<"+from+">"+linefeed);
readresponse("250");
//rcpt to:<..>
for(enumeration enu=to.elements();enu.hasmoreelements();){
string to1=(string)enu.nextelement();
sendrequest("rcpt to:<"+to1+">"+linefeed);
readresponse("250");
}
//data
sendrequest("data"+linefeed);
readresponse("354");
//邮件内容
stringbuffer s1=new stringbuffer("from: <"+from+">"+linefeed);
s1.append("to: <"+to+">"+linefeed);
s1.append("subject: "+subject+linefeed);
s1.append("date: "+new java.util.date().tolocalestring()+linefeed);
s1.append("content-type: text/plain;charset=/"gb2312/""+linefeed);
s1.append(linefeed);
s1.append(content);
s1.append(linefeed+"."+linefeed);//发送
sendrequest(s1.tostring());
readresponse("250");
//quit退出
sendrequest("quit"+linefeed);
readresponse("221");
try{
indata.close();
indata=null;
}catch(exception ex){}
try{
outdata.close();
outdata=null;
}catch(exception ex){}
try{
socket.close();
socket=null;
}catch(exception ex){}
}catch(exception e){
return false;
//e.printstacktrace();
}
return true;
}
private void readresponse(string cmd)throws exception{
string tmp=indata.readline();
if(tmp.startswith(cmd));//system.out.println(" [s:]"+tmp);
else throw new exception("##########邮件发送失败!##########"+tmp);
while(tmp.startswith(cmd+"-"))tmp=indata.readline();
}
private void sendrequest(string msg){
//system.out.print("***[c:]"+msg);
outdata.write(msg);
outdata.flush();
}
public void close(){
try{
indata.close();
indata=null;
}catch(exception ex){}
try{
outdata.close();
outdata=null;
}catch(exception ex){}
try{
socket.close();
socket=null;
}catch(exception ex){}
}
}
闽公网安备 35060202000074号