springboot利用hutool工具发送验证码到邮箱以及使用阿里云短信服务发送验证码

springboot利用hutool工具发送验证码到邮箱

1.代码结构
在这里插入图片描述
2.引入依赖

   <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
               <version>5.8.2</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>

3.配置信息application.properties

# 邮件服务器的SMTP地址
# 每个邮件服务器的SMTP地址不同,注意修改,这边以163为例
demo.mail.host=smtp.163.com
#端口默认25
demo.mail.port=25
#发件人
demo.mail.from=sdfsdfdf@163.com
#用户名,默认为发件人邮箱前缀
demo.mail.user=sdfsdfdf@163.com
#密码(注意不是登陆密码,是网易客户端登陆受权码),
#若网站邮箱的POP3/SMTP/IMAP默认已开启,密码可能为登录密码
demo.mail.pwd=LNVSYAMPZSVKXHXX
#收件人
demo.mail.to=sdfsdfdf@163.com

4.业务类MailService

package com.by;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MailService {

    @Value("${demo.mail.host}")
    private String mailHost;

    @Value("${demo.mail.port:25}")
    private Integer mailPort;

    @Value("${demo.mail.from}")
    private String mailFrom;

    @Value("${demo.mail.user}")
    private String mailUser;

    @Value("${demo.mail.pwd}")
    private String mailPwd;

    @Value("${demo.mail.to}")
    private List<String> mailTo;


    public void send() {
        MailAccount account = new MailAccount();
        //邮件服务器的SMTP地址
        //每个邮件服务器的SMTP地址不同,注意修改,这边以163为例
        account.setHost(this.mailHost);
        //端口默认25
        account.setPort(this.mailPort);
        account.setAuth(true);
        //发件人
        account.setFrom(this.mailFrom);
        //用户名,默认为发件人邮箱前缀
        account.setUser(this.mailUser);
        //密码(注意不是登陆密码,是网易客户端登陆受权码),
        // 若网站邮箱的POP3/SMTP/IMAP默认已开启,密码可能为登录密码
        account.setPass(this.mailPwd);

        //随机数
        int c = RandomUtil.randomInt(1000, 9999);
        String body = StrUtil.format("您的验证码为:{}", c);
        //收件人
        MailUtil.send(account, this.mailTo, "验证码", body, false);
    }
}

5.执行代码

package com.by;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class SbEmailApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(SbEmailApplication.class, args);

        MailService bean = ctx.getBean(MailService.class);

        bean.send();
    }

}

6.实现结果截图
在这里插入图片描述

使用阿里云短信服务发送验证码

    <!--阿里云短信服务-->
      <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.5.16</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>2.1.0</version>
        </dependency>
package com.itheima.reggie.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;

/**
 * 短信发送工具类
 */
public class SMSUtils {

	/**
	 * 发送短信
	 * @param signName 签名
	 * @param templateCode 模板
	 * @param phoneNumbers 手机号
	 * @param param 参数
	 */
	public static void sendMessage(String signName, String templateCode,String phoneNumbers,String param){
		DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "", "");
		IAcsClient client = new DefaultAcsClient(profile);

		SendSmsRequest request = new SendSmsRequest();
		request.setSysRegionId("cn-hangzhou");
		request.setPhoneNumbers(phoneNumbers);
		request.setSignName(signName);
		request.setTemplateCode(templateCode);
		request.setTemplateParam("{\"code\":\""+param+"\"}");
		try {
			SendSmsResponse response = client.getAcsResponse(request);
			System.out.println("短信发送成功");
		}catch (ClientException e) {
			e.printStackTrace();
		}
	}

}

要在页面中实现hutool工具类的图形验证码,你需要完成以下步骤: 1. 在你的项目中引入hutool工具类库,可以通过Maven或Gradle等构建工具来实现。 2. 在你的页面中添加一个img标签,用于显示验证码图片。 3. 在后端代码中,使用hutool工具类库生成验证码,并将验证码存储到Session中,以便在验证时使用。 4. 将生成的验证码图片以Base64编码的形式返回给前端页面,以便显示。 下面是一个示例代码,演示了如何在页面中实现hutool工具类的图形验证码: ``` <!-- 页面中添加一个img标签用于显示验证码 --> <img id="captchaImg" src="" /> <!-- 在页面中添加一个按钮,用于刷新验证码 --> <button onclick="refreshCaptcha()">刷新验证码</button> <script> // 刷新验证码 function refreshCaptcha() { // 发送Ajax请求获取新的验证码 $.ajax({ url: "/captcha", type: "get", success: function(data) { // 将Base64编码的验证码图片显示在img标签中 $("#captchaImg").attr("src", "data:image/jpeg;base64," + data); } }); } </script> // 后端代码 // 生成验证码 CaptchaUtil.createLineCaptcha(width, height, codeCount, lineCount); // 将验证码存储到Session中 HttpUtil.getSession().setAttribute("captcha", captcha.getCode()); // 将Base64编码的验证码图片返回给前端页面 return Base64.encode(captcha.getImageBytes()); ``` 注意:以上代码仅为示例,实际使用时需要根据自己的项目情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

great-sun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值