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();
}
}
}