公司网站开发说明介绍公司网站
目录
- 引出
- springboot整合email
- 配置邮箱
- 导入依赖
- application.yml配置
- email业务类
- 测试类
- springboot整合阿里云短信服务
- 申请阿里云短信服务
- 测试短信服务
- 获取阿里云的accessKey
- springboot整合阿里云短信
- 导包
- 工具类
- 总结
引出
1.springboot整合email,qq邮箱,特点免费;
2.springboot整合阿里短信服务,100条免费;
3.后续应用,可以用在登陆业务上,比如邮箱登陆,短信登陆;
springboot整合email
配置邮箱
登录邮箱服务器: 登录QQ邮箱
后面要用到这个授权码key
导入依赖
<!-- qq邮箱--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>
application.yml配置
email业务类
接口
package com.tianju.auth.service;public interface IEmailService {/*** 发送右键* @param to 邮件接收方* @param subject 邮件主题* @param content 邮件内容*/void sendEmail(String to,String subject,String content);
}
实现
package com.tianju.auth.service.impl;import com.tianju.auth.service.IEmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.Date;@Service
@Slf4j
public class EmailServiceImpl implements IEmailService {@Value("${spring.mail.username}")private String from;@Resourceprivate JavaMailSender javaMailSender;@Overridepublic void sendEmail(String to, String subject, String content) {SimpleMailMessage mailMessage = new SimpleMailMessage();mailMessage.setSubject(subject);mailMessage.setTo(to);mailMessage.setText(content);mailMessage.setSentDate(new Date());mailMessage.setFrom(from);javaMailSender.send(mailMessage);log.debug("在{}发送一条邮件{}给{}",mailMessage.getSentDate(),mailMessage.getText(),mailMessage.getTo());}
}
测试类
package com.tianju.auth.service.impl;import com.tianju.auth.service.IEmailService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import javax.xml.ws.soap.Addressing;import java.util.UUID;import static org.junit.Assert.*;@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class EmailServiceImplTest {@Autowiredprivate IEmailService emailService;@Testpublic void sendEmail() {emailService.sendEmail("xxxx@qq.com", "我是老王,我在测试代码", UUID.randomUUID().toString());}
}
springboot整合阿里云短信服务
申请阿里云短信服务
短信服务 (aliyun.com)
测试短信服务
进行测试
调用结果
成功接收短信
获取阿里云的accessKey
springboot整合阿里云短信
导包
<!-- 阿里云短信验证码相关包--><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.5.3</version></dependency>
工具类
package com.tianju.auth.util;import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import net.minidev.json.JSONObject;import java.util.HashMap;
import java.util.Map;
import java.util.Random;public class SMSUtil {private static String AccessIdKey = "获取的keyID";private static String AccessKeySecret = "获取的KeySecret";public static void send(String tel,String code) {DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",AccessIdKey, //AccessIdKeyAccessKeySecret); //AccessKey SecretIAcsClient client = new DefaultAcsClient(profile);CommonRequest request = new CommonRequest();request.setSysMethod(MethodType.POST);//下面这3个不要改动request.setSysDomain("dysmsapi.aliyuncs.com");request.setSysVersion("2017-05-25");request.setSysAction("SendSms");//接收短信的手机号码request.putQueryParameter("PhoneNumbers",tel);//此处写电话号码//短信签名名称request.putQueryParameter("SignName","阿里云短信测试");//短信模板IDrequest.putQueryParameter("TemplateCode","SMS_154950909");//短信模板变量对应的实际值 ${code} 中的值Map<String,String> param = new HashMap<>(2);param.put("code", String.valueOf(code)); //写入的短信内容,验证码request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));try {CommonResponse response = client.getCommonResponse(request);System.out.println(response.getData());} catch (ServerException e) {e.printStackTrace();} catch (ClientException e) {e.printStackTrace();}}public static void main(String[] args) {int i = new Random().nextInt(80000) + 10000;System.out.println(i); // 35054SMSUtil.send("xxxx", "392712");}}
总结
1.springboot整合email,qq邮箱,特点免费;
2.springboot整合阿里短信服务,100条免费;
3.后续应用,可以用在登陆业务上,比如邮箱登陆,短信登陆;