使用 JavaMailSenderImpl SSL 465 发送邮件

package org.apache.log4j;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

/***
 * @Author 淡漠Vip
 *
 * @Email itdreamlmc@163.com
 *
 * @Date Create in 17:29 2017/9/11 0011
 */
public class EmailUtils {

    private static JavaMailSenderImpl javaMailSender;

    private static final String userName = "xxxxxxxxxxx";

    private static final String password = "xxxxxxxxxxx";

    private static final String host = "smtp.exmail.qq.com";

    private static final int port = 465;

    //定义收件人列表
    private static final String[] revicedUserName = {"xxxxx", "xxxx@qq.com"};

    private static final Logger logger = Logger.getLogger(EmailUtils.class);

    static {
        javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost(host);//链接服务器
        javaMailSender.setPort(port);
        javaMailSender.setUsername(userName);//账号
        javaMailSender.setPassword(password);//密码
        javaMailSender.setDefaultEncoding("UTF-8");

        Properties properties = new Properties();
        properties.setProperty("mail.smtp.auth", "true");//开启认证
        properties.setProperty("mail.debug", "true");//启用调试
        properties.setProperty("mail.smtp.timeout", "1000");//设置链接超时
        properties.setProperty("mail.smtp.port", Integer.toString(port));//设置端口
        properties.setProperty("mail.smtp.socketFactory.port", Integer.toString(port));//设置ssl端口
        properties.setProperty("mail.smtp.socketFactory.fallback", "false");
        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        javaMailSender.setJavaMailProperties(properties);
    }

    /***
     * 发送项目异常 代码提醒
     * @param msg
     */
    public static void sendEmail(String msg) {
        //开启线程异步发送  防止发送请求时间过长
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (revicedUserName != null && revicedUserName.length > 0) {
                    logger.info("===================开始发送异常提醒邮件================");
                    SimpleMailMessage mailMessage = new SimpleMailMessage();
                    mailMessage.setFrom(userName);
                    mailMessage.setSubject("====后台管理项目异常====");
                    mailMessage.setText(msg);
                    mailMessage.setTo(revicedUserName);
                    //发送邮件
                    javaMailSender.send(mailMessage);
                    logger.info("==================结束发送异常提醒邮件================");
                }
            }
        }).start();
    }


    public static void main(String[] args) {
        sendEmail("代码开始抽风报警了------");
    }
}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值