邮件和短信工具类

This commit is contained in:
Unique-Jerry 2023-11-23 23:23:22 +08:00
parent 3791cc2a35
commit 5afe95f67a
6 changed files with 176 additions and 8 deletions

24
pom.xml
View File

@ -123,6 +123,30 @@
<version>4.1.75.Final</version>
</dependency>
<!--添加邮箱-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.10</version>
</dependency>
<!--阿里云短信服务-->
<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>
</dependencies>
<build>

View File

@ -25,7 +25,7 @@ public class UserInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Map<String, Object> map = new HashMap<>();
String Token = request.getHeader("Authorization");
String Token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MDMyMzQ0MzksInVzZXJpZCI6MTJ9.y_Vdnkk2HZMYLcnfHIOpwitkZArrcOQfQM2hb1ys5ZY";
// 捕获刚刚JWT中抛出的异常,并封装对应的返回信息
try {

View File

@ -3,7 +3,7 @@ package com.yzdx.AiInterviewer.controller;
import com.yzdx.AiInterviewer.comment.R;
import com.yzdx.AiInterviewer.utiles.ParseResumeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
@ -94,7 +94,7 @@ public class UploadController {
return R.error("文件存储出现异常");
}
String RealFilePath="http://localhost:8080/upload/picture/"+filename;
String RealFilePath="http://101.43.255.47/upload/picture/"+filename;
Map<String,Object> data=new HashMap<>();
data.put("image",RealFilePath);
data.put("filename",filename);
@ -155,7 +155,7 @@ public class UploadController {
}
String RealFilePath="http://localhost:8080/upload/video/"+filename;
String RealFilePath="http://101.43.255.47:8080/upload/video/"+filename;
Map<String,Object> data=new HashMap<>();
data.put("video",RealFilePath);
data.put("filename",filename);
@ -267,7 +267,7 @@ public class UploadController {
return R.error("文件存储出现异常");
}
String RealFilePath="http://localhost:8080/upload/resume/"+fileName;
String RealFilePath="http://101.43.255.47:8080/upload/resume/"+fileName;
ExecutorService pool= Executors.newCachedThreadPool();
pool.submit(new Runnable() {
@Override

View File

@ -0,0 +1,105 @@
package com.yzdx.AiInterviewer.utiles;
import com.sun.mail.util.MailSSLSocketFactory;
import lombok.extern.slf4j.Slf4j;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
@Slf4j
public class EmailUtil {
//邮件服务器主机名
// QQ邮箱的 SMTP 服务器地址为: smtp.qq.com
private static String myEmailSMTPHost = "smtp.qq.com";
//发件人邮箱
private static String myEmailAccount = "2209176490@qq.com";
//发件人邮箱密码授权码
//在开启SMTP服务时会获取到一个授权码把授权码填在这里
private static String myEmailPassword = "smemgkzuixtbebja";
/**
* 邮件单发自由编辑短信并发送适用于私信
*
* @param email 收件箱地址
* @throws Exception
*/
public static String sendEmail(String email) {
String code=RandomCodeUtil.random(6);
log.info(code);
Transport ts = null;
try {
Properties prop = new Properties();
//设置QQ邮件服务器
prop.setProperty("mail.host", "smtp.qq.com");
//邮件发送协议
prop.setProperty("mail.transport.protocol", "smtp");
//需要验证用户名密码
prop.setProperty("mail.smtp.auth", "true");
//关于QQ邮箱还要设置SSL加密加上以下代码即可
// MailSSLSocketFactory sf = new MailSSLSocketFactory();
// sf.setTrustAllHosts(true);
// prop.put("mail.smtp.ssl.enable", "true");
// prop.put("mail.smtp.ssl.socketFactory", sf);
//使用JavaMail发送邮件的5个步骤
//1.创建定义整个应用程序所需的环境信息的Session对象
Session session = Session.getDefaultInstance(prop, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
//发件人邮件用户名授权码
return new PasswordAuthentication(myEmailAccount,
myEmailPassword);
}
});
//开启Session的debug模式这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2通过session得到transport对象
ts = session.getTransport();
//3使用邮箱的用户名和授权码连上邮件服务器
ts.connect("smtp.qq.com",myEmailAccount, myEmailPassword);
//4创建邮件
//4-1.txt创建邮件对象
MimeMessage message = new MimeMessage(session);
//4-2指明邮件的发件人
message.setFrom(new InternetAddress(myEmailAccount));
//4-3指明邮件的收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
//4-4邮件标题
message.setSubject("扬城直聘:");
//4-5邮件文本内容
message.setContent("我是测试!", "text/html;charset=UTF-8");
//4-6发送邮件
ts.sendMessage(message, message.getAllRecipients());
//5关闭连接
ts.close();
return code;
} catch (Exception e) {
return "发送邮件失败";
}
}
public static void main(String[] args) {
sendEmail("2660820526@qq.com");
}
}

View File

@ -0,0 +1,39 @@
package com.yzdx.AiInterviewer.utiles;
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", "LTAI5tQGXQtfT6ayrPEDDkbT", "gRLgZct8GQKreTseYPEVP1Qhi5rTFC");
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();
}
}
public static void main(String[] args) {
sendMessage("ReggieJerry","SMS_266290133","18652557103","236544");
}
}

View File

@ -3,9 +3,9 @@ server:
spring:
datasource:
url: jdbc:mysql://localhost:3306/ai_interviewer?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
url: jdbc:mysql://101.43.255.47:3306/ai_interviewer?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: Jerry
password: 2002811
jackson:
default-property-inclusion: non_null