Java发送邮件工具类

Java发送邮件工具类

引入依赖

  <!--发送邮件依赖-->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>

引入工具类

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

//发送邮件工具类
public class EmailUtils implements Runnable {
    private String email;// 收件人邮箱
    private String emailKey;// 激活码
   // private int verificationCode;// 验证码

    public EmailUtils(String email,String emailKey) {
        this.email = email;
       // this.verificationCode=verificationCode;
        this.emailKey = emailKey;
    }

    @Override
    public void run() {
        // 1.创建连接对象javax.mail.Session
        // 2.创建邮件对象 javax.mail.Message
        // 3.发送一封激活邮件
        String from = "1551483075@qq.com";// 发件人电子邮箱,更换即可
        String host = "smtp.qq.com"; // 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易)

        Properties properties;// 获取系统属性
        properties = System.getProperties();

        properties.setProperty("mail.smtp.host", host);// 设置邮件服务器
        properties.setProperty("mail.smtp.auth", "true");// 打开认证

        try {
            //QQ邮箱需要下面这段代码,163邮箱不需要
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);


            // 1.获取默认session对象
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("1551483075@qq.com", "XXX"); // 发件人邮箱账号、更换授权码,去QQ邮箱查看
                }
            });

            // 2.创建邮件对象
            Message message = new MimeMessage(session);
            // 2.1设置发件人
            message.setFrom(new InternetAddress(from));
            // 2.2设置接收人
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
            // 2.3设置邮件主题
            message.setSubject("激活码");
            // 2.4设置邮件内容
            String content = "<html><head></head><body><h1>酒店服务系统</h1><h3>激活码(10分钟内有效):"+emailKey+"</h3></body></html>";
            //点击此链接找回密码:http://localhost:8080/#/findpassword
            message.setContent(content, "text/html;charset=UTF-8");
            // 3.发送邮件
            Transport.send(message);
            // System.out.println("邮件成功发送!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//测试
    public static void main(String[] args) {
        EmailUtils utils = new EmailUtils("1551483075@qq.com",889654);
        utils.run();
    }
}

使用场景

  • 登录&注册接口中可以发送验证码验证
  • 邮箱正确性验证
  • 找回密码发送验证码
  • 邮箱消息推送
  • 发送其他链接
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值