import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
/**
* @author zhaohongbing
*
*/
@SuppressWarnings("unused")
public class SockerMail {
String mailServer;
String from;
String to;
String subject;
String content;
String lineFeet = "\r\n";
private int port = 25;
Socket client;
BufferedReader bf;
DataOutputStream dos;
public String getContent(){
return content;
}
public void setContent(String content){
this.content = content;
}
public String getMailServer(){
return mailServer;
}
public void setMailServer(String mailServer){
this.mailServer = mailServer;
}
public String getFrom(){
return from;
}
public void setFrom(String from){
this.from = from;
}
public String getTo(){
return to;
}
public void setTo(String to){
this.to = to;
}
public String getSubject(){
return subject;
}
public void setSubject(String sub){
this.subject = sub;
}
/**
* 初始化连接
* @return
*/
private boolean init(){
System.out.println("init be invoke");
boolean boo = true;
if(mailServer == null || "".equals(mailServer)){
return false;
}
try{
client = new Socket(mailServer, port);
bf = new BufferedReader(new InputStreamReader(client.getInputStream()));
dos = new DataOutputStream(client.getOutputStream());
String isConnect = getResponse();
if(isConnect.startsWith("220")){
}else{
System.out.println("建立连接失败: "+isConnect);
boo = false;
}
}catch(UnknownHostException e){
System.out.println("建立连接失败!");
e.printStackTrace();
boo = false;
}catch(IOException e){
System.out.println("读取流数据失败!");
e.printStackTrace();
boo = false;
}
System.out.println("init result = " +boo);
return boo;
}
/**
* 发送smtp指令
* 并返回服务器响应信息
* @param msg
* @return
*/
private String sendCommand(String msg){
String result = null;
try{
dos.writeBytes(msg);
dos.flush();
result = getResponse();
}catch(IOException e){
e.printStackTrace();
}
return result;
}
/**
* 读取服务器端响应信息
* @return
*/
private String getResponse(){
String result = null;
try{
result = bf.readLine();
}catch(IOException e){
e.printStackTrace();
}
return result;
}
/**
* 关闭
*/
private void close(){
try{
dos.close();
bf.close();
client.close();
}catch(IOException e){
e.printStackTrace();
}
}
public boolean sendMail(){
//初始化
if(client == null){
if(init()){
}else{
return false;
}
}
//判断 from, to
if(from == null || from.isEmpty() || to == null || to.isEmpty()){
return false;
}
//进行握手
String result = sendCommand("HELO "+mailServer +lineFeet);
if(isStartWith(result, "250")){
System.out.println("握手结果:"+true);
}else{
System.out.println("握手失败:"+result);
return false;
}
//验证发信人信息
// String auth = sendCommand("AUTH LOGIN"+lineFeet);
// if(isStartWith(auth,"334")){
// System.out.println("验证发信人信息结果:"+true);
// }else{
// return false;
// }
// String user = sendCommand(new String(Base64.encode("anszhao@163.com".getBytes()))+lineFeet);
// System.out.println("user = " +user);
// if(isStartWith(user, "334")){
// System.out.println("验证user信息结果:"+true);
// }else{
// return false;
// }
// String pass = sendCommand(new String(Base64.encode("".getBytes()))+lineFeet);
// System.out.println("pass = " +pass);
// if(isStartWith(pass, "235")){
// System.out.println("验证pass信息结果:"+true);
// }else{
// System.out.println("验证pass信息结果:"+false);
// return false;
// }
//发送指令
String f = sendCommand("Mail From:<"+from+">"+lineFeet);
System.out.println("发送指令结果:"+f);
if(isStartWith(f,"250")){
System.out.println("发送指令结果:"+true);
}else{
System.out.println("发送指令结果:"+false);
return false;
}
String toStr = sendCommand("RCPT TO:<"+to+">"+lineFeet);
System.out.println("验证toStr结果:"+toStr);
if(isStartWith(toStr,"250")){
System.out.println("验证toStr结果:"+true);
}else{
return false;
}
String data = sendCommand("DATA"+lineFeet);
if(isStartWith(data,"354")){
System.out.println("验证data信息结果:"+true);
}else{
return false;
}
StringBuilder sb = new StringBuilder();
sb.append("From:<"+from+">"+lineFeet);
sb.append("To:<"+to+">"+lineFeet);
sb.append("Subject:" +subject+lineFeet);
sb.append("Date:2014/06/27 17:30"+lineFeet);
sb.append("Content-Type:text/plain;charset=\"GB2312\",\"UTF-8\""+lineFeet);
sb.append(lineFeet);
sb.append(content);
sb.append(lineFeet+"."+lineFeet);
String conStr = sendCommand(sb.toString());
if(isStartWith(conStr,"250")){
System.out.println("验证conStr信息结果:"+true);
}else{
return false;
}
//quit
String quit = sendCommand("QUIT"+lineFeet);
if(isStartWith(quit,"221")){
System.out.println("验证quit信息结果:"+true);
}else{
return false;
}
close();
return true;
}
/**
*
* 检查字符串开头
*/
private boolean isStartWith(String res, String with){
return res.startsWith(with);
}
/**
* @param args
*/
public static void main(String[] args) {
SockerMail mail = new SockerMail();
mail.setMailServer("stmp.mail.163.com");
mail.setFrom("anson@163.com");
mail.setTo("anson@163.com");
mail.setSubject("[Test Email]");
mail.setContent("Hello,this is a test mail, please replay me if you have receviced it");
boolean boo = mail.sendMail();
if(boo){
System.out.println("邮件发送成功");
}else{
System.out.println("邮件发送失败");
}
}
}
Java通过socket实现smtp协议发送邮件,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/ansonz/p/3816343.html