#
vincentlu
15 小时以前 1bbfdfcecf243ba36512b59da80e41602ad50263
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.zy.acs.manager.common.service;
 
import com.alibaba.fastjson.JSON;
import com.zy.acs.common.utils.Utils;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.manager.system.enums.EmailType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
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 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);
//            helper.setTo(to);
//            helper.setSubject(subject);
//            helper.setText(content);
//
//            mailSender.send(message);
//            return true;
//        } catch (MailException | MessagingException e) {
//            log.error(this.getClass().getSimpleName(), e);
//            return false;
//        }
 
        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();
    }
 
}