淺談Mail

定義發送郵件API 

public interface SendMail {


    /**
     * @param subject
     * @param message
     * @param recipients
     * @throws MessagingException
     */
    void send(String subject, String message, String... recipients) throws MessagingException, UnsupportedEncodingException;


    /**
     * 向一個人發送郵件(帶多個附件)
     *
     * @param subject
     * @param message
     * @param attachments
     * @param recipients
     * @throws MessagingException
     * @throws UnsupportedEncodingException
     */
    void send(String subject, String message, List<File> attachments, String... recipients) throws MessagingException, UnsupportedEncodingException;
}

發送郵件具體實現 

public class SendMailImpl extends Authenticator implements SendMail {

    private String username;
    private String password;
    //發送郵件的props文件
    private Properties props = System.getProperties();
    //郵箱發送會話
    private Session session;

    /**
     * 帶參數的構造器,初始化郵箱登錄的用戶名和密碼
     *
     * @param username
     * @param password
     */
    public SendMailImpl(String username, String password) {
        this.username = username;
        this.password = password;

        //根據發送郵箱的賬號解析出smtp服務器
        String smtpHostName = "smtp." + username.split("@")[1];
        //初始化props
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", smtpHostName);
        props.put("mail.smtp.ssl.enable", true);
        //發送驗證
        //創建郵箱收發會話的session
        session = Session.getInstance(props, this);
    }

    //重寫該方法,獲得  PasswordAuthentication  對象
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }


    @Override
    public void send(String subject, String message, String... recipients) throws MessagingException, UnsupportedEncodingException {
        this.send(subject,message,null,recipients);
    }

    @Override
    public void send(String subject, String message, List<File> attachments, String... recipients) throws MessagingException, UnsupportedEncodingException {
        //根據session創建MimeMessage
        MimeMessage mimeMessage = new MimeMessage(this.session);
        //發件人
        mimeMessage.setFrom(new InternetAddress(this.username,"趁我們還未老","UTF-8"));
        //收件人們
        InternetAddress[] addresses = new InternetAddress[recipients.length];
        for (int i = 0; i < addresses.length; i++) {
            addresses[i] = new InternetAddress(recipients[i]);
        }
        mimeMessage.setRecipients(MimeMessage.RecipientType.TO, addresses);
        //主題
        mimeMessage.setSubject(subject);

//        msg.setFrom (new InternetAddress ("[email protected]", "這裏是需要的暱稱", "UTF-8"));
        //郵件附件
        if (attachments != null) {
            //郵件正文
            BodyPart contentPart = new MimeBodyPart();
            contentPart.setContent(message, "text/html;charset=utf-8");
            //附件
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(contentPart);
            for (File attachment : attachments) {
                BodyPart attachmentPart = new MimeBodyPart();
                DataSource source = new FileDataSource(attachment);
                attachmentPart.setDataHandler(new DataHandler(source));
                //避免中文亂碼的處理
                attachmentPart.setFileName(MimeUtility.encodeWord(attachment.getName()));
                multipart.addBodyPart(attachmentPart);
            }
            mimeMessage.setContent(multipart);
            //保存郵件
            mimeMessage.saveChanges();
        }else {
            //內容
            mimeMessage.setContent(message, "text/html;charset=utf-8");
        }
        //發送
        Transport.send(mimeMessage);
    }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章