SpringBoot推送微信测试公众号信息
1、登陆微信公众平台测试号
2、扫码关注
3、新建模版
参数需以{{开头,以.DATA}}结尾 ,ex:{{msg.DATA}},代码里面替换就可以了
templateMessage.addData(new WxMpTemplateData("msg","测试测试","#FF69B4"));
4、导入依赖
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
5、配置文件及配置类编写
- 配置文件
wechat:
mpAppId: xxxxxxxxxxxxxxxxx
mpAppSecret: xxxxxxxxxxxxxxxxx
toUser: xxxxxxxxxxxxxxxxx
templateId: xxxxxxxxxxxxxxxxxxxxx
- 配置类
package com.wanqi.email2.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
/**
* @Description TODO
* @Version 1.0.0
* @Date 2022/10/2
* @Author wandaren
*/
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {
private String mpAppId;
private String mpAppSecret;
private String toUser;
private String templateId;
public String getMpAppId() {
return mpAppId;
}
public void setMpAppId(String mpAppId) {
this.mpAppId = mpAppId;
}
public String getMpAppSecret() {
return mpAppSecret;
}
public void setMpAppSecret(String mpAppSecret) {
this.mpAppSecret = mpAppSecret;
}
public String getToUser() {
return toUser;
}
public void setToUser(String toUser) {
this.toUser = toUser;
}
public String getTemplateId() {
return templateId;
}
public void setTemplateId(String templateId) {
this.templateId = templateId;
}
}
package com.wanqi.email2.config;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WeChatMyConfig {
@Autowired
private WechatAccountConfig wechatAccountConfig;
@Bean(name = "wxMpConfigStorage")
public WxMpConfigStorage wxMpConfigStorage(){
WxMpDefaultConfigImpl wxMpConfigStorage = new WxMpDefaultConfigImpl();
wxMpConfigStorage.setAppId(wechatAccountConfig.getMpAppId());
wxMpConfigStorage.setSecret(wechatAccountConfig.getMpAppSecret());
return wxMpConfigStorage;
}
@Bean(name = "wxMpService")
public WxMpService wxMpService(@Qualifier(value = "wxMpConfigStorage") WxMpConfigStorage wxMpConfigStorage){
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage);
return wxMpService;
}
@Bean
public WxMpTemplateMessage wxMpTemplateMessage(){
return WxMpTemplateMessage.builder()
//接收方微信openId
.toUser(wechatAccountConfig.getToUser())
//模板Id
.templateId(wechatAccountConfig.getTemplateId())
.build();
}
}
6、消息推送
package com.wanqi.email2;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import me.chanjar.weixin.mp.api.WxMpService;
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.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @Description TODO
* @Version 1.0.0
* @Date 2022/9/28
* @Author wandaren
*/
@Component
@RefreshScope
public class EmailScheduler {
@Autowired
private WxMpService wxMpService;
@Autowired
private WxMpTemplateMessage wxMpTemplateMessage;
private void push(String msg) {
wxMpTemplateMessage.addData(new WxMpTemplateData("riqi", DateUtil.formatDateTime(new Date()), "#00BFFF"));
wxMpTemplateMessage.addData(new WxMpTemplateData("msg", msg, "#e6b422"));
wxMpTemplateMessage.addData(new WxMpTemplateData("url", "cookie过期修复清查看邮件"));
wxMpTemplateMessage.addData(new WxMpTemplateData("beizhu", "京东E卡", "#f09199"));
try {
wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
} catch (Exception e) {
e.printStackTrace();
}
}
}
分类:
Java
标签:
SpringBoot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)