|  |  | 
 |  |  | package com.vincent.rsf.server.common.service; | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson.JSON; | 
 |  |  | import com.vincent.rsf.common.utils.Utils; | 
 |  |  | import com.vincent.rsf.framework.common.Cools; | 
 |  |  | import com.vincent.rsf.server.system.enums.EmailType; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.beans.factory.annotation.Value; | 
 |  |  | 
 |  |  |  | 
 |  |  | import javax.mail.MessagingException; | 
 |  |  | import javax.mail.internet.MimeMessage; | 
 |  |  | import java.util.Map; | 
 |  |  | import java.util.regex.Matcher; | 
 |  |  | import java.util.regex.Pattern; | 
 |  |  |  | 
 |  |  | @Slf4j | 
 |  |  | @Service | 
 |  |  | public class EmailService { | 
 |  |  |  | 
 |  |  |     private static final String EMAIL_REGEX = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$"; | 
 |  |  |  | 
 |  |  |     @Value("${spring.mail.username}") | 
 |  |  |     private String from; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     @SuppressWarnings("all") | 
 |  |  |     private JavaMailSender mailSender; | 
 |  |  |  | 
 |  |  |     public boolean sendVerificationEmail(String to, String verificationCode) { | 
 |  |  |     public boolean sendEmail(String to, EmailType emailType, Map<String, Object> param) { | 
 |  |  |  | 
 |  |  |         String subject = emailType.subject; | 
 |  |  |         String content = emailType.content; | 
 |  |  |         if (!Cools.isEmpty(param) && !param.isEmpty()) { | 
 |  |  |             content = Utils.processTemplate(content, param); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         boolean sendRes = this.send(this.from, to, subject, content); | 
 |  |  |         if (sendRes) { | 
 |  |  |             log.info("Email sent successfully. To: {}, Subject: {}, Parameter: {}" | 
 |  |  |                     , to | 
 |  |  |                     , subject | 
 |  |  |                     , JSON.toJSONString(param)); | 
 |  |  |         } | 
 |  |  |         return sendRes; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public boolean send(String from, String to, String subject, String content) { | 
 |  |  |         try { | 
 |  |  |             MimeMessage message = mailSender.createMimeMessage(); | 
 |  |  |             message.setFrom(from); | 
 |  |  |             MimeMessageHelper helper = new MimeMessageHelper(message, true); | 
 |  |  |  | 
 |  |  |             // 邮件主题和内容 | 
 |  |  |             MimeMessageHelper helper = new MimeMessageHelper(message, true); | 
 |  |  |             helper.setTo(to); | 
 |  |  |             helper.setSubject("Email Verification Code"); | 
 |  |  |             helper.setText("Your verification code is: " + verificationCode); | 
 |  |  |             helper.setSubject(subject); | 
 |  |  |             helper.setText(content); | 
 |  |  |  | 
 |  |  |             mailSender.send(message); | 
 |  |  |             return true; | 
 |  |  | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public boolean isValid(String email) { | 
 |  |  |         if (Cools.isEmpty(email)) { | 
 |  |  |             return false; | 
 |  |  |         } | 
 |  |  |         Pattern pattern = Pattern.compile(EMAIL_REGEX); | 
 |  |  |         Matcher matcher = pattern.matcher(email); | 
 |  |  |         return matcher.matches(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |