更新代码,添加email通知发送

This commit is contained in:
xuthus 2021-09-14 10:33:54 +08:00
parent 25eb21956d
commit 2b88fbe48f
5 changed files with 103 additions and 21 deletions

View File

@ -21,8 +21,6 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
@ -33,12 +31,16 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.binarywang</groupId> <groupId>com.github.binarywang</groupId>
<artifactId>wx-java-mp-spring-boot-starter</artifactId> <artifactId>wx-java-mp-spring-boot-starter</artifactId>
<version>4.1.0</version> <version>4.1.0</version>
</dependency> </dependency>
<dependency>
<groupId>io.github.biezhi</groupId>
<artifactId>oh-my-email</artifactId>
<version>0.0.4</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -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");
}
}

View File

@ -1,38 +1,26 @@
package cn.xuthus83.wx_post.controller; 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 lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException; 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.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController @RestController
@RequestMapping("/wx") @RequestMapping("/wx")
@Slf4j @Slf4j
public class ApiController { public class ApiController {
@Autowired @Autowired
WxMpService wxMpService; private ApiService apiService;
@RequestMapping("sms/{title}/{content}") @RequestMapping("sms/{title}/{content}")
public String wxpostsms(@PathVariable String title, @PathVariable String content) throws WxErrorException { public String wxpostsms(@PathVariable String title, @PathVariable String content) throws WxErrorException, SendMailException {
WxMpTemplateMsgService templateMsgService = wxMpService.getTemplateMsgService(); apiService.emailByQQ(title, content);
WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage(); apiService.wxTemplate(title, content);
wxMpTemplateMessage.setTemplateId("39Rc_oFPxUZN_IhSO_7jyXEJ6bWtnK7Q_dAwGZkt-MA");
List<WxMpTemplateData> 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);
return title + "/" + content; return title + "/" + content;
} }
} }

View File

@ -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;
}

View File

@ -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<WxMpTemplateData> 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();
}
}