要實現郵箱的發送
郵件發送是利用與郵件發送服務器建立的Socket連接
下面是程序步驟
與郵件發送服務器建立器Socket連接(需要端口號和服務器名稱)
用
用
用
用
用
最後用
下面是程序代碼
定義一個接口
SendMail
package com
/**
* 向服務器發送命令
*
* @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;}
接口的實現類
SendMailImp
package com
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) {
this
}
public BufferedReader getInput() {
return input;
}
public void setInput(BufferedReader input) {
this
}
public BufferedWriter getOutput() {
return output;
}
public void setOutput(BufferedWriter output) {
this
}
public String getServer() {
return server;
}
public void setServer(String server) {
this
}
public int getPort() {
return port;
}
public void setPort(int port) {
this
}
/**
* 根據郵箱類型
*/
public static SendMailImp getInstance(int sendMailType) {
SendMailImp sendMailImp = new SendMailImp()
switch (sendMailType) {
case SINA:
sendMailImp
sendMailImp
break;
case QQ:
case FOXMAIL:
sendMailImp
sendMailImp
break;
case WANGYI
sendMailImp
sendMailImp
break;
case WANGYI
sendMailImp
sendMailImp
break;
case YEAH:
sendMailImp
sendMailImp
break;
case YAHOO:
sendMailImp
sendMailImp
break;
case GMAIL:
sendMailImp
sendMailImp
break;
case HOTMAIL:
sendMailImp
sendMailImp
break;
default:
break;
}
try {
// 與發送郵件服務器建立Socket連接
Socket socket = new Socket(sendMailImp
BufferedReader input = new BufferedReader(new InputStreamReader(
socket
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(
socket
sendMailImp
sendMailImp
sendMailImp
}
catch (UnknownHostException e) {
e
}
catch (IOException e) {
e
}
return sendMailImp;
}
public int sendToServer(String order) {
try {
this
this
this
System
}
catch (IOException e) {
e
}
return getResult()
}
public int getResult() {
String line =
try {
line = this
System
}
catch (IOException e) {
e
}
// 從服務器返回消息中取得狀態碼
StringTokenizer get = new StringTokenizer(line
return Integer
}
public void regist() throws IOException {
int result = getResult()
// 連接上服務器後
if (result !=
throw new IOException(
result = sendToServer(
// HELO命令成功後
if (result !=
throw new IOException(
}
public void login() throws IOException {
BASE
int result = sendToServer(
// 登陸服務成功
if (result !=
throw new IOException(
// 對郵箱用戶名進行驗證
result = sendToServer(encode
if (result !=
throw new IOException(
// 對郵箱密碼進行驗證
result = sendToServer(encode
if (result !=
throw new IOException(
}
public void setMailFrom() throws IOException {
int result = sendToServer(
+
if (result !=
throw new IOException(
}
public void setMailTo() throws IOException {
int result = sendToServer(
if (result !=
throw new IOException(
}
public void setData() throws IOException {
int result = sendToServer(
// 輸入date回車後
if (result !=
throw new IOException(
this
this
this
this
this
this
this
this
this
// 句點加回車結束郵件內容輸入
result = sendToServer(
if (result !=
throw new IOException(
}
public void quit() throws IOException {
int result = sendToServer(
if (result !=
throw new IOException(
this
this
}
public boolean sendMail() {
boolean success = true;
try {
// 注冊到發送郵件服務器
regist()
// 登陸到服務器
login()
// 設置郵件源地址
setMailFrom()
// 設置郵件目的地址
setMailTo()
// 設置郵件體
setData()
// 退出郵件
quit()
}
catch (Exception e) {
e
success = false;
}
return success;
}}
郵件信息的實體類
MailMessage
package com
// 發件人
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) {
this
}
public String getTo() {
return to;
}
public void setTo(String to) {
this
}
public String getDatafrom() {
return datafrom;
}
public void setDatafrom(String datafrom) {
this
}
public String getDatato() {
return datato;
}
public void setDatato(String datato) {
this
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this
}
public String getContent() {
return content;
}
public void setContent(String content) {
ntent = content;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this
}
public String getUser() {
return user;
}
public void setUser(String user) {
this
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this
}}
測試類
Test
package com
public static void main(String[] args) {
MailMessage message = new MailMessage()
message
message
message
message
message
message
message
message
SendMail send = SendMailImp
try {
send
}
catch (IOException e) {
e
}
}}
要注意的是setUser()裡面的字符信息是郵件源地址的前綴
程序運行效果
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25888.html