Java SMTP协议电子邮件传送


import java.io.*;
import java.net.NoRouteToHostException;
import java.net.Socket;
import java.util.*;
import javax.naming.NamingException;
import sun.misc.BASE64Encoder;
public class WorkThread extends Thread
{
String domain;
String to;
String subject;
String body;
String myEncode="gb2312";
public String hosts[]={
"google.com","sun.com","borland.com","dell.com","163.com","263.com",
"sina.com","etang.com","sohu.com" };
String from="ddd@"+getRandomDomain();
public void run(){

String mxServers[]=null;
MxFinder finder = new MxFinder(domain,"202.204.208.2");
try
{
mxServers = finder.getMxServers();
}
catch(NamingException e)
{
e.printStackTrace();
};
if(mxServers==null)
{
System.out.println("address is wrong");
}else{
try{
L1:
for(int i=0;i<mxServers.length;i++)
{
String mxServer=mxServers[i];
for(int j=0;j<3;j++)
{
Socket socket=new Socket(mxServer,25);
DataInputStream dis = new DataInputStream(socket.getInputStream());
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
String response = "";
response = readLastLine(dis);
if(!response.startsWith("220"))
{
dis.close();
dos.close();
socket.close();
dis = null;
dos = null;
socket = null;
continue;
} else { //连接成功,开始写入HELO,标识自己身份

dos.writeBytes("HELO "+domain+"\r\n");
response = readLastLine(dis);
if(!response.startsWith("250"))
{ //无法识别此域名,可在此进行相应处理; dis.close();
dos.close();
socket.close();
dis = null;
dos = null;
socket = null;
continue;
} else
{ //SMTP可以识别此域名;写入:MAIL FROM告诉服务器想发一封邮件;

dos.writeBytes("MAIL FROM: "+from+"\r\n");
response = readLastLine(dis);
System.out.println(response);
if(!response.startsWith("250"))
{ //SMTP服务器不打算接收邮件,可在此做相应处理;
System.out.println("failure"); dis.close();
dos.close();
socket.close();
dis = null;
dos = null;
socket = null;
continue;
} else
{ //SMTP服务器准备接收邮件,写入RCPT TO告诉服务器收件人地址;

dos.writeBytes("RCPT TO: <"+to+">\r\n");
response = readLastLine(dis);
if(!response.startsWith("250"))
{ //SMTP不愿意为接收人接收邮件,可在此进行相应处理; dis.close();
dos.close();
socket.close();
dis = null;
dos = null;
socket = null;
continue;
}else
{ //SMTP愿意为接收入接收邮件,写入DATA表明开始邮件传送;

dos.writeBytes("DATA\r\n");
response = readLastLine(dis);
if(!response.startsWith("354"))
{ dis.close();
dos.close();
socket.close();
dis = null;
dos = null;
socket = null;
continue;
} else
{ //SMTP服务器同意开始输入邮件,对邮件体的//From,To,Subject,Data等做相应设置,写入要传送的邮件体//正文:body;

dos.writeBytes("From:< "+getStr(from,myEncode)+">\r\n");
dos.writeBytes("To: <"+getStr(to,myEncode)+">\r\n");
dos.writeBytes("Subject: "+getStr(subject,myEncode)+"\r\n");
dos.writeBytes("Reply-To: <"+from+">\r\n");
dos.writeBytes("mime-version: 1.0\r\n");


dos.writeBytes("Content-Type: text/html;\r\n"); dos.writeBytes("Content-Transfer-Encoding: Base64");
dos.writeBytes("\r\n\r\n");
dos.writeBytes(getBase64(body));
dos.writeBytes("\r\n\r\n"); dos.writeBytes("\r\n.\r\n");
response =readLastLine(dis);
if(!response.startsWith("250"))
{ //发送失败,可在此做相应处理; dis.close();
dos.close();
socket.close();
dis = null;
dos = null;
socket = null;
continue;
} else
{ //发送成功,结束此次传送;

dos.writeBytes("quit\r\n");
dis.close();
dos.close();
socket.close();
dis = null;
dos = null;
socket = null; break L1;
}//sixth else
}//fifth else
}//fourth else
}//third else
} //second else
}//first else
}
}
}catch(Exception e){ e.printStackTrace();}
}
}//run end

private String readLastLine(DataInputStream in) throws IOException
{
String ret = "";
do
{
BufferedReader d= new BufferedReader(new InputStreamReader(in));
ret=d.readLine();
} while(in.available() > 0);
return ret;
}

private String getBase64(String str)
{
return (new BASE64Encoder().encode(str.getBytes()));
}

public static String getStr(String str, String encode)
{
try
{
byte temp[] = str.getBytes(encode);
String tmp = new String(temp, "8859_1");
String s = tmp;
return s;
}
catch(Exception exception)
{
return null;
}
}

private String getRandomDomain()
{
String ret = "";
Random r = new Random(System.currentTimeMillis());
ret = hosts[r.nextInt(hosts.length)];
return ret;
}

public static void main(String[] args)
{
WorkThread wt=new WorkThread();
int pos;
if(args.length<3) System.out.println("请输入您要发送到的邮件地址(如:dddd@163.com)、主题、内容");
else{
wt.to=args[0];
wt.subject=args[1];
wt.body=args[2];
pos=wt.to.indexOf("@");
if(pos==-1) System.out.println("您输入的地址错误,请重新输入");
else { wt.domain=wt.to.substring(pos+1);
wt.start();
}
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值