From 2b88fbe48fb2a15446532e06b0373889c5ee03fc Mon Sep 17 00:00:00 2001 From: xuthus Date: Tue, 14 Sep 2021 10:33:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BB=A3=E7=A0=81,=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0email=E9=80=9A=E7=9F=A5=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 8 ++-- .../wx_post/config/OhMyEmailConfig.java | 22 +++++++++ .../wx_post/controller/ApiController.java | 24 +++------- .../xuthus83/wx_post/service/ApiService.java | 24 ++++++++++ .../wx_post/service/impl/ApiServiceImpl.java | 46 +++++++++++++++++++ 5 files changed, 103 insertions(+), 21 deletions(-) create mode 100644 src/main/java/cn/xuthus83/wx_post/config/OhMyEmailConfig.java create mode 100644 src/main/java/cn/xuthus83/wx_post/service/ApiService.java create mode 100644 src/main/java/cn/xuthus83/wx_post/service/impl/ApiServiceImpl.java diff --git a/pom.xml b/pom.xml index 43b8fd9..f986f77 100644 --- a/pom.xml +++ b/pom.xml @@ -21,8 +21,6 @@ org.springframework.boot spring-boot-starter-web - - org.projectlombok lombok @@ -33,12 +31,16 @@ spring-boot-starter-test test - com.github.binarywang wx-java-mp-spring-boot-starter 4.1.0 + + io.github.biezhi + oh-my-email + 0.0.4 + diff --git a/src/main/java/cn/xuthus83/wx_post/config/OhMyEmailConfig.java b/src/main/java/cn/xuthus83/wx_post/config/OhMyEmailConfig.java new file mode 100644 index 0000000..a9adf48 --- /dev/null +++ b/src/main/java/cn/xuthus83/wx_post/config/OhMyEmailConfig.java @@ -0,0 +1,22 @@ +package cn.xuthus83.wx_post.config; + +import io.github.biezhi.ome.OhMyEmail; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.util.Properties; + +@Component +public class OhMyEmailConfig { + // 1 + @PostConstruct + public void postConstruct() { + before(); + System.out.println("system started, triggered by postConstruct."); + } + + public void before() { + // 配置,一次即可 + OhMyEmail.config(OhMyEmail.SMTP_QQ(false), "913651466@qq.com", "xgz187415157"); + } +} diff --git a/src/main/java/cn/xuthus83/wx_post/controller/ApiController.java b/src/main/java/cn/xuthus83/wx_post/controller/ApiController.java index 8af597d..afad6fb 100644 --- a/src/main/java/cn/xuthus83/wx_post/controller/ApiController.java +++ b/src/main/java/cn/xuthus83/wx_post/controller/ApiController.java @@ -1,38 +1,26 @@ package cn.xuthus83.wx_post.controller; +import cn.xuthus83.wx_post.service.ApiService; +import io.github.biezhi.ome.SendMailException; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.error.WxErrorException; -import me.chanjar.weixin.mp.api.WxMpService; -import me.chanjar.weixin.mp.api.WxMpTemplateMsgService; -import me.chanjar.weixin.mp.bean.template.WxMpTemplateData; -import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import java.util.ArrayList; -import java.util.List; - @RestController @RequestMapping("/wx") @Slf4j public class ApiController { @Autowired - WxMpService wxMpService; + private ApiService apiService; @RequestMapping("sms/{title}/{content}") - public String wxpostsms(@PathVariable String title, @PathVariable String content) throws WxErrorException { - WxMpTemplateMsgService templateMsgService = wxMpService.getTemplateMsgService(); - WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage(); - wxMpTemplateMessage.setTemplateId("39Rc_oFPxUZN_IhSO_7jyXEJ6bWtnK7Q_dAwGZkt-MA"); - List data = new ArrayList<>(); - data.add(new WxMpTemplateData("phone", title)); - data.add(new WxMpTemplateData("content", content)); - wxMpTemplateMessage.setData(data); - wxMpTemplateMessage.setToUser("o_XWL59pfGYvg_5cj0kuMpsFqqRE"); - String s = templateMsgService.sendTemplateMsg(wxMpTemplateMessage); + public String wxpostsms(@PathVariable String title, @PathVariable String content) throws WxErrorException, SendMailException { + apiService.emailByQQ(title, content); + apiService.wxTemplate(title, content); return title + "/" + content; } } diff --git a/src/main/java/cn/xuthus83/wx_post/service/ApiService.java b/src/main/java/cn/xuthus83/wx_post/service/ApiService.java new file mode 100644 index 0000000..e88a6cb --- /dev/null +++ b/src/main/java/cn/xuthus83/wx_post/service/ApiService.java @@ -0,0 +1,24 @@ +package cn.xuthus83.wx_post.service; + +import io.github.biezhi.ome.SendMailException; +import me.chanjar.weixin.common.error.WxErrorException; + +public interface ApiService { + + /** + * 发送微信测试号订阅消息 + * + * @param title + * @param content + * @throws WxErrorException + */ + public void wxTemplate(String title, String content) throws WxErrorException; + + /** + * 通过QQ邮箱发送邮件通知 + * + * @param title + * @param content + */ + public void emailByQQ(String title, String content) throws SendMailException; +} diff --git a/src/main/java/cn/xuthus83/wx_post/service/impl/ApiServiceImpl.java b/src/main/java/cn/xuthus83/wx_post/service/impl/ApiServiceImpl.java new file mode 100644 index 0000000..4ac430f --- /dev/null +++ b/src/main/java/cn/xuthus83/wx_post/service/impl/ApiServiceImpl.java @@ -0,0 +1,46 @@ +package cn.xuthus83.wx_post.service.impl; + +import cn.xuthus83.wx_post.service.ApiService; +import io.github.biezhi.ome.OhMyEmail; +import io.github.biezhi.ome.SendMailException; +import lombok.extern.slf4j.Slf4j; +import me.chanjar.weixin.common.error.WxErrorException; +import me.chanjar.weixin.mp.api.WxMpService; +import me.chanjar.weixin.mp.api.WxMpTemplateMsgService; +import me.chanjar.weixin.mp.bean.template.WxMpTemplateData; +import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +@Slf4j +public class ApiServiceImpl implements ApiService { + + @Autowired + private WxMpService wxMpService; + + @Override + public void wxTemplate(String title, String content) throws WxErrorException { + WxMpTemplateMsgService templateMsgService = wxMpService.getTemplateMsgService(); + WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage(); + wxMpTemplateMessage.setTemplateId("39Rc_oFPxUZN_IhSO_7jyXEJ6bWtnK7Q_dAwGZkt-MA"); + List data = new ArrayList<>(); + data.add(new WxMpTemplateData("phone", title)); + data.add(new WxMpTemplateData("content", content)); + wxMpTemplateMessage.setData(data); + wxMpTemplateMessage.setToUser("o_XWL59pfGYvg_5cj0kuMpsFqqRE"); + String s = templateMsgService.sendTemplateMsg(wxMpTemplateMessage); + } + + @Override + public void emailByQQ(String title, String content) throws SendMailException { + OhMyEmail.subject(title) + .from("橘子土豆") + .to("xuthus83@outlook.com") + .text(content) + .send(); + } +}