码迷,mamicode.com
首页 > 编程语言 > 详细

java发送邮件

时间:2019-07-31 11:02:01      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:lsm   main   try   sendmail   mon   final   ssl   imp   eth   

@参考文章1 @参考文章2

 我测试用的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");
            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);
        }
    }
}

 

java发送邮件

标签:lsm   main   try   sendmail   mon   final   ssl   imp   eth   

原文地址:https://www.cnblogs.com/yanan7890/p/11274453.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!