java發送郵件

@參考文章1 @參考文章2apache

 我測試用的QQ,須要到QQ郵箱裏開通pop3服務安全

 

 

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public class SendMail {

    private static final String MAIL_CC = "xxx@qq.com";// 郵件的抄送人
    private static final String MAIL_BCC = "xxx@qq.com";// 郵件的密送人
    // 設置郵件服務器
    private static final String send_email_host_name = "smtp.qq.com";
    // 設置安全鏈接端口號
    private static final String send_email_ssl_port = "465";
    // 發送Email用戶名
    private static final String send_email_user_name = "xxx@qq.com";
    // 發送Email密碼。即上面開通的pop3的受權碼
    private static final String send_email_user_pwd = "xxx";
    // 發送Email編碼
    private static final String send_email_charset = "UTF-8";

    public static void main(String[] args) throws Exception {
        sendPlainEmail(send_email_user_name, "這是一個subject", "this is content");
    }

    /**
     * @todo 發送純文本的Email
     * @param receiver 收信人
     * @param subjecct Email主題
     * @param msg Email內容
     * @throws EmailException
     * @date 2019年7月31日
     * @author yanan
     */
    public static void sendPlainEmail(String receiver, String subjecct, String msg) throws EmailException {
        SimpleEmail email = new SimpleEmail();
        email.setHostName(send_email_host_name);
        email.setSSLOnConnect(true);// 設置使用安全鏈接
        email.setSslSmtpPort(send_email_ssl_port);
        email.setAuthentication(send_email_user_name, send_email_user_pwd);
        try {
            email.addTo(receiver, "receiver");//評論區有猿討論羣發接口,其實直接在這裏屢次addTo增長收信人就好了
            email.setCharset(send_email_charset);
            email.setFrom(send_email_user_name);
            email.addBcc(MAIL_BCC);
            email.addCc(MAIL_CC);
            email.setSubject(subjecct);
            email.setMsg(msg);
            email.send();
        } catch (EmailException e) {
            throw new EmailException(e);
        }
    }
}
相關文章
相關標籤/搜索