茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1729597 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00006.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2008/3/18 6:51:52
import java.util.Properties;
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();
            //?#93;定SMTP Server
            prop.put("mail.smtp.host",this.SMTP);
            Session mailConn = Session.getDefaultInstance(prop,null);

            Message msg = new MimeMessage(mailConn);

            try{
                //?#93;定信件編碼
                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));
                
                if(this.mailCC.trim().length()!=0){
                    msg.setRecipient(Message.RecipientType.CC,
                            new InternetAddress(this.mailCC));
                }
                
                //?#93;定信件標題
                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));
                    mbpFile.setFileName(fds.getName());
                    mp.addBodyPart(mbpFile);
                }
                
                //將內容?#91;入
                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 = 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;
        }
    }
}
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|