详情参见:Java SpringBoot集成阿里云短信与邮件服务_阿里云短信jar包_大鱼>的博客-CSDN博客
org.springframework.boot
spring-boot-starter-thymeleaf
在resources下创建一个模板文件夹template
在配置文件yml的spring节点下添加对template文件夹内容的引入
thymeleaf:
prefix: classpath:/template/
在template文件夹下创建一个html文件,比如Cn_UserShare.html
账户分享
尊敬的用户:
您收到一个账户的分享,分享链接为:
点击登录
访问地址:
到期时间:
请使用:Chrome、Edge、Safari、Firefox等浏览器
如有疑问,请致电24小时客服热线 +86 139xxxxxxxx
其中:${userName},"${url}","${expireTime}"为需要动态替换的内容
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
/**
* 账户分享服务
* @author Mr.Lee
* @date 20230222
*/
@Service
@Slf4j
public class UserShareService {
@Value("${gnss.share.url}")
private String baseUrl;
@Autowired
private UserShareDao userShareDao;
@Autowired
private TokenService tokenService;
@Autowired
private TemplateEngine templateEngine;
/**
* 添加分享记录
* @param userShare
* @return
*/
public String addUserShare(UserShare userShare){
String id= CommonUtils.getUUID();
String url=String.format("%s?id=%s&userId=%s&language=%s",baseUrl,id,userShare.getUserId(),userShare.getLanguage());
userShare.setId(id);
userShare.setUrl(url);
userShare.setCreatorId(tokenService.getTokenInfo().getUserId());
//后续对接邮件推送
if(StringUtils.isNotBlank(userShare.getEmail())&&CommonUtils.checkEmailAddress(userShare.getEmail())){
String subject = "";
if (userShare.getLanguage() ==1) {
subject = "账户分享";
} else {
subject = "Account sharing";
}
Context context = new Context();
// 设置模板中的变量
context.setVariable("userName", userShare.getShareUser());
context.setVariable("url",url);
context.setVariable("expireTime",userShare.getExpireTime());
// 第一个参数为模板的名称
String htmlBody;
if(userShare.getLanguage() ==1) {
htmlBody = templateEngine.process("Cn_UserShare.html", context);
}else{
htmlBody = templateEngine.process("En_UserShare.html", context);
}
AliPushUtils.sendEmail(userShare.getEmail(),"",htmlBody,subject);
}
int status = userShareDao.addUserShare(userShare);
if(status>0){
return url;
}else{
return null;
}
}
}
/**
* 发送HTML格式邮件
* @param toAddress
* @param fromAlias
* @param htmlBody
* @param subject
* @return
*/
public static SingleSendMailResponse sendEmail(String toAddress, String fromAlias, String htmlBody, String subject) {
try {
if (fromAlias == null || fromAlias.length() <= 0) {
fromAlias = AliPushConstants.ALI_EMAIL_CN_FROM_ALIAS;
}
IClientProfile profile = DefaultProfile.getProfile(AliPushConstants.ALI_EMAIL_REGION_ID, AliPushConstants.ALI_EMAIL_ACCESS_KEY_ID, AliPushConstants.ALI_EMAIL_SECRET);
IAcsClient client = new DefaultAcsClient(profile);
SingleSendMailRequest request = new SingleSendMailRequest();
request.setAccountName(AliPushConstants.ALI_EMAIL_ACCOUNT_NAME);
request.setFromAlias(fromAlias);
request.setAddressType(1);
request.setToAddress(toAddress);
request.setReplyToAddress(true);
//可以给多个收件人发送邮件,收件人之间用逗号分开
request.setSubject(subject);
//如果采用byte[].toString的方式的话请确保最终转换成utf-8的格式再放入htmlbody和textbody,若编码不一致则会被当成垃圾邮件。
//注意:文本邮件的大小限制为3M,过大的文本会导致连接超时或413错误
request.setHtmlBody(htmlBody);
//若textBody、htmlBody或content的大小不确定,建议采用POST方式提交,避免出现uri is not valid异常
request.setMethod(MethodType.POST);
SingleSendMailResponse sendMailResponse = client.getAcsResponse(request);
log.info("邮件发送失败:给邮箱为{},发送的内容为{},主题为{}", toAddress, htmlBody, subject);
return sendMailResponse;
}catch (Exception e) {
log.error(e.getMessage());
return null;
}
}