发送短信验证码
阿里云发送验证码
public Integer sendTelCode(String tel) {
String url = "https://dfsns.market.alicloudapi.com/data/send_sms";
String appcode = "a3198282fbdf443d97aa9f3cfbe1232e";
int code = RandomUtil.randomInt(1000,10000);
emailMap.put(tel,code);
String result = HttpRequest.post(url)
.header("Authorization","APPCODE "+appcode)
.body("content=code:"+code+"&template_id=TPL_0000&phone_number="+tel)
.execute().body();
JSONObject object = JSONUtil.parseObj(result);
if (!object.get("status").equals("OK")){
log.error("发送验证码错误:{}",object.get("reason"));
throw new BizException(404,"发送验证码错误");
}
return 0;
}
发送邮件功能
pom.xml 引入hutool和javax.mail
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
2.书写代码
首先你的邮箱要开启SMTP服务.
书写代码:
public Integer sendCode(String email) {
//MailAccount 邮件账户对象
MailAccount account = new MailAccount();
account.setHost("smtp.aliyun.com");//阿里云的邮箱服务器地址
account.setPort(25); //邮件服务器端口,默认25
account.setAuth(true);
account.setUser("3813@aliyun.com");
account.setFrom("3813@aliyun.com");
account.setPass("这里是自己的邮箱密码");
int code = RandomUtil.randomInt(1000,10000);//RandomUtil:随机工具类
emailMap.put(email,code);
//MailUtil:邮件的工具类
MailUtil.send(account,email,"WMS验证码","验证码:"+code,false);
return 0;
}