邮件发送
This commit is contained in:
parent
9d1f0f5852
commit
e828e089a6
|
@ -3,8 +3,11 @@ package com.hzya.frame.plugin.sendEmail.dao;
|
||||||
import com.hzya.frame.basedao.dao.IBaseDao;
|
import com.hzya.frame.basedao.dao.IBaseDao;
|
||||||
import com.hzya.frame.plugin.sendEmail.entity.SendEmailEntity;
|
import com.hzya.frame.plugin.sendEmail.entity.SendEmailEntity;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface ISendEmailDao extends IBaseDao<SendEmailEntity, String> {
|
public interface ISendEmailDao extends IBaseDao<SendEmailEntity, String> {
|
||||||
|
|
||||||
|
|
||||||
|
List<SendEmailEntity> queryFileList(SendEmailEntity fileEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,20 @@
|
||||||
package com.hzya.frame.plugin.sendEmail.dao.impl;
|
package com.hzya.frame.plugin.sendEmail.dao.impl;
|
||||||
|
|
||||||
|
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||||
import com.hzya.frame.basedao.dao.MybatisGenericDao;
|
import com.hzya.frame.basedao.dao.MybatisGenericDao;
|
||||||
import com.hzya.frame.plugin.sendEmail.dao.ISendEmailDao;
|
import com.hzya.frame.plugin.sendEmail.dao.ISendEmailDao;
|
||||||
import com.hzya.frame.plugin.sendEmail.entity.SendEmailEntity;
|
import com.hzya.frame.plugin.sendEmail.entity.SendEmailEntity;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
public class SendEmailDaoImpl extends MybatisGenericDao<SendEmailEntity, String> implements ISendEmailDao {
|
public class SendEmailDaoImpl extends MybatisGenericDao<SendEmailEntity, String> implements ISendEmailDao {
|
||||||
|
|
||||||
|
@DS("#entity.dataSourceCode")
|
||||||
|
@Override
|
||||||
|
public List<SendEmailEntity> queryFileList(SendEmailEntity entity) {
|
||||||
|
return (List<SendEmailEntity>) super.selectList(getSqlIdPrifx()+"queryFileList",entity);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,46 @@ package com.hzya.frame.plugin.sendEmail.entity;
|
||||||
|
|
||||||
import com.hzya.frame.web.entity.BaseEntity;
|
import com.hzya.frame.web.entity.BaseEntity;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class SendEmailEntity extends BaseEntity {
|
public class SendEmailEntity extends BaseEntity {
|
||||||
|
|
||||||
|
private List<String> ids;//附件id
|
||||||
|
private String id;
|
||||||
|
private String filename;
|
||||||
|
private String contentType;
|
||||||
|
@Override
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFilename() {
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFilename(String filename) {
|
||||||
|
this.filename = filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getIds() {
|
||||||
|
return ids;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIds(List<String> ids) {
|
||||||
|
this.ids = ids;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContentType() {
|
||||||
|
return contentType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContentType(String contentType) {
|
||||||
|
this.contentType = contentType;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,18 @@
|
||||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
<mapper namespace="com.hzya.frame.plugin.sendEmail.dao.impl.SendEmailDaoImpl">
|
<mapper namespace="com.hzya.frame.plugin.sendEmail.dao.impl.SendEmailDaoImpl">
|
||||||
|
|
||||||
|
<select id="queryFileList" resultType="com.hzya.frame.plugin.sendEmail.entity.SendEmailEntity"
|
||||||
|
parameterType="com.hzya.frame.plugin.sendEmail.entity.SendEmailEntity">
|
||||||
|
SELECT
|
||||||
|
b.id,
|
||||||
|
b.filename
|
||||||
|
FROM
|
||||||
|
CTP_ATTACHMENT a
|
||||||
|
LEFT JOIN ctp_file b on a.file_url = b.id
|
||||||
|
WHERE sub_reference in
|
||||||
|
<foreach item="item" collection="ids" open="(" separator="," close=")">
|
||||||
|
#{item}
|
||||||
|
</foreach>
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|
||||||
|
|
|
@ -16,14 +16,29 @@ import org.springframework.mock.web.MockMultipartFile;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import javax.net.ssl.HostnameVerifier;
|
||||||
|
import javax.net.ssl.HttpsURLConnection;
|
||||||
|
import javax.net.ssl.SSLContext;
|
||||||
|
import javax.net.ssl.TrustManager;
|
||||||
|
import javax.net.ssl.X509TrustManager;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.cert.CertificateException;
|
||||||
|
import java.security.cert.X509Certificate;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
public class SendEmailServiceImpl extends BaseService<SendEmailEntity, String> implements ISendEmailService {
|
public class SendEmailServiceImpl extends BaseService<SendEmailEntity, String> implements ISendEmailService {
|
||||||
@Value("${zt.url}")
|
@Value("${zt.url}")
|
||||||
|
@ -66,10 +81,8 @@ public class SendEmailServiceImpl extends BaseService<SendEmailEntity, String> i
|
||||||
return BaseResult.getFailureMessageEntity("供应商邮箱为空,请检查");
|
return BaseResult.getFailureMessageEntity("供应商邮箱为空,请检查");
|
||||||
}
|
}
|
||||||
String subject = null;
|
String subject = null;
|
||||||
//subject = mainData.getString("field0034");//todo
|
subject = mainData.getOrDefault("field0016","")+"采购";//供应商名称+采购
|
||||||
if(sendEmail == null || "".equals(sendEmail)){
|
|
||||||
subject = "采购";
|
|
||||||
}
|
|
||||||
JSONArray detailArray = businessDataStr.getJSONArray("formson_1234");
|
JSONArray detailArray = businessDataStr.getJSONArray("formson_1234");
|
||||||
//组装发送数据
|
//组装发送数据
|
||||||
String htmls = null;
|
String htmls = null;
|
||||||
|
@ -79,28 +92,132 @@ public class SendEmailServiceImpl extends BaseService<SendEmailEntity, String> i
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return BaseResult.getFailureMessageEntity("发送邮件失败:组装数据失败");
|
return BaseResult.getFailureMessageEntity("发送邮件失败:组装数据失败");
|
||||||
}
|
}
|
||||||
|
List<SendEmailEntity> files = null;
|
||||||
|
List<String > ids = new ArrayList<>();
|
||||||
|
if(detailArray != null && detailArray.size() > 0){
|
||||||
|
for (int i = 0; i < detailArray.size(); i++) {
|
||||||
|
if(detailArray.getJSONObject(i).getString("field0015") != null && !"".equals(detailArray.getJSONObject(i).getString("field0015"))){
|
||||||
|
ids.add(detailArray.getJSONObject(i).getString("field0015"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(ids != null && ids.size() > 0){
|
||||||
|
SendEmailEntity fileEntity = new SendEmailEntity();
|
||||||
|
fileEntity.setDataSourceCode("SW-OA");
|
||||||
|
fileEntity.setId(mainData.getString("field0040"));
|
||||||
|
files = sendEmailDao.queryFileList(fileEntity);
|
||||||
|
}
|
||||||
|
List<MultipartFile> attachments = new ArrayList<>();
|
||||||
|
|
||||||
//发送数据
|
//发送数据
|
||||||
logger.error("发送邮件发送数据:" + htmls);
|
logger.error("发送邮件发送数据:" + htmls);
|
||||||
try {
|
try {
|
||||||
String filePath = "/Users/apple/Desktop/【C4协同费控产品】(报表相关)字典说明zm@2025.pdf";
|
//循环下载附件
|
||||||
File file = new File(filePath);
|
if(files != null && files.size() > 0){
|
||||||
List<MultipartFile> attachments = new ArrayList<>();
|
String token = null;
|
||||||
try (FileInputStream input = new FileInputStream(file)) {
|
JSONObject tokenData = new JSONObject();
|
||||||
MultipartFile multipartFile = new MockMultipartFile(
|
tokenData.put("userName","hzya_rest");
|
||||||
file.getName(), // 对应表单中的文件字段名
|
tokenData.put("password","879dead9-a8a7-464a-9bac-e8f574e66985");
|
||||||
file.getName(), // 原始文件名
|
tokenData.put("loginName","shileilei");
|
||||||
"application/pdf", // 或根据文件类型指定 MIME 类型
|
String tokenResult = HttpRequest.post("http://oa.sptzj.cn/seeyon/rest/token").
|
||||||
input
|
body(tokenData.toJSONString()).
|
||||||
);
|
execute().
|
||||||
attachments.add(multipartFile);
|
body();
|
||||||
|
logger.error("OAToken:" + tokenResult);
|
||||||
|
JSONObject resoutJson = JSONObject.parseObject(tokenResult);
|
||||||
|
if (resoutJson.getBoolean("flag")) {
|
||||||
|
token = resoutJson.getJSONObject("attribute").getString("id");
|
||||||
|
} else {
|
||||||
|
return BaseResult.getFailureMessageEntity("获取OA token失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < files.size(); i++) {
|
||||||
|
String encodedFileName = URLEncoder.encode(files.get(i).getFilename(), StandardCharsets.UTF_8.name());
|
||||||
|
String pdfUrl = "http://oa.sptzj.cn/seeyon/rest/attachment/file/"+files.get(i).getId()+"?fileName="+encodedFileName+"&token="+token;
|
||||||
|
logger.error("附件的url"+pdfUrl);
|
||||||
|
File file = getFile(pdfUrl,files.get(i).getFilename());
|
||||||
|
if(file != null){
|
||||||
|
try {
|
||||||
|
FileInputStream input = new FileInputStream(file);
|
||||||
|
MultipartFile multipartFile = new MockMultipartFile(
|
||||||
|
file.getName(), // 对应表单中的文件字段名
|
||||||
|
file.getName(), // 原始文件名
|
||||||
|
files.get(i).getContentType(), // 或根据文件类型指定 MIME 类型
|
||||||
|
input
|
||||||
|
);
|
||||||
|
attachments.add(multipartFile);
|
||||||
|
}catch (Exception e){
|
||||||
|
return BaseResult.getFailureMessageEntity("发送邮件失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(attachments != null && attachments.size() > 0){
|
||||||
|
emailUtil.sendHtmlAndFileMessage(sendEmail,subject, htmls,attachments);
|
||||||
|
}else {
|
||||||
|
emailUtil.sendHtmlMessage(sendEmail,subject, htmls);
|
||||||
}
|
}
|
||||||
emailUtil.sendHtmlAndFileMessage(sendEmail,subject, htmls,attachments);
|
|
||||||
return BaseResult.getSuccessMessageEntity("发送邮件成功");
|
return BaseResult.getSuccessMessageEntity("发送邮件成功");
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
return BaseResult.getFailureMessageEntity("发送邮件失败");
|
return BaseResult.getFailureMessageEntity("发送邮件失败");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private File getFile(String fileurl,String filename) {
|
||||||
|
try {
|
||||||
|
// 获取文件字节数据
|
||||||
|
byte[] fileBytes = null;
|
||||||
|
|
||||||
|
// 创建信任所有证书的TrustManager
|
||||||
|
TrustManager[] trustAllCerts = new TrustManager[]{
|
||||||
|
new X509TrustManager() {
|
||||||
|
public X509Certificate[] getAcceptedIssuers() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
|
||||||
|
}
|
||||||
|
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 安装信任管理器
|
||||||
|
SSLContext sc = SSLContext.getInstance("TLS");
|
||||||
|
sc.init(null, trustAllCerts, new java.security.SecureRandom());
|
||||||
|
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
|
||||||
|
|
||||||
|
// 创建允许所有主机名的验证器
|
||||||
|
HostnameVerifier allHostsValid = (hostname, session) -> true;
|
||||||
|
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
|
||||||
|
|
||||||
|
URL url = new URL(fileurl);
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("GET");
|
||||||
|
|
||||||
|
try (InputStream inputStream = connection.getInputStream();
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
||||||
|
byte[] buffer = new byte[4096];
|
||||||
|
int bytesRead;
|
||||||
|
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||||
|
outputStream.write(buffer, 0, bytesRead);
|
||||||
|
}
|
||||||
|
fileBytes = outputStream.toByteArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (fileBytes != null) {
|
||||||
|
// 模拟一个文件
|
||||||
|
File tempFile = new File(filename);
|
||||||
|
try (FileOutputStream fos = new FileOutputStream(tempFile)) {
|
||||||
|
fos.write(fileBytes);
|
||||||
|
}
|
||||||
|
return tempFile;
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error(e.getMessage());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @param mainData
|
* @param mainData
|
||||||
* @param detailArray
|
* @param detailArray
|
||||||
|
|
Loading…
Reference in New Issue