最近一直在Java的世界裡打轉....沒辦法...學期末的報告總是要交 
自己寫一寫好像還滿無聊的,分享一個透過JavaMail發信的Class ^^...
因為時間很趕,沒有太多時間去規劃程式流程,只能靠著直覺去馬上規劃,馬上寫。
功能還滿陽春的,不過基本的功能都有。處理附加中文檔名目前還有點問題,但是還不曉得怎麼改正,在網路上找到的方法也轉碼不成功,若有更好的方法的話也麻煩提供一下吧~~
程序代碼import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class sendMail {
private String SMTP,mailFrom,mailTo,mailCC,mailTitle,mailBody,mailEncode;
private String userName,userPass;
private Object[] attachFileList;
private boolean isAuthentic = false;
public sendMail(){
this.SMTP = "";
this.mailFrom ="";
this.mailTo = "";
this.mailCC = "";
this.mailTitle = "";
this.mailBody = "";
this.mailEncode = "BIG5";
}
public void setFrom(String from){this.mailFrom = from;}
public void setTo(String to){this.mailTo = to;}
public void setCC(String cc){this.mailCC = cc;}
public void setBody(String body){this.mailBody = body;}
public void setTitle(String title){this.mailTitle = title;}
public void setSMTP(String smtp){this.SMTP = smtp;}
public void setUserName(String userName){this
.userName = userName;}
public void setPassword(String pass){this.userPass = pass;}
public void setAttachFile(Object[] fileListAry){this.attachFileList = fileListAry;}
public void setEncode(String encode){this.mailEncode = encode;}
/*錯誤訊息對應
* 0 = 沒有錯誤
* 1 = 參數不齊全
* 2 = 送信失敗(有驗證)
* 3 = 送信失敗(不驗證)
* 4 = 其他失敗
* */
public int send(boolean hasSMTPAuth){
this.isAuthentic = hasSMTPAuth;
//檢查參數是否齊全
if(!checkParameters(isAuthentic)){
return 1;
}else{
Properties prop = new Properties();
//設定SMTP Server
prop.put("mail.smtp.host",this.SMTP);
Session mailConn = Session.getDefaultInstance(prop,null);
Message msg = new MimeMessage(mailConn);
try{
//設定信件編碼
msg.setHeader("Content-Transfer-Encoding",this.mailEncode);
//將參數修正編碼
reEncode();
//Set Sender e-mail Address and nickname
msg.setFrom(new InternetAddress(this.mailFrom));
//Set reciever e-mail address and nickname
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress(this.mailTo));
&n
bsp;
if(this.mailCC.trim().length()!=0){
msg.setRecipient(Message.RecipientType.CC,
new InternetAddress(this.mailCC));
}
//設定信件標題
if(this.mailTitle.trim().length()==0){
msg.setSubject("[無標題信件]");
}else{
msg.setSubject(this.mailTitle);
}
//attach content with MIME
Multipart mp = new MimeMultipart();
MimeBodyPart mbpBody = new MimeBodyPart();
//mbpBody.setContent(Message,MIME_Type);
mbpBody.setContent(this.mailBody,"text/html; charset=" + this.mailEncode);
//Attach files
MimeBodyPart mbpFile;
for(int i=0;i<attachFileList.length;i++){
String fullPath = this.toBIG5(attachFileList[i].toString());
mbpFile = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fullPath);
mbpFile.setDataHandler(new DataHandler(fds));
&
nbsp; mbpFile.setFileName(fds.getName());
mp.addBodyPart(mbpFile);
}
//將內容加入
mp.addBodyPart(mbpBody);
msg.setContent(mp);
//送信
if(isAuthentic){
prop.put("mail.smtp.auth",isAuthentic);
Transport trans = mailConn.getTransport("smtp");
try{
trans.connect(this.SMTP,this.userName,this.userPass);
trans.sendMessage(msg,msg.getAllRecipients());
return 0;
}catch(Exception ex){
ex.printStackTrace();
return 2;
}finally{
trans.close();
}
}else{
try{
Transport.send(msg);
return 0;
}catch(SendFailedException ex){
System.out.println(ex.toString());
return 3;
}
}
}catch(Exception ex){
ex.printStackTrace();
return 4;
}
}
}
private boolean checkParameters(boolean hasSMTPAuth){
if(SMTP.trim().length()==0 || mailFrom.trim().length()==0
|| mailTo.trim().length()==0)
return false;
else
if(hasSMTPAuth){
if(this.userName.trim().length()==0)
return false;
else
return true;
}else{
return true;
}
}
private void reEncode(){
if(this.mailEncode.toUpperCase()=="BIG5"){
this.mailFrom = toBIG5(mailFrom);
this.mailTo = toBIG5(mailTo);
this.mailTitle = toBIG5(mailTitle);
this.mailBody = toBIG5(mailBody);
}else if(this.mailEncode.toUpperCase()=="UTF8"){
this.mailFrom style="color:#0000ff">= toUTF8(mailFrom);
this.mailTo = toUTF8(mailTo);
this.mailTitle = toUTF8(mailTitle);
this.mailBody = toUTF8(mailBody);
}
}
private String toUTF8(String str){
try {
str = MimeUtility.encodeText(str,"UTF-8", "B");
return str;
} catch (Exception ex){
return str;
}
}
private String toBIG5(String str){
try {
str = MimeUtility.encodeText(str,"Big5", null);
return str;
} catch (Exception ex){
return str;
}
}
}
[/color]
用法很簡單啦!先new一下嘛
程序代碼
然後就開始把該設定的參數都設定進去
程序代碼mail.setMailTo(xxx);
mail.setMailFrom(xxxx);[/color]
最後呢,再使用send()方法把信件發送出去
程序代碼
※send()方法會回傳一個狀態碼,若為"0"的話表示沒有錯誤,其他就看程式裡面的註解啦,俺有註解在上面~
我自知功力尚淺,尚未練就昇陽十八般武藝,所以這個碗糕寫得不是很好,也希望大家可以一起討論吧.....這次...我就獻醜了 

點擊下載此文件