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