要实现邮箱的发送需要一个已知账号和密码的邮箱并且开通了STMPPOP服务常见主流邮箱邮件收取和发送popstmp服务配置汇总介绍一文中详细介绍了各个邮箱开通服务的步骤
邮件发送是利用与邮件发送服务器建立的Socket连接通过这个连接获得输入输出流再利用流在程序中输入特定的命令行来实现邮件的发送
下面是程序步骤
与邮件发送服务器建立器Socket连接(需要端口号和服务器名称)
用HELO命令注册到服务器上
用AUTH LOGIN命令根据已有的邮箱账号和密码登陆到服务器
用MAIL FROM<>命令设置邮件源地址信息
用MAIL TO<>命令设置邮件目的地址信息
用DATAFROMTOSUBJECT命令分别设置发件人昵称收件人昵称主题信息和内容+回车结束内容输入
最后用QUIT命令退出邮件发送服务
下面是程序代码
定义一个接口主要是上面步骤方法的声明
SendMailjava:
package comsunfloweryuansendmail;import javaioIOException;/** * @author Caihanyuan * @time 上午:: */public interface SendMail {
/**
* 向服务器发送命令
*
* @param order
*
向服务器发送的命令行
* @return 返回服务器状态
* */
public int sendToServer(String order)
/**
* 解析服务器返回的应答信息
*
* @return 返回服务器状态
* */
public int getResult()
/** 与服务器发起会话 */
public void regist() throws IOException;
/** 登录到服务器 */
public void login() throws IOException;
/** 设置邮件源地址 */
public void setMailFrom() throws IOException;
/** 设置邮件目的地址 */
public void setMailTo() throws IOException;
/** 设置邮件内容 */
public void setData() throws IOException;
/** 退出邮箱服务 */
public void quit() throws IOException;
/** 发送邮件的主程序 */
public boolean sendMail() throws IOException;}
接口的实现类
SendMailImpjava:
package comsunfloweryuansendmail;import javaioBufferedReader;import javaioBufferedWriter;import javaioIOException;import javaioInputStreamReader;import javaioOutputStreamWriter;import Socket;import UnknownHostException;import javautilStringTokenizer;import sunmiscBASEEncoder;/** * @author Caihanyuan * @time 上午:: */public class SendMailImp implements SendMail {
public static final int SINA = ;
public static final int QQ = ;
public static final int FOXMAIL = ;
public static final int WANGYI = ;
public static final int WANGYI = ;
public static final int YEAH = ;
public static final int HOTMAIL = ;
public static final int YAHOO = ;
public static final int GMAIL = ;
/** 用于向服务器发起连接 */
private Socket socket = null;
/** 与服务器连接后的输入流 */
private BufferedReader input = null;
/** 与服务器连接后的输出流 */
private BufferedWriter output = null;
/** 发送邮件服务器名称 */
private String server = null;
/** 发送端口号 */
private int port;
/** 邮件信息 */
private MailMessage message;
public MailMessage getMessage() {
return message;
}
//设置邮件的信息内容以方法链的方式实现
public SendMailImp setMessage(MailMessage message) {
ssage = message;
return this;
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
thissocket = socket;
}
public BufferedReader getInput() {
return input;
}
public void setInput(BufferedReader input) {
thisinput = input;
}
public BufferedWriter getOutput() {
return output;
}
public void setOutput(BufferedWriter output) {
thisoutput = output;
}
public String getServer() {
return server;
}
public void setServer(String server) {
thisserver = server;
}
public int getPort() {
return port;
}
public void setPort(int port) {
thisport = port;
}
/**
* 根据邮箱类型设置发送邮件服务器和端口号
*/
public static SendMailImp getInstance(int sendMailType) {
SendMailImp sendMailImp = new SendMailImp()
switch (sendMailType) {
case SINA:
sendMailImpsetServer()
sendMailImpsetPort()
break;
case QQ:
case FOXMAIL:
sendMailImpsetServer()
sendMailImpsetPort()
break;
case WANGYI:
sendMailImpsetServer()
sendMailImpsetPort()
break;
case WANGYI:
sendMailImpsetServer()
sendMailImpsetPort()
break;
case YEAH:
sendMailImpsetServer()
sendMailImpsetPort()
break;
case YAHOO:
sendMailImpsetServer()
sendMailImpsetPort()
break;
case GMAIL:
sendMailImpsetServer()
sendMailImpsetPort()
break;
case HOTMAIL:
sendMailImpsetServer()
sendMailImpsetPort()
break;
default:
break;
}
try {
// 与发送邮件服务器建立Socket连接并得到输入输出流
Socket socket = new Socket(sendMailImpgetServer() sendMailImp
getPort())
BufferedReader input = new BufferedReader(new InputStreamReader(
socketgetInputStream()))
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(
socketgetOutputStream()))
sendMailImpsetSocket(socket)
sendMailImpsetInput(input)
sendMailImpsetOutput(output)
}
catch (UnknownHostException e) {
eprintStackTrace()
}
catch (IOException e) {
eprintStackTrace()
}
return sendMailImp;
}
public int sendToServer(String order) {
try {
thisgetOutput()write(order)
thisgetOutput()newLine()
thisgetOutput()flush()
Systemoutprintln(已发送命令 + order)
}
catch (IOException e) {
eprintStackTrace()
}
return getResult()
}
public int getResult() {
String line = ;
try {
line = thisgetInput()readLine()
Systemoutprintln(服务器返回状态 + line)
}
catch (IOException e) {
eprintStackTrace()
}
// 从服务器返回消息中取得状态码并转换成整数返回
StringTokenizer get = new StringTokenizer(line )
return IntegerparseInt(getnextToken())
}
public void regist() throws IOException {
int result = getResult()
// 连接上服务器后服务器给出应答
if (result != )
throw new IOException(连接服务器失败)
result = sendToServer(HELO + thisgetServer())
// HELO命令成功后服务器返回应答
if (result != )
throw new IOException(注册邮件服务器失败)
}
public void login() throws IOException {
BASEEncoder encode = new BASEEncoder()
int result = sendToServer(AUTH LOGIN)
// 登陆服务成功服务器返回应答
if (result != )
throw new IOException(登陆服务失败)
// 对邮箱用户名进行验证
result = sendToServer(encodeencode(ssagegetUser()getBytes()))
if (result != )
throw new IOException(用户名错误)
// 对邮箱密码进行验证
result = sendToServer(encodeencode(messagegetPassword()getBytes()))
if (result != )
throw new IOException(用户验证错误)
}
public void setMailFrom() throws IOException {
int result = sendToServer(MAIL FROM:< + thisgetMessage()getFrom()
+ >)
if (result != )
throw new IOException(邮件源地址错误)
}
public void setMailTo() throws IOException {
int result = sendToServer(RCPT TO:< + thisgetMessage()getTo() + >)
if (result != )
throw new IOException(邮件目的地址错误)
}
public void setData() throws IOException {
int result = sendToServer(DATA)
// 输入date回车后若收到应答后继续输入邮件内容
if (result != )
throw new IOException(不能发送数据)
thisgetOutput()write(FROM: + thisgetMessage()getDatafrom())
thisgetOutput()newLine()
thisgetOutput()write(TO: + thisgetMessage()getDatato())
thisgetOutput()newLine()
thisgetOutput()write(SUBJECT: + thisgetMessage()getSubject())
thisgetOutput()newLine()
thisgetOutput()newLine()
thisgetOutput()write(thisgetMessage()getContent())
thisgetOutput()newLine()
// 句点加回车结束邮件内容输入
result = sendToServer()
if (result != )
throw new IOException(发送数据错误)
}
public void quit() throws IOException {
int result = sendToServer(QUIT)
if (result != )
throw new IOException(未能正确退出)
thisgetInput()close()
thisgetOutput()close()
}
public boolean sendMail() {
boolean success = true;
try {
// 注册到发送邮件服务器
regist()
// 登陆到服务器
login()
// 设置邮件源地址
setMailFrom()
// 设置邮件目的地址
setMailTo()
// 设置邮件体
setData()
// 退出邮件
quit()
}
catch (Exception e) {
eprintStackTrace()
success = false;
}
return success;
}}
邮件信息的实体类
MailMessagejava:
package comsunfloweryuansendmail;/** * @author Caihanyuan * @time 下午:: */public class MailMessage {
// 发件人
private String from;
// 收件人
private String to;
// 发件人在邮件中显示
private String datafrom;
// 收件人在邮件中显示
private String datato;
// 主题
private String subject;
// 内容
private String content;
// 日期
private String date;
// 发送邮箱用户名邮箱名的前缀
private String user;
// 发送邮箱密码
private String password;
public String getFrom() {
return from;
}
public void setFrom(String from) {
thisfrom = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
thisto = to;
}
public String getDatafrom() {
return datafrom;
}
public void setDatafrom(String datafrom) {
thisdatafrom = datafrom;
}
public String getDatato() {
return datato;
}
public void setDatato(String datato) {
thisdatato = datato;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
thissubject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
ntent = content;
}
public String getDate() {
return date;
}
public void setDate(String date) {
thisdate = date;
}
public String getUser() {
return user;
}
public void setUser(String user) {
thisuser = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
thispassword = password;
}}
测试类
Testjava:
package comsunfloweryuantest;import javaioIOException;import comsunfloweryuansendmailMailMessage;import comsunfloweryuansendmailSendMail;import comsunfloweryuansendmailSendMailImp;/** * @author Caihanyuan * @time 下午:: */public class Test {
public static void main(String[] args) {
MailMessage message = new MailMessage()
messagesetFrom(caih****)
messagesetTo(*****)
messagesetSubject(这个是一个邮件发送测试)
messagesetUser(caih****)
messagesetContent(Hellothis is a mail send test\n你好这是一个邮件发送测试)
messagesetDatafrom(caih****)
messagesetDatato(****)
messagesetPassword(*****)
SendMail send = SendMailImpgetInstance(SendMailImpWANGYI)setMessage(message)
try {
sendsendMail()
}
catch (IOException e) {
eprintStackTrace()
}
}}
要注意的是setUser()里面的字符信息是邮件源地址的前缀
程序运行效果