邮件发送与使用thymeleaf引擎重置密码邮件

邮件发送

原生java-mail进行邮件发送; 前提:先登录邮箱,开启POP3/SMTP服务,使第三方可以使用授权码登录邮箱。

  @Test
    public void sendEmail(){
        String account="19008239181@163.com";
        String pwd="KXNZHOZDMLTVWHOZ";
        //设置SMTP请求头
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp");// 连接协议
        properties.put("mail.smtp.host", "smtp.163.com");// QQ smtp.qq.com
        properties.put("mail.smtp.port", 465);// 默认端口号 25
        properties.put("mail.smtp.auth", "true");//服务端认证。
        properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
        properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息

        String to="chengdujavasm@126.com";
        //会话对象
        Session s = Session.getDefaultInstance(properties);
        try {
            Message msg = new MimeMessage(s);
            msg.setFrom(new InternetAddress(account));
            msg.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
            msg.addRecipient(Message.RecipientType.CC,new InternetAddress(account));
            msg.setSubject("今日新闻");
            msg.setText("飓风来袭");

            //邮差对象
            Transport transport = s.getTransport();
            transport.connect(account,pwd);
            transport.sendMessage(msg,msg.getAllRecipients());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

spring封装的spring-mail组件; 项目开发中我们选择sprng中封装的mail组件,使用简单方便。

  1. 添加mail启动器

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
  2. yml中配置登录邮箱,授权码,端口,ssl等。

    spring:  
      mail:
        protocol: smtp
        host: smtp.163.com
        port: 465
        username: 1993
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值