In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how SpringBoot to send e-mail, send Wechat official account push function, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to know about it.
SpringBoot implements sending mail
JavaMailSender is a Spring-encapsulated email wrapper class that supports plain text, attachments, html and other formats.
Pom.xml org.springframework.boot spring-boot-starter-mail application.yml
We use QQ Mail to send email as an example. First of all, we need to open POP3/SMTP in QQ Mail and get the authorization code.
The configuration that needs to add to application.yml is as follows:
Spring: mail: host: smtp.qq.com username: xxxxxxxx@qq.com # this is not a QQ password Instead, the client authorization code password: xxxxxxxxx default-encoding: utf-8 protocol: smtp properties: mail: smtp: auth: starttls: # requires TLS authentication to ensure the security of sending email to verify the code implementation of enable: true required: true when POP3/SMTP is enabled.
sends mail with attachments using the instance MimeMessage created by the createMimeMessage () method of the JavaMailSender object as the sending object, and setting the mail parameters is done through the MimeMessageHelper object.
We create the EmailUtil utility class, in which we define the sendMail method to send mail. The complete EmailUtil code is as follows:
@ Componentpublic class EmailUtil {@ Value ("${spring.mail.username}") private String from; @ Resource private JavaMailSender javaMailSender; public boolean sendMail (String email, String subject, String text) {MimeMessage mimeMessage = javaMailSender.createMimeMessage (); try {MimeMessageHelper mimeMessageHelper = new MimeMessageHelper (mimeMessage, true) / / set sender mimeMessageHelper.setFrom (from); / / set recipient mimeMessageHelper.setTo (email); / / set email subject mimeMessageHelper.setSubject (subject) / / set the text message sent mimeMessageHelper.setText (text); javaMailSender.send (mimeMessage); return true;} catch (MessagingException e) {e.printStackTrace (); return false }}}
here uses the @ Resource annotation for the member variable JavaMailSender. The @ Resource annotation is similar to the @ Autowired annotation, which is used to declare the bean that needs to be automatically assembled, except that @ Autowired is a type-driven injection, while @ Resource is a name-driven injection, so the former has the problem of multi-bean, while the latter will not have the problem of multi-bean as long as the bean naming is unique.
SpringBoot implements sending Wechat official account push.
We need to log in to the Wechat official account test platform, obtain appID and appsecret, create a new template in the template message API, and obtain the template ID:
Then let the people who need to receive the message push follow the test official account:
In this way, we can get the openid (WeChat account) of the users who follow this official account:
Pom.xml
here we use Wechat template messages to push three-party sdk:
Com.github.binarywang weixin-java-mp 3.3.0 code implements @ Componentpublic class WxUtils {private Logger log = LoggerFactory.getLogger (getClass ()); public String sendMessage (String title, String message, String userOpenId) {/ / 1, configure WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage () / / set appId and secret wxStorage.setAppId ("xxxxxx"); wxStorage.setSecret ("xxxxxxxxx"); WxMpService wxMpService = new WxMpServiceImpl (); wxMpService.setWxMpConfigStorage (wxStorage); / / set templateID String messageTemplateId of message template = "xxxxxxxxxxx" / / 2 Push message WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder () .toUser (userOpenId) / / user to push openid .templateId (messageTemplateId) / / template id// .url ("https://www.xxxxx.com/xxxxxx/") / / after the user clicks the template message here Jump to the visited URL .build () / / 3, here configure the title and content of the push message, and their text color templateMessage.addData (new WxMpTemplateData ("title", title, "# FF00FF"); templateMessage.addData (new WxMpTemplateData ("message", message, "# FF00FF")); try {wxMpService.getTemplateMsgService (). SendTemplateMsg (templateMessage) Log.info ("Wechat message pushed successfully, message title: {}, message content: {}, pushed user openid: {}, template id: {}", title, message, userOpenId, messageTemplateId); return "pushed successfully" } catch (Exception e) {log.error ("push failed: {}, message title: {}, message content: {}, pushed user openid: {}, template id: {}", e.getMessage (), return "push failed";}
Various configuration items, such as appId and secret, need to be configured in application.yml, and then read out the properties of our configuration file with the @ Value annotation. I am lazy here.
Thank you for reading this article carefully. I hope the article "how to send email and send Wechat official account push function" shared by the editor is helpful to everyone. At the same time, I also hope you can support us and follow the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.