JavaEmail發送網易163郵箱和QQ郵箱

引入javamail的座標依賴!省略!直接展示中心思想!

@Component
public class EmailUtil {
    /**
     * todo 發送QQ郵箱
     */
    public void sendQQEmail(String emailAddress){
        //做鏈接前的準備工作  也就是參數初始化
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.host","smtp.qq.com");//發送郵箱服務器
        properties.setProperty("mail.smtp.port","465");//發送端口
        properties.setProperty("mail.smtp.auth","true");//是否開啓權限控制
        properties.setProperty("mail.debug","true");//true 打印信息到控制檯
        properties.setProperty("mail.transport","smtp");//發送的協議是簡單的郵件傳輸協議
        properties.setProperty("mail.smtp.ssl.enable","true");
        //建立兩點之間的鏈接
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("[email protected]","wbomvmxtifjybcbb");
            }
        });
        //創建郵件對象
        Message message = new MimeMessage(session);
        //設置發件人
        try {
            message.setFrom(new InternetAddress("[email protected]"));

            //設置收件人
            message.setRecipient(Message.RecipientType.TO,new InternetAddress(emailAddress));//收件人
            //設置主題
            message.setSubject("Activiti工作流審覈結果通知");
            //設置郵件正文  第二個參數是郵件發送的類型
            message.setContent("尊敬的用戶您好,您的請假申請已通過,請知悉!","text/html;charset=UTF-8");
            //發送一封郵件
            Transport transport = session.getTransport();
            transport.connect("[email protected]","wbomvmxtifjybcbb");
            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * todo 發送網易163郵箱
     */
    // 發件人 賬號和密碼
    public static final String MY_EMAIL_ACCOUNT = "[email protected]";
    public static final String MY_EMAIL_PASSWORD = "PCRAVAFQNZVIJDOI";// 密碼,是你自己的設置的授權碼

    // SMTP服務器(這裏用的163 SMTP服務器)
    public static final String MEAIL_163_SMTP_HOST = "smtp.163.com";
    public static final String SMTP_163_PORT = "25";// 端口號,這個是163使用到的;QQ的應該是465或者875

    // 收件人
    public static final String RECEIVE_EMAIL_ACCOUNT = "[email protected]";

    @Test
    public void sendWY163Email(String emailAddress) throws Exception{
        Properties p = new Properties();
        p.setProperty("mail.smtp.host", MEAIL_163_SMTP_HOST);
        p.setProperty("mail.smtp.port", SMTP_163_PORT);
        p.setProperty("mail.smtp.socketFactory.port", SMTP_163_PORT);
        p.setProperty("mail.smtp.auth", "true");
        p.setProperty("mail.smtp.socketFactory.class", "SSL_FACTORY");

        Session session = Session.getInstance(p, new Authenticator() {
            // 設置認證賬戶信息
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(MY_EMAIL_ACCOUNT, MY_EMAIL_PASSWORD);
            }
        });
        session.setDebug(true);
        MimeMessage message = new MimeMessage(session);
        // 發件人
        message.setFrom(new InternetAddress(MY_EMAIL_ACCOUNT));
        // 收件人和抄送人
        message.setRecipients(Message.RecipientType.TO, RECEIVE_EMAIL_ACCOUNT);
//		message.setRecipients(Message.RecipientType.CC, MY_EMAIL_ACCOUNT);

        // 內容(這個內容還不能亂寫,有可能會被SMTP拒絕掉;多試幾次吧)
        message.setSubject("Activiti工作流審覈結果通知");
        message.setContent("尊敬的用戶您好,您的請假申請已通過,請知悉!", "text/html;charset=UTF-8");
        message.setSentDate(new Date());
        message.saveChanges();
        Transport.send(message);

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