国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java中javamail發送帶附件的郵件實現方法

2019-11-26 15:19:48
字體:
來源:轉載
供稿:網友

本文實例講述了java中javamail發送帶附件的郵件實現方法。分享給大家供大家參考。具體分析如下:

JavaMail,顧名思義,提供給開發者處理電子郵件相關的編程接口。它是Sun發布的用來處理email的API。它可以方便地執行一些常用的郵件傳輸,JavaMail是可選包,因此如果需要使用的話你需要首先從java官網上下載。目前最新版本是JavaMail1.5.0,下面我們來看看javamail發送帶附件的郵件實例

mail.java 代碼:

復制代碼 代碼如下:
package mail; 
 
import java.util.* ; 
import java.io.* ; 
import javax.mail.* ; 
import javax.mail.internet.* ; 
import javax.activation.* ; 
public class Mail { 
    //定義發件人、收件人、SMTP服務器、用戶名、密碼、主題、內容等 
    private String displayName; 
    private String to; 
    private String from; 
    private String smtpServer; 
    private String username; 
    private String password; 
    private String subject; 
    private String content; 
    private boolean ifAuth; //服務器是否要身份認證 
    private String filename=""; 
    private Vector file = new Vector(); //用于保存發送附件的文件名的集合
    
    /**
     * 設置SMTP服務器地址
     */ 
    public void setSmtpServer(String smtpServer){ 
        this.smtpServer=smtpServer; 
    } 
    
    /**
     * 設置發件人的地址
     */ 
    public void setFrom(String from){ 
        this.from=from; 
    } 
    /**
     * 設置顯示的名稱
     */ 
    public void setDisplayName(String displayName){ 
        this.displayName=displayName; 
    } 
    
    /**
     * 設置服務器是否需要身份認證
     */ 
    public void setIfAuth(boolean ifAuth){ 
        this.ifAuth=ifAuth; 
    } 
    
    /**
     * 設置E-mail用戶名
     */ 
    public void setUserName(String username){ 
        this.username=username; 
    } 
    
    /**
     * 設置E-mail密碼
     */ 
    public void setPassword(String password){ 
        this.password=password; 
    } 
    
    /**
     * 設置接收者
     */ 
    public void setTo(String to){ 
        this.to=to; 
    } 
    
    /**
     * 設置主題
     */ 
    public void setSubject(String subject){ 
        this.subject=subject; 
    } 
    
    /**
     * 設置主體內容
     */ 
    public void setContent(String content){ 
        this.content=content; 
    } 
    
    /**
     * 該方法用于收集附件名
     */ 
    public void addAttachfile(String fname){ 
        file.addElement(fname); 
    } 
    
    public Mail(){ 
        
    } 
    
    /**
     * 初始化SMTP服務器地址、發送者E-mail地址、用戶名、密碼、接收者、主題、內容
     */ 
    public Mail(String smtpServer,String from,String displayName,String username,String password,String to,String subject,String content){ 
        this.smtpServer=smtpServer; 
        this.from=from; 
        this.displayName=displayName; 
        this.ifAuth=true; 
        this.username=username; 
        this.password=password; 
        this.to=to; 
        this.subject=subject; 
        this.content=content; 
    } 
    
    /**
     * 初始化SMTP服務器地址、發送者E-mail地址、接收者、主題、內容
     */ 
    public Mail(String smtpServer,String from,String displayName,String to,String subject,String content){ 
        this.smtpServer=smtpServer; 
        this.from=from; 
        this.displayName=displayName; 
        this.ifAuth=false; 
        this.to=to; 
        this.subject=subject; 
        this.content=content; 
    } 
 
    /**
     * 發送郵件
     */ 
    public HashMap send(){ 
        HashMap map=new HashMap(); 
        map.put("state", "success"); 
        String message="郵件發送成功!"; 
        Session session=null; 
        Properties props = System.getProperties(); 
        props.put("mail.smtp.host", smtpServer); 
        if(ifAuth){ //服務器需要身份認證 
            props.put("mail.smtp.auth","true");    
            SmtpAuth smtpAuth=new SmtpAuth(username,password); 
            session=Session.getDefaultInstance(props, smtpAuth);  
        }else{ 
            props.put("mail.smtp.auth","false"); 
            session=Session.getDefaultInstance(props, null); 
        } 
        session.setDebug(true); 
        Transport trans = null;   
        try { 
            Message msg = new MimeMessage(session);  
            try{ 
                Address from_address = new InternetAddress(from, displayName); 
                msg.setFrom(from_address); 
            }catch(java.io.UnsupportedEncodingException e){ 
                e.printStackTrace(); 
            } 
            InternetAddress[] address={new InternetAddress(to)}; 
            msg.setRecipients(Message.RecipientType.TO,address); 
            msg.setSubject(subject); 
            Multipart mp = new MimeMultipart(); 
            MimeBodyPart mbp = new MimeBodyPart(); 
            mbp.setContent(content.toString(), "text/html;charset=gb2312"); 
            mp.addBodyPart(mbp);   
            if(!file.isEmpty()){//有附件 
                Enumeration efile=file.elements(); 
                while(efile.hasMoreElements()){  
                    mbp=new MimeBodyPart(); 
                    filename=efile.nextElement().toString(); //選擇出每一個附件名 
                    FileDataSource fds=new FileDataSource(filename); //得到數據源 
                    mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart 
                    mbp.setFileName(fds.getName());  //得到文件名同樣至入BodyPart 
                    mp.addBodyPart(mbp); 
                }   
                file.removeAllElements();     
            }  
            msg.setContent(mp); //Multipart加入到信件 
            msg.setSentDate(new Date());     //設置信件頭的發送日期 
            //發送信件 
            msg.saveChanges();  
            trans = session.getTransport("smtp"); 
            trans.connect(smtpServer, username, password); 
            trans.sendMessage(msg, msg.getAllRecipients()); 
            trans.close(); 
            
        }catch(AuthenticationFailedException e){    
             map.put("state", "failed"); 
             message="郵件發送失敗!錯誤原因:/n"+"身份驗證錯誤!"; 
             e.printStackTrace();  
        }catch (MessagingException e) { 
             message="郵件發送失敗!錯誤原因:/n"+e.getMessage(); 
             map.put("state", "failed"); 
             e.printStackTrace(); 
             Exception ex = null; 
             if ((ex = e.getNextException()) != null) { 
                 System.out.println(ex.toString()); 
                 ex.printStackTrace(); 
             }  
        } 
        //System.out.println("/n提示信息:"+message); 
        map.put("message", message); 
        return map; 
    } 
}

SmtpAuth.java 代碼:
復制代碼 代碼如下:
package mail; 
 
public class SmtpAuth extends javax.mail.Authenticator {  
    private String username,password;  
 
    public SmtpAuth(String username,String password){  
        this.username = username;   
        this.password = password;   
    }  
    protected javax.mail.PasswordAuthentication getPasswordAuthentication() {  
        return new javax.mail.PasswordAuthentication(username,password);
    }  
}

存在的問題就是發送到163的郵件全部都帶有一個附件的符號,不管有沒有發送附件,感興趣的朋友可以對此加以改進和完善。

希望本文所述對大家的Java程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 滨海县| 乌兰浩特市| 巢湖市| 苍山县| 新兴县| 绍兴县| 汝城县| 永善县| 南涧| 江华| 聂荣县| 屯门区| 宁南县| 乐至县| 体育| 罗田县| 蒙自县| 垦利县| 伽师县| 克山县| 黑河市| 宜君县| 长武县| 安塞县| 温州市| 泾川县| 柘荣县| 宝坻区| 延津县| 屯留县| 本溪市| 泸西县| 巨鹿县| 玉山县| 惠州市| 东方市| 长治县| 慈利县| 元谋县| 隆化县| 东至县|