javasocket實现SMTP协议发送邮件.doc
文本预览下载声明
java socket 实现SMTP协议 发送邮件
文章分类:Java编程
package com.socket.test;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import .Socket;
import .UnknownHostException;
import ernal.impl.dv.util.Base64;
/**
* 通过socket向smtp协议服务器发送邮件
* @author fuyanqing
*
*/
public class SocketMail {
String mailServer;
String from;
String to;
String content;
String lineFeet = \r\n;
private int port = 25;
Socket client;
BufferedReader in;
DataOutputStream os;
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;
}
/**
* 初始化连接
* @return
*/
private boolean init(){
boolean boo = true;
if(mailServer==null || .equals(mailServer)){
return false;
}
try {
client = new Socket(mailServer,port);
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
os = new DataOutputStream(client.getOutputStream());
String isConnect = response();
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;
}
return boo;
}
/**
* 发送smtp指令
* 并返回服务器响应信息
*/
private String sendCommand(String msg){
String result = null;
try {
os.writeBytes(msg);
os.flush();
result = response();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* 读取服务器端响应信息
* @return
*/
private String response(){
String result = null;
try {
result = in.readLine();
显示全部