封裝JavaMail發送郵件

package cc.ccoder.until.mail;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Author    :  chencong
 * Time      :  2017/8/26 11:01
 * Package   :  cc.ccoder.until.mail
 * Describe  :  郵件工具類,直接調用構造方法進行配置目的郵箱地址。
 */
public class MailUtil implements Runnable {
    private String email;// 收件人郵箱
    private String code;// 激活碼
    private String username;  // 顯示用戶名

    public MailUtil(String email,String username, String code) {
        this.email = email;
        this.username = username;
        this.code = code;
    }

    public void run() {
        // 1.創建連接對象javax.mail.Session
        // 2.創建郵件對象 javax.mail.Message
        // 3.發送一封激活郵件
        String from = "[email protected]";// 發件人電子郵箱
        String host = "smtp.qq.com"; // 指定發送郵件的主機smtp.qq.com(QQ)|smtp.163.com(網易)

        Properties properties = System.getProperties();// 獲取系統屬性

        properties.setProperty("mail.smtp.host", host);// 設置郵件服務器
        properties.setProperty("mail.smtp.auth", "true");// 打開認證

        try {
            //QQ郵箱需要下面這段代碼,163郵箱不需要
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);


            // 1.獲取默認session對象
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("[email protected]", "tkzxnbhcznzpbedb"); // 發件人郵箱賬號、授權碼
                }
            });

            // 2.創建郵件對象
            Message message = new MimeMessage(session);
            // 2.1設置發件人
            message.setFrom(new InternetAddress(from));
            // 2.2設置接收人
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
            // 2.3設置郵件主題
            message.setSubject("賬號激活");
            // 2.4設置郵件內容
            String content = "<html><head></head><body><h1>這是一封激活郵件,激活用戶名 " + username + " ,激活請點擊以下鏈接</h1><h3><a href='http://localhost:8080/RegisterDemo/ActiveServlet?code="
                    + code + "'>http://localhost:8080/user/UpdateUserStatusServlet?username=" + username + "&key=" + code
                    + "</href></h3></body></html>";
            message.setContent(content, "text/html;charset=UTF-8");
            // 3.發送郵件
            Transport.send(message);
            System.out.println("郵件成功發送!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //這樣執行
        MailUtil mailUtil = new MailUtil("[email protected]","chencong","12121212");
        mailUtil.run();
        //或者這樣執行
        // 收件人郵箱   收件人用戶名(激活用戶名)  激活碼
        //new MailUtil("[email protected]","聰聰","121212121").run();
    }
}

之前想用qq郵箱發送激活郵件,於是就想到了使用javamail實現這樣的一個功能來完成。詳細代碼都在上面,代碼都有註釋。
還有就是注意自己的郵箱服務器還是授權碼,授權碼在自己的郵箱設置當中查找這裏寫圖片描述

聰聰的獨立博客

聰聰的獨立博客 ,一個喜歡技術,喜歡鑽研的95後。

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