Java發送騰訊企業郵箱郵件,基於ssl協議

在工作中很多時候需要基於代碼發送郵件,最近基於JavaMail寫了一個測試demo
引入依賴,

<dependencies>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.1</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.4</version>
        </dependency>
    </dependencies>

實例代碼

package com.demo;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

public class EMailDemo {

    private static String account = "";	//登錄用戶名
    private static String pass = "";		//登錄密碼
    private static String host = "smtp.exmail.qq.com";		//服務器地址(郵件服務器)
    private static String port = "465";		//端口
    private static String protocol = "smtp"; //協議

    private static MimeMessage mimeMessage;

    /**
    * 用戶名密碼驗證,需要實現抽象類Authenticator的抽象方法PasswordAuthentication,
   * SMTP驗證類(內部類),繼承javax.mail.Authenticator
   */
    static class MyAuthenricator extends Authenticator {
        String username = null;
        String password = null;
        public MyAuthenricator(String username,String password){
            this.username=username;
            this.password=password;
        }
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username,password);
        }
    }

    /**
   * 指定發送郵件
   * @param subject 郵件主題
   * @param sendHtml 郵件內容
   * @param receiveUser 收件人
    */
    public static void sendEmail(String subject,
            String sendHtml, String receiveUser){
        Properties prop = new Properties();
        // 協議
        prop.setProperty("mail.transport.protocol", protocol);
        // 服務器
        prop.setProperty("mail.smtp.host", host);
        // 端口
        prop.setProperty("mail.smtp.port", port);
        // 使用smtp身份驗證
        prop.setProperty("mail.smtp.auth", "true");

        //使用SSL,企業郵箱必需!
        //開啓安全協議
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
        } catch (GeneralSecurityException e1) {
            e1.printStackTrace();
        }

        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);

        Session session = Session.getDefaultInstance(prop, new MyAuthenricator(account, pass));
        // 開啓DEBUG模式,在控制檯中或日誌中有日誌信息顯示,也就是可以從控制檯中看一下服務器的響應信息;
        session.setDebug(true);
        mimeMessage = new MimeMessage(session);

        try {
            //發件人
            mimeMessage.setFrom(new InternetAddress(account));

            //收件人
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(receiveUser));
            //主題
            mimeMessage.setSubject(subject);
            //時間
            mimeMessage.setSentDate(new Date());

            //僅僅發送文本
            mimeMessage.setText(sendHtml,"UTF-8");
            mimeMessage.saveChanges();
            Transport.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        sendEmail("test", "測試", "[email protected]");
    }
}

在測試類添加收件人和發件人以及發件人密碼就可以發送郵件了.

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