|
@@ -0,0 +1,125 @@
|
|
|
+package cn.kdan.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.kdan.entity.Users;
|
|
|
+import cn.kdan.service.UsersService;
|
|
|
+import cn.kdan.mapper.UsersMapper;
|
|
|
+import cn.kdan.utils.EmailUtil;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.mail.Folder;
|
|
|
+import javax.mail.Message;
|
|
|
+import javax.mail.Session;
|
|
|
+import javax.mail.Store;
|
|
|
+import javax.mail.search.AndTerm;
|
|
|
+import javax.mail.search.FromStringTerm;
|
|
|
+import javax.mail.search.SearchTerm;
|
|
|
+import javax.mail.search.SubjectTerm;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Properties;
|
|
|
+
|
|
|
+/**
|
|
|
+* @author kdan
|
|
|
+* @description 针对表【users(官网用户信息)】的数据库操作Service实现
|
|
|
+* @createDate 2024-06-13
|
|
|
+*/
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService{
|
|
|
+
|
|
|
+ @Value("${workflow.host}")
|
|
|
+ private String host;
|
|
|
+ @Value("${workflow.port}")
|
|
|
+ private String port;
|
|
|
+ @Value("${workflow.user}")
|
|
|
+ private String user;
|
|
|
+ @Value("${workflow.appPassword}")
|
|
|
+ private String appPassword;
|
|
|
+ @Value("${workflow.blockedStatus}")
|
|
|
+ private String blockedStatus;
|
|
|
+ @Value("${workflow.notFoundStatus}")
|
|
|
+ private String notFoundStatus;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public JSONObject checkInbox() {
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ List<String> blockedEmailList = new ArrayList<>();
|
|
|
+ List<String> notFoundEmailList = new ArrayList<>();
|
|
|
+
|
|
|
+ Properties properties = new Properties();
|
|
|
+ properties.put("mail.store.protocol", "imaps");
|
|
|
+ properties.put("mail.imap.host", host);
|
|
|
+ properties.put("mail.imap.port", port);
|
|
|
+ properties.put("mail.imap.starttls.enable", "true");
|
|
|
+ properties.put("mail.imap.ssl.trust", "*");
|
|
|
+ properties.put("mail.imap.ssl.enable", "true");
|
|
|
+ Session session = Session.getInstance(properties);
|
|
|
+ try {
|
|
|
+ // 调试模式
|
|
|
+// session.setDebug(true);
|
|
|
+ // Connect to the store
|
|
|
+ Store store = session.getStore("imap");
|
|
|
+ // 使用应用专用密码
|
|
|
+ store.connect(user, appPassword);
|
|
|
+ // Open the inbox folder
|
|
|
+ Folder inbox = store.getFolder("INBOX");
|
|
|
+ inbox.open(Folder.READ_ONLY);
|
|
|
+ // Fetch messages from the inbox
|
|
|
+ Message[] messages = inbox.getMessages();
|
|
|
+ log.info("收件箱总计邮件数: {}", messages.length);
|
|
|
+
|
|
|
+ // 创建发件人和主题的搜索条件
|
|
|
+ SearchTerm senderTerm = new FromStringTerm("Mail Delivery Subsystem <mailer-daemon@googlemail.com>");
|
|
|
+// SearchTerm senderTerm = new FromStringTerm("no-reply@pdfreaderpro1.com");
|
|
|
+ SearchTerm subjectTerm = new SubjectTerm("Delivery Status Notification (Failure)");
|
|
|
+ SearchTerm searchTerm = new AndTerm(senderTerm, subjectTerm);
|
|
|
+ // 搜索匹配的邮件
|
|
|
+ Message[] inboxFailureMail = inbox.search(searchTerm);
|
|
|
+ System.out.println("邮件发送失败反馈邮件数: " + inboxFailureMail.length);
|
|
|
+ // 获取收件箱失败投递邮件
|
|
|
+ for (int i = 0; i < inboxFailureMail.length; i++) {
|
|
|
+ Message message = inboxFailureMail[i];
|
|
|
+ log.info("Email Number {}", (i + 1));
|
|
|
+ System.out.println("Subject: " + message.getSubject());
|
|
|
+ System.out.println("From: " + message.getFrom()[0]);
|
|
|
+ String body = EmailUtil.getTextFromMessage(message);
|
|
|
+ int status;
|
|
|
+ if (body.contains(blockedStatus)) {
|
|
|
+ status = 1;
|
|
|
+ body = body.substring(body.indexOf(blockedStatus));
|
|
|
+ } else if (body.contains(notFoundStatus)) {
|
|
|
+ status = 2;
|
|
|
+ body = body.substring(body.indexOf(notFoundStatus));
|
|
|
+ } else {
|
|
|
+ log.warn("未知的失败邮件,邮件内容:{}", body);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String targetEmail = EmailUtil.extractTargetEmail(body);
|
|
|
+ log.info("目标邮箱:{}", targetEmail);
|
|
|
+ if (status == 1) {
|
|
|
+ blockedEmailList.add(targetEmail);
|
|
|
+ } else {
|
|
|
+ notFoundEmailList.add(targetEmail);
|
|
|
+ }
|
|
|
+ // 更新数据库状态
|
|
|
+ Users users = new Users();
|
|
|
+ users.setEmail(targetEmail);
|
|
|
+ users.setEmailStatus(status);
|
|
|
+ this.baseMapper.updatebyEmail(users);
|
|
|
+ }
|
|
|
+ // Close the folder and store
|
|
|
+ inbox.close(false);
|
|
|
+ store.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ jsonObject.set("已屏蔽的邮箱", blockedEmailList);
|
|
|
+ jsonObject.set("找不到的邮箱", notFoundEmailList);
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|