“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”

Preface:

Recently, there is a requirement that when a user leaves a message in the system, the system needs to notify the administrator by email, and the observer mode in the design pattern is adopted. This topic is about the implementation of mail

1. Implementation of the observer mode in SpringBoot

See another article I wrote: SpringBoot Practices – the Observer Pattern

2. SpringBoot integrates emails

(1) Sign up for an email

Here is to go to 163 to register the mailbox, after registration, login, according to the following figure, open the corresponding configuration, and then you will get a”Authorization code“Will be used in later configurations

As an aside: Calling mailbox 163 to send an email is easily identified as spam T_T……. I switched to two 163 mailboxes, and both of them were still able to send emails at the beginning, but both of them were considered spam and failed to send emails…….. So changed to QQ mailbox QAQ

(2) Introduce dependencies into POM
<! <dependency> <groupId>org.springframework.boot</groupId> <artifactId> </artifactId> </artifactId> </artifactId> </artifactId> </artifactId> </artifactId> </artifactId> </artifactId> </dependency>Copy the code
(3) Write the configuration in application.yml
  • Host: The address of the mail server. In this case, since we use email 163, write the address of the mail 163
  • Username: indicates the email login account
  • Password: indicates the authorization code. You need to log in to the mailbox to configure it
  • Default-encoding: indicates the encoding mode

(4) Create the mail information class mailTextTemplate-java
@data public class MailTextTemplate {/** * MailTextTemplate */ String to; /** * Email subject */ String subject; /** * Email content */ String text; }Copy the code
(5) Create a MailService
Public interface MailService{/** * textMail * @param data mail information */ void textMail(MailTextTemplate data); }Copy the code
(6) Create MailServiceImpl
@Service @Slf4j public class MailServiceImpl implements MailService { @Autowired private JavaMailSender jms; @Value("${spring.mail.username}") private String from; @Override public void textMail(MailTextTemplate data) { if(StringUtils.isNullOrEmpty(data.getTo()) ){ Log.error (" Recipient mailbox is NUll or empty "); return ; } SimpleMailMessage message = new SimpleMailMessage(); Message.setfrom (from); Message.setto (data.getto ()); Message.setsubject (data.getSubject()); Message.settext (data.gettext ()); JMS. Send (message); }}Copy the code
(7) After the customer leaves a message, send an email

We use @eventListener to listen for customer message events and then write the business logic inside

@Service @Slf4j public class CustomerLeaveMessageEventListener { @Resource MailService mailService; @EventListener public void sendEmail(CustomerLeaveMessageEvent event){ CustomerLeaveMessage customerLeaveMessage = event.getCustomerLeaveMessage(); Log.info (" The customer leaves messages and the system sends emails to the administrator "); MailTextTemplate data= new MailTextTemplate(); data.setTo("[email protected]"); / / administrator mailbox data. SetSubject (" website has a new message, please handle as soon as possible, leave a message: "+ customerLeaveMessage. The getName ()); StringBuilder content= new StringBuilder(); Content. append (" message people call: "). Append (customerLeaveMessage. GetPhone ()), append (", a message email: "), append (customerLeaveMessage getEmail ()), append (", the content of the message: "), append (customerLeaveMessage. GetContent ()); data.setText(content.toString()); mailService.textMail(data); }}Copy the code
(8) Test results

3. Reference:

Juejin. Cn/post / 684490…