javaMail 发送送邮件,项目中异常告警使用

maven引用
<dependency>
   <groupId>javax.mail</groupId>
   <artifactId>mail</artifactId>
   <version>1.4.7</version>
</dependency>
工具类:
package com.xxx.seller.invoice.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 只提供报警使用 !!!!!
 * 业务发送邮件不可用!!!!!
 */
public class EmailUtils {
    private static final Logger logger = LoggerFactory.getLogger(EmailUtils.class);


    private static String[] emails = {"123@xxx.com","456@xx.com"};
    /**
     * 只提供报警使用 !!!!!
     * 业务发送邮件不可用!!!!!
     */
    public static void sendEmail(String subject,String content){
        // 收件人电子邮箱

        Properties props = new Properties();
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host","smtp.163.com");// smtp服务器地址

        Session session = Session.getInstance(props);
        session.setDebug(false);

        Message msg = new MimeMessage(session);
        try {
            msg.setSubject(subject);
            msg.setText(content);
            //发件人邮箱
            msg.setFrom(new InternetAddress("xxx@163.com"));
            //收件人邮箱,可设置list
            if (emails.length>0){
                InternetAddress[] ts = new InternetAddress[emails.length];
                for (int i = 0; i < emails.length; i++) {
                    String email = emails[i];
                    InternetAddress internetAddress = new InternetAddress(email);
                    ts[i]=internetAddress;
                }


                msg.addRecipients(Message.RecipientType.TO,ts);
                msg.saveChanges();

                Transport transport = session.getTransport();
                //发件人邮箱,授权码(可以在邮箱设置中获取到授权码的信息)
                transport.connect("xxx@163.com","ltz");

                transport.sendMessage(msg, msg.getAllRecipients());

                transport.close();
            }
        } catch (AddressException e) {
            logger.error("接受邮件地址错误",e);
        } catch (MessagingException e) {
            logger.error("发送邮件出现异常",e);
        }
    }


}

subject  :这是一个测试程序....

content : 你好!这是我的第一个javamail程序---WQ

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值