springboot发送邮件

0.使用前提:发送邮箱开通STMP

邮箱设置-账户-开启STMP-记录下密码串 

 

 

一、引入依赖

        <!-- https://mvnrepository.com/artifact/javax.mail/mail -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.activation/activation -->
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>

二、设置SendEmail工具类


// 网站三秒原则
public class SendEmail extends Thread {
    // 发送邮件的邮箱
    private String from = "470070654@qq.com";
    // 邮箱用户名
    private String username = "470070654@qq.com";
    // 授权码
    private String password = "你的邮箱密钥";
    // 发送邮件的服务器地址
    private String host = "smtp.qq.com";

    public String recipient1;
//    private User user;
//
//    public SendMail(User user) {
//        this.user = user;
//    }
    public SendEmail( ) {
    }

    public void run() {
        try {
            Properties prop = new Properties();
            prop.setProperty("mail.host", host); // 设置qq邮件服务器
            prop.setProperty("mail.transport.protocol", "smtp"); // 邮件发送协议
            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 对象
            // 这一步是qq邮箱才有, 其他邮箱不用
            Session session = Session.getDefaultInstance(prop, new Authenticator() {
                @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    // 发件人邮箱 用户名和授权码
                    return new PasswordAuthentication(username, password);
                }
            });

            // 开启 Session debugger 模式, 可以看到邮件发送的运行状态
            session.setDebug(true);

            // 2. 通过 Session得到 transport 对象
            Transport transport = session.getTransport();

            // 3. 使用邮箱用户名和授权码连上邮件服务器 (登陆)
            transport.connect(host, username, password);

            // 4. 创建邮件: 写邮件
            MimeMessage message = new MimeMessage(session);


            // ======== 写邮件 ========
            // 设置邮件的发件人
            message.setFrom(new InternetAddress(from));
            // 设置邮件的收件人(单个收件人)
//            message.setRecipient(Message.RecipientType.TO,new InternetAddress("cxmfly@163.com"));

            //设置多个收件人
            // recipient1 ="470070654@qq.com ,cxmfly@163.com";
            String[] recipientList = recipient1.split(",");
            InternetAddress[] recipientAddress = new InternetAddress[recipientList.length];
            int counter = 0;
            for (String recipient : recipientList) {
                recipientAddress[counter] = new InternetAddress(recipient.trim());
                counter++;
            }
            message.setRecipients(Message.RecipientType.TO, recipientAddress);

            // 邮件标题
            message.setSubject("用户注册通知");
            // 邮件文本内容
            String content = "<div>" +
                    "<span>恭喜你注册成功,你的用户名是:" +

                    ",你的密码是:" +

                    ",请妥善保管。</span>" +
                    "<a href='https://www.tmall.com/' target='_blank'>" +
                    "<img width='100%' src='https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2672619668,2196295127&fm=26&gp=0.jpg' />" +
                    "</a>" +
                    "<h4 style='color: red'>This email is sent from Java, 本邮件由Java程序发送, from to 33344466</h4>" +
                    "</div>";
            message.setContent(content, "text/html; charset=UTF-8");
            // ======== 写邮件 ========


            // 5. 发送邮件
            transport.sendMessage(message, message.getAllRecipients());

            // 关闭连接
            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
}

三、使用

public class test {
    public static void main(String[] args) {
        SendEmail sendEmail = new SendEmail();
        //设置多个收件人
        sendEmail.recipient1 ="470070654@qq.com ,cxmfly@163.com";
        sendEmail.start();

    }
}

参考:https://blog.youkuaiyun.com/weixin_44953227/article/details/112601787

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值