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);
        }
    }
}
相关文章
相关标签/搜索