diff --git a/ruoyi-admin/pom.xml b/ruoyi-admin/pom.xml index deeedf1..41397ff 100644 --- a/ruoyi-admin/pom.xml +++ b/ruoyi-admin/pom.xml @@ -112,7 +112,17 @@ - + + + + + + + + + + + diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index 664d4d4..cdbd0a4 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -164,7 +164,22 @@ org.lionsoul ip2region - + + com.aliyun + aliyun-java-sdk-core + 4.5.3 + + + + com.aliyun + aliyun-java-sdk-dm + 3.3.1 + + + com.larksuite.oapi + oapi-sdk + 2.4.19 + diff --git a/ruoyi-oa/pom.xml b/ruoyi-oa/pom.xml index bdd6c52..9db7fe7 100644 --- a/ruoyi-oa/pom.xml +++ b/ruoyi-oa/pom.xml @@ -32,6 +32,24 @@ fastjson2 2.0.35 + + com.aliyun + aliyun-java-sdk-core + 4.5.20 + compile + + + com.aliyun + aliyun-java-sdk-dm + 3.3.1 + compile + + + com.larksuite.oapi + oapi-sdk + 2.4.19 + compile + diff --git a/ruoyi-oa/src/main/java/com/ruoyi/oa/service/impl/OaEmailAccountServiceImpl.java b/ruoyi-oa/src/main/java/com/ruoyi/oa/service/impl/OaEmailAccountServiceImpl.java index 046c97b..e8f955f 100644 --- a/ruoyi-oa/src/main/java/com/ruoyi/oa/service/impl/OaEmailAccountServiceImpl.java +++ b/ruoyi-oa/src/main/java/com/ruoyi/oa/service/impl/OaEmailAccountServiceImpl.java @@ -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; + } + } }