完善阿里云和飞书邮件发送逻辑API调用需要注册并添加对应的key
This commit is contained in:
@@ -112,7 +112,17 @@
|
||||
<!-- <artifactId>apm-toolkit-trace</artifactId>-->
|
||||
<!-- <version>${与你的agent探针版本保持一致}</version>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.aliyun</groupId>-->
|
||||
<!-- <artifactId>aliyun-java-sdk-core</artifactId>-->
|
||||
<!-- <version>4.5.3</version>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- <!– https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-dm –>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.aliyun</groupId>-->
|
||||
<!-- <artifactId>aliyun-java-sdk-dm</artifactId>-->
|
||||
<!-- <version>3.3.1</version>-->
|
||||
<!-- </dependency>-->
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -164,7 +164,22 @@
|
||||
<groupId>org.lionsoul</groupId>
|
||||
<artifactId>ip2region</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>aliyun-java-sdk-core</artifactId>
|
||||
<version>4.5.3</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-dm -->
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>aliyun-java-sdk-dm</artifactId>
|
||||
<version>3.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.larksuite.oapi</groupId>
|
||||
<artifactId>oapi-sdk</artifactId>
|
||||
<version>2.4.19</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -32,6 +32,24 @@
|
||||
<artifactId>fastjson2</artifactId>
|
||||
<version>2.0.35</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>aliyun-java-sdk-core</artifactId>
|
||||
<version>4.5.20</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>aliyun-java-sdk-dm</artifactId>
|
||||
<version>3.3.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.larksuite.oapi</groupId>
|
||||
<artifactId>oapi-sdk</artifactId>
|
||||
<version>2.4.19</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -1,6 +1,17 @@
|
||||
package com.ruoyi.oa.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.aliyuncs.DefaultAcsClient;
|
||||
import com.aliyuncs.IAcsClient;
|
||||
import com.aliyuncs.dm.model.v20151123.SingleSendMailRequest;
|
||||
import com.aliyuncs.dm.model.v20151123.SingleSendMailResponse;
|
||||
import com.aliyuncs.exceptions.ClientException;
|
||||
import com.aliyuncs.profile.DefaultProfile;
|
||||
import com.lark.oapi.Client;
|
||||
import com.lark.oapi.service.mail.v1.model.MailAddress;
|
||||
import com.lark.oapi.service.mail.v1.model.Message;
|
||||
import com.lark.oapi.service.mail.v1.model.SendUserMailboxMessageReq;
|
||||
import com.lark.oapi.service.mail.v1.model.SendUserMailboxMessageResp;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.core.page.TableDataInfo;
|
||||
import com.ruoyi.common.core.domain.PageQuery;
|
||||
@@ -17,6 +28,7 @@ import com.ruoyi.oa.service.IOaEmailAccountService;
|
||||
import com.ruoyi.oa.domain.request.EmailSendRequest;
|
||||
import cn.hutool.extra.mail.MailAccount;
|
||||
import cn.hutool.extra.mail.MailUtil;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -146,81 +158,103 @@ public class OaEmailAccountServiceImpl implements IOaEmailAccountService {
|
||||
}
|
||||
}
|
||||
}
|
||||
// else if (type == 2) {
|
||||
// // 阿里云邮件推送API
|
||||
// for (String to : request.getToList()) {
|
||||
// try {
|
||||
// boolean result = sendAliyunMail(account, to, request.getSubject(), request.getContent());
|
||||
// if (result) {
|
||||
// success++;
|
||||
// } else {
|
||||
// fail++;
|
||||
// failList.append(to).append(", ");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// fail++;
|
||||
// failList.append(to).append(", ");
|
||||
// }
|
||||
// }
|
||||
// } else if (type == 3) {
|
||||
// // 飞书邮件API
|
||||
// for (String to : request.getToList()) {
|
||||
// try {
|
||||
// boolean result = sendFeishuMail(account, to, request.getSubject(), request.getContent());
|
||||
// if (result) {
|
||||
// success++;
|
||||
// } else {
|
||||
// fail++;
|
||||
// failList.append(to).append(", ");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// fail++;
|
||||
// failList.append(to).append(", ");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
else if (type == 2) {
|
||||
// 阿里云邮件推送API
|
||||
for (String to : request.getToList()) {
|
||||
try {
|
||||
boolean result = sendAliyunMail(account, to, request.getSubject(), request.getContent());
|
||||
if (result) {
|
||||
success++;
|
||||
} else {
|
||||
fail++;
|
||||
failList.append(to).append(", ");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
fail++;
|
||||
failList.append(to).append(", ");
|
||||
}
|
||||
}
|
||||
} else if (type == 3) {
|
||||
// 飞书邮件API
|
||||
for (String to : request.getToList()) {
|
||||
try {
|
||||
boolean result = sendFeishuMail(account, to, request.getSubject(), request.getContent());
|
||||
if (result) {
|
||||
success++;
|
||||
} else {
|
||||
fail++;
|
||||
failList.append(to).append(", ");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
fail++;
|
||||
failList.append(to).append(", ");
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
return "不支持的邮箱类型";
|
||||
}
|
||||
return "发送成功" + success + "封,失败" + fail + "封" + (fail > 0 ? (",失败邮箱:" + failList) : "");
|
||||
}
|
||||
// private boolean sendAliyunMail(OaEmailAccount account, String to, String subject, String content) {
|
||||
// try {
|
||||
// DefaultProfile profile = DefaultProfile.getProfile(
|
||||
// "cn-hangzhou",
|
||||
// account.getAccessKey(),
|
||||
// account.getSecretKey()
|
||||
// );
|
||||
// IAcsClient client = new DefaultAcsClient(profile);
|
||||
//
|
||||
// SingleSendMailRequest request = new SingleSendMailRequest();
|
||||
// request.setAccountName(account.getEmail());
|
||||
// request.setFromAlias("发件人昵称");
|
||||
// request.setAddressType(1);
|
||||
// request.setReplyToAddress(true);
|
||||
// request.setToAddress(to);
|
||||
// request.setSubject(subject);
|
||||
// request.setHtmlBody(content);
|
||||
//
|
||||
// SingleSendMailResponse response = client.getAcsResponse(request);
|
||||
// return true;
|
||||
// } catch (ClientException e) {
|
||||
// // 打印日志 e.getErrCode() + e.getErrMsg()
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
// private boolean sendFeishuMail(OaEmailAccount account, String to, String subject, String content) {
|
||||
// // 1. 获取access_token(用account.getAccessKey()/getSecretKey())
|
||||
// // 2. 调用飞书邮件API发送邮件
|
||||
// // 这里只做伪代码,实际需查飞书开放平台文档
|
||||
// try {
|
||||
// // String accessToken = getFeishuAccessToken(account.getAccessKey(), account.getSecretKey());
|
||||
// // 用accessToken和邮件参数发POST请求
|
||||
// // RestTemplate restTemplate = new RestTemplate();
|
||||
// // String response = restTemplate.postForObject(apiUrl, params, String.class);
|
||||
// return true;
|
||||
// } catch (Exception e) {
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
private boolean sendAliyunMail(OaEmailAccount account, String to, String subject, String content) {
|
||||
try {
|
||||
DefaultProfile profile = DefaultProfile.getProfile(
|
||||
"cn-hangzhou",
|
||||
account.getAccessKey(),
|
||||
account.getSecretKey()
|
||||
);
|
||||
IAcsClient client = new DefaultAcsClient(profile);
|
||||
|
||||
SingleSendMailRequest request = new SingleSendMailRequest();
|
||||
request.setAccountName(account.getEmail());
|
||||
request.setFromAlias("发件人昵称");
|
||||
request.setAddressType(1);
|
||||
request.setReplyToAddress(true);
|
||||
request.setToAddress(to);
|
||||
request.setSubject(subject);
|
||||
request.setHtmlBody(content);
|
||||
|
||||
SingleSendMailResponse response = client.getAcsResponse(request);
|
||||
//打印这个响应
|
||||
System.out.println(response.getRequestId());
|
||||
return true;
|
||||
} catch (ClientException e) {
|
||||
// 打印日志 e.getErrCode() + e.getErrMsg()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
private boolean sendFeishuMail(OaEmailAccount account, String to, String subject, String content) {
|
||||
try {
|
||||
// 1. 构建飞书Client
|
||||
Client client = Client.newBuilder(account.getAccessKey(), account.getSecretKey()).build();
|
||||
|
||||
// 2. 构建收件人
|
||||
MailAddress[] toList = new MailAddress[]{
|
||||
MailAddress.newBuilder().mailAddress(to).name(to).build()
|
||||
};
|
||||
|
||||
// 3. 构建请求
|
||||
SendUserMailboxMessageReq req = SendUserMailboxMessageReq.newBuilder()
|
||||
.userMailboxId("me") // 推荐用 "me"
|
||||
.message(Message.newBuilder()
|
||||
.subject(subject)
|
||||
.to(toList)
|
||||
.bodyHtml(content)
|
||||
.build())
|
||||
.build();
|
||||
|
||||
// 4. 发送请求
|
||||
SendUserMailboxMessageResp resp = client.mail().v1().userMailboxMessage().send(req);
|
||||
|
||||
// 5. 判断结果
|
||||
if (!resp.success()) {
|
||||
// 可加日志 resp.getCode(), resp.getMsg()
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
// 可加日志 e.getMessage()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user