springboot发送邮箱功能

public class MailUtils {

    public static final Logger log = LoggerFactory.getLogger(MailUtils.class);

    /**
     *
     * @param mailFrom  指明邮件的发件⼈
     * @param passwordMailFrom  指明邮件的发件⼈登陆密码
     * @param mailTo    指明邮件的收件⼈
     * @param mailTittle    邮件的标题
     * @param mailText  邮件的⽂本内容
     * @param mailHost 邮件的服务器域名
     * @throws Exception
     */
    public static void sendMail(String mailFrom,String passwordMailFrom,String mailTo,String mailTittle,
                                String mailText,String mailHost,String mailPort,String protocol) throws Exception {
        Properties prop = new Properties();
        Transport ts = null;
        try {

        prop.setProperty("mail.host", mailHost);
        prop.setProperty("mail.properties.mail.smtp.port",mailPort);
        prop.setProperty("mail.transport.protocol", protocol);
        prop.setProperty("mail.default-encoding","UTF-8");
        prop.setProperty("mail.properties.mail.smtp.auth", "true");
        prop.setProperty("mail.smtp.auth",  "true");

        //QQ存在一个特性设置SSL加密
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);

        // 使⽤JavaMail发送邮件的5个步骤
        // 1、创建session

        //创建一个session对象
        Session session = Session.getDefaultInstance(prop, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("发送者邮箱","邮箱密钥");
            }
        });
        // 开启Session的debug模式,这样就可以查看到程序发送Email的运⾏状态
        session.setDebug(true);
        // 2、通过session得到transport对象
        ts = session.getTransport();
        // 3、使⽤邮箱的⽤户名和密码连上邮件服务器,发送邮件时,发件⼈需要提交邮箱的⽤户名和密码给smtp服务器,⽤户名和密码都通过验证之后才能够正常发送邮件
        ts.connect(mailHost,mailFrom, passwordMailFrom);
        // 4、创建邮件
        Message message = createSimpleMail(session,mailFrom,mailTo,mailTittle,mailText);
        // 5、发送邮件
        ts.sendMessage(message, message.getAllRecipients());

        }catch (Exception e)
        {
            log.error("邮件发送异常",e);
        }
        finally {
            if(ts!=null)
            {
                ts.close();;
            }
        }
    }

    /**
     * @Method: createSimpleMail
     * @Description: 创建⼀封只包含⽂本的邮件
     */
    public static MimeMessage createSimpleMail(Session session, String mailfrom, String mailTo, String mailTittle,
                                               String mailText) throws Exception {
        // 创建邮件对象
        MimeMessage message = new MimeMessage(session);
        try {
            // 指明邮件的发件⼈
            message.setFrom(new InternetAddress(mailfrom));
            // 指明邮件的收件⼈,现在发件⼈和收件⼈是⼀样的,那就是⾃⼰给⾃⼰发
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
            // 邮件的标题
            message.setSubject(mailTittle);
            // 邮件的⽂本内容
            message.setContent(mailText, "text/html;charset=UTF-8");
        }catch (Exception e){
            log.error("邮件发送异常",e);
        }
        // 返回创建好的邮件对象
        return message;
    }
    public static void main(String[] args) throws Exception {
        MailUtils.sendMail("发送者邮箱","","接收者邮箱",
                "短信通道告警信息","队列超过","smtp.qq.com","465","smtp");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值