调解系统初始化版本
This commit is contained in:
@@ -0,0 +1,436 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
|
||||
|
||||
import com.ruoyi.common.utils.uuid.UUID;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.activation.DataHandler;
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.*;
|
||||
import javax.mail.search.*;
|
||||
import javax.mail.util.ByteArrayDataSource;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.Security;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* @ClassName EmailInUtil
|
||||
* @Description 邮件发送工具
|
||||
*/
|
||||
@Component
|
||||
@Data
|
||||
@Slf4j
|
||||
public class EmailOutUtil {
|
||||
private static Pattern emailPattern = Pattern.compile("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");
|
||||
// private static Pattern phonePattern = Pattern.compile("0?(13|14|15|18)[0-9]{9}");
|
||||
private static Pattern phonePattern = Pattern.compile("^1\\d{10}$");
|
||||
|
||||
|
||||
// @Autowired
|
||||
// private JavaMailSender mailSender;
|
||||
|
||||
// 发送发邮箱地址(外网地址)
|
||||
// @Value("${spring.mail-out-network.from}")
|
||||
// private static String fromOut;
|
||||
@Value("${spring.mail.host}")
|
||||
private String hostOut;
|
||||
// @Value("${spring.mail.username}")
|
||||
// private String usernameOut;
|
||||
private String usernameOut="wq18792927508@163.com";
|
||||
// @Value("${spring.mail.password}")
|
||||
// private String passwordOut;
|
||||
private String passwordOut= "WDFHKSEMCKVRELEA";
|
||||
@Value("${spring.mail.port}")
|
||||
private Integer portOut;
|
||||
|
||||
public JavaMailSender rebuildMailSender() {
|
||||
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
|
||||
mailSender.setHost(hostOut);
|
||||
mailSender.setUsername(usernameOut);
|
||||
mailSender.setPassword(passwordOut);
|
||||
mailSender.setPort(portOut);
|
||||
mailSender.setProtocol("smtp");
|
||||
mailSender.setDefaultEncoding("UTF-8");
|
||||
return mailSender;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送纯文本邮件信息
|
||||
*
|
||||
* @param to 接收方
|
||||
* @param subject 邮件主题
|
||||
* @param content 邮件内容(发送内容)
|
||||
*/
|
||||
public void sendMessage(String to, String subject, String content, String from, JavaMailSender mailSender) {
|
||||
// 创建一个邮件对象
|
||||
SimpleMailMessage msg = new SimpleMailMessage();
|
||||
msg.setFrom(from);
|
||||
msg.setTo(to);
|
||||
// 设置邮件主题
|
||||
msg.setSubject(subject);
|
||||
// 设置邮件内容
|
||||
msg.setText(content);
|
||||
// 发送邮件
|
||||
mailSender.send(msg);
|
||||
////System.out.println("发送成功:" + from + ":to:" + to);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param to 收件人
|
||||
* @param message 邮件内容
|
||||
* @param subject 邮件主题
|
||||
* @param fileList 邮件附件
|
||||
*/
|
||||
public Boolean sendEmil(String to, String message, String subject, List<File> fileList, File file) {
|
||||
try {
|
||||
String messageContent = "<html><body><p style=\"font-family: Arial, sans-serif; font-size: 18px;\">"+message+"。</p></body></html>";
|
||||
MimeBodyPart messageBodyPart = new MimeBodyPart();
|
||||
messageBodyPart.setContent(messageContent, "text/html;charset=utf-8");
|
||||
messageBodyPart.setContentID(UUID.randomUUID().toString());
|
||||
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
|
||||
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
|
||||
//设置邮件会话参数
|
||||
Properties props = new Properties();
|
||||
//邮箱的发送服务器地址
|
||||
props.setProperty("mail.smtp.host", "smtp.163.com");
|
||||
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
|
||||
props.setProperty("mail.smtp.socketFactory.fallback", "false");
|
||||
//邮箱发送服务器端口,这里设置为465端口
|
||||
props.setProperty("mail.smtp.port", "465");
|
||||
props.setProperty("mail.smtp.socketFactory.port", "465");
|
||||
props.put("mail.smtp.auth", "true");
|
||||
//获取到邮箱会话,利用匿名内部类的方式,将发送者邮箱用户名和密码授权给jvm
|
||||
Session session = Session.getDefaultInstance(props, new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(usernameOut, passwordOut);
|
||||
}
|
||||
});
|
||||
//通过会话,得到一个邮件,用于发送
|
||||
Message msg = new MimeMessage(session);
|
||||
//设置发件人
|
||||
msg.setFrom(new InternetAddress(usernameOut));
|
||||
//设置收件人,to为收件人,cc为抄送,bcc为密送
|
||||
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
|
||||
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(to, false));
|
||||
msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(to, false));
|
||||
//设置邮件消息
|
||||
msg.setSubject(subject);
|
||||
msg.setText(message);
|
||||
msg.setDescription("messageutf-8html");
|
||||
//设置发送的日期
|
||||
msg.setSentDate(new Date());
|
||||
// 创建邮件正文
|
||||
MimeMultipart multipart = new MimeMultipart();
|
||||
// MimeBodyPart bodyPart = new MimeBodyPart();
|
||||
// bodyPart.setContent("This is the body of the email", "text/html");
|
||||
multipart.addBodyPart(messageBodyPart);
|
||||
// 添加附件
|
||||
if (file != null) {
|
||||
MimeBodyPart attachmentPart = new MimeBodyPart();
|
||||
attachmentPart.attachFile(file);
|
||||
attachmentPart.setFileName(MimeUtility.encodeText(file.getName()));
|
||||
multipart.addBodyPart(attachmentPart);
|
||||
//将multipart对象放入邮件
|
||||
msg.setContent(multipart);
|
||||
} else if (fileList != null && fileList.size() > 0) {
|
||||
// 添加附件(多个)
|
||||
if (fileList != null && fileList.size() > 0) {
|
||||
for (File tempfile : fileList) {
|
||||
MimeBodyPart attachmentPart = new MimeBodyPart();
|
||||
attachmentPart.attachFile(tempfile);
|
||||
attachmentPart.setFileName(MimeUtility.encodeText(tempfile.getName()));
|
||||
multipart.addBodyPart(attachmentPart);
|
||||
}
|
||||
msg.setContent(multipart);
|
||||
}
|
||||
}
|
||||
//调用Transport的send方法去发送邮件
|
||||
Transport.send(msg);
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送带附件的邮件信息
|
||||
*
|
||||
* @param to 接收方
|
||||
* @param subject 邮件主题
|
||||
* @param content 邮件内容(发送内容)
|
||||
* @param fileList 文件集合 // 可发送多个附件
|
||||
*/
|
||||
public void sendMessageCarryFiles(String to, String subject, String content, List<File> fileList, String from, @NotNull JavaMailSender mailSender) {
|
||||
MimeMessage mimeMessage = mailSender.createMimeMessage();
|
||||
try {
|
||||
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
|
||||
helper.setFrom(from);
|
||||
helper.setTo(to);
|
||||
// 设置邮件主题
|
||||
helper.setSubject(subject);
|
||||
// 设置邮件内容
|
||||
helper.setText(content);
|
||||
// 添加附件(多个)
|
||||
if (fileList != null && fileList.size() > 0) {
|
||||
for (File file : fileList) {
|
||||
helper.addAttachment(file.getName(), file);
|
||||
}
|
||||
}
|
||||
} catch (MessagingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 发送邮件
|
||||
mailSender.send(mimeMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送带附件的邮件信息
|
||||
*
|
||||
* @param to 接收方
|
||||
* @param subject 邮件主题
|
||||
* @param content 邮件内容(发送内容)
|
||||
* @param file 单个文件
|
||||
*/
|
||||
public void sendMessageCarryFile(String to, String subject, String content, File file, String from, JavaMailSender mailSender) {
|
||||
MimeMessage mimeMessage = mailSender.createMimeMessage();
|
||||
try {
|
||||
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
|
||||
helper.setFrom(from);
|
||||
helper.setTo(to);
|
||||
// 设置邮件主题
|
||||
helper.setSubject(subject);
|
||||
// 设置邮件内容
|
||||
helper.setText(content);
|
||||
// 单个附件
|
||||
helper.addAttachment(file.getName(), file);
|
||||
} catch (MessagingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 发送邮件
|
||||
mailSender.send(mimeMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化内外网邮件发送对象
|
||||
*
|
||||
* @param num
|
||||
*/
|
||||
// public static JavaMailSender initJavaMailSender(Integer num) {
|
||||
// if (num != null && num == 1) {
|
||||
// //内网
|
||||
// return rebuildMailSender(hostIn, usernameIn, passwordIn, Integer.parseInt(portIn), "smtps");
|
||||
// } else {
|
||||
// //外网
|
||||
// return rebuildMailSender(hostOut, usernameOut, passwordOut, Integer.parseInt(portOut), "smtps");
|
||||
// }
|
||||
// }
|
||||
|
||||
// public static String getInnerFrom() {
|
||||
// return EmailInUtil;
|
||||
// }
|
||||
//
|
||||
// public static String getOutterFrom() {
|
||||
// return fromOut;
|
||||
// }
|
||||
|
||||
/**
|
||||
* 验证邮箱格式
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static boolean isEmail(String str) {
|
||||
boolean flag = false;
|
||||
Matcher matcher = emailPattern.matcher(str);
|
||||
if (matcher.matches()) {
|
||||
flag = true;
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
public static boolean isPhoneNumber(String str) {
|
||||
boolean flag = false;
|
||||
Matcher matcher = phonePattern.matcher(str);
|
||||
if (matcher.matches()) {
|
||||
flag = true;
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
public void buildReceiveConnect() throws Exception {
|
||||
|
||||
//POP3主机名
|
||||
String host = "pop3.163.com";
|
||||
//设置传输协议
|
||||
String protocol = "pop3";
|
||||
//用户账号
|
||||
String username = "wq18792927508@163.com";
|
||||
//密码或者授权码
|
||||
String password = "WDFHKSEMCKVRELEA";
|
||||
/*
|
||||
* 获取Session
|
||||
*/
|
||||
Properties props = new Properties();
|
||||
//协议
|
||||
props.setProperty("mail.store.protocol", protocol);
|
||||
//POP3主机名
|
||||
props.setProperty("mail.pop3.host", host);
|
||||
props.setProperty("mail.smtp.auth", "true");
|
||||
props.setProperty("mail.pop3.default-encoding", "UTF-8");
|
||||
Session session = Session.getDefaultInstance(props, new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(usernameOut, passwordOut);
|
||||
}
|
||||
});
|
||||
URLName urlName = new URLName(protocol, host, 110, null, username, password);
|
||||
Store store = session.getStore(urlName);
|
||||
store.connect(username, password);
|
||||
|
||||
|
||||
Folder folder = store.getFolder("INBOX");
|
||||
|
||||
folder.open(Folder.READ_ONLY);
|
||||
|
||||
}
|
||||
/**
|
||||
* 接收邮件
|
||||
*/
|
||||
public List<String> receiverMail() {
|
||||
List<String> messageIds=new ArrayList<>();
|
||||
|
||||
// session.setDebug(true);
|
||||
|
||||
try {
|
||||
//POP3主机名
|
||||
String host = "pop3.163.com";
|
||||
//设置传输协议
|
||||
String protocol = "pop3";
|
||||
//用户账号
|
||||
String username = "wq18792927508@163.com";
|
||||
//密码或者授权码
|
||||
String password = "WDFHKSEMCKVRELEA";
|
||||
/*
|
||||
* 获取Session
|
||||
*/
|
||||
Properties props = new Properties();
|
||||
//协议
|
||||
props.setProperty("mail.store.protocol", protocol);
|
||||
//POP3主机名
|
||||
props.setProperty("mail.pop3.host", host);
|
||||
props.setProperty("mail.smtp.auth", "true");
|
||||
props.setProperty("mail.pop3.default-encoding", "UTF-8");
|
||||
Session session = Session.getDefaultInstance(props, new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(usernameOut, passwordOut);
|
||||
}
|
||||
});
|
||||
URLName urlName = new URLName(protocol, host, 110, null, username, password);
|
||||
Store store = session.getStore(urlName);
|
||||
store.connect(username, password);
|
||||
|
||||
|
||||
Folder folder = store.getFolder("INBOX");
|
||||
folder.open(Folder.READ_ONLY);
|
||||
SearchTerm orTerm = new SubjectTerm("退信");
|
||||
// Message[] messages = folder.search(orTerm);
|
||||
Date endTime= new Date();
|
||||
long oneDayMillis=24*60*60*1000L;
|
||||
Date startTime=new Date(endTime.getTime()-oneDayMillis);
|
||||
// SearchTerm comparisonTermGe = new SentDateTerm(ComparisonTerm.GE, startTime);
|
||||
// SearchTerm comparisonTermLe = new SentDateTerm(ComparisonTerm.LE, endTime);
|
||||
// SearchTerm comparisonAndTerm = new AndTerm(comparisonTermGe, comparisonTermLe);
|
||||
// SearchTerm searchTerm = new AndTerm(comparisonAndTerm, orTerm);
|
||||
Message[] messages = folder.search(orTerm);
|
||||
if (messages != null) {
|
||||
|
||||
Arrays.stream(messages).forEach(message -> {
|
||||
String messageId="";
|
||||
try {
|
||||
messageId = EmailUtil.getMessageId(message,session);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
messageIds.add(messageId);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
folder.close(false);
|
||||
store.close();
|
||||
} catch (Exception e) {
|
||||
return messageIds;
|
||||
}
|
||||
return messageIds;
|
||||
}
|
||||
|
||||
public void analyseMail(Session session, Object content) throws Exception {
|
||||
|
||||
if (content instanceof Multipart) {
|
||||
Multipart multipart = (Multipart) content;
|
||||
for (int i = 0; i < multipart.getCount(); i++) {
|
||||
BodyPart bodyPart = multipart.getBodyPart(i);
|
||||
// if (bodyPart.isMimeType("message/rfc822")) { if(bodyPart.getContentType().startsWith("Message/Rfc822"));
|
||||
// MimeMessage mimeMessage = new MimeMessage(session, bodyPart.getInputStream());
|
||||
// }
|
||||
if(bodyPart.isMimeType("Message/Rfc822")){
|
||||
MimeMessage mimeMessage = new MimeMessage(session, bodyPart.getInputStream());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public static String getMessageId(Part part) throws Exception {
|
||||
if (!part.isMimeType("multipart/*")) {
|
||||
return "";
|
||||
}
|
||||
|
||||
Multipart multipart = (Multipart) part.getContent();
|
||||
for (int i = 0; i < multipart.getCount(); i++) {
|
||||
BodyPart bodyPart = multipart.getBodyPart(i);
|
||||
|
||||
if (part.isMimeType("message/rfc822")) {
|
||||
return getMessageId((Part) part.getContent());
|
||||
}
|
||||
InputStream inputStream = bodyPart.getInputStream();
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||
String strLine;
|
||||
while ((strLine = br.readLine()) != null) {
|
||||
if (strLine.startsWith("Message_Id:")) {
|
||||
String[] split = strLine.split("Message_Id:");
|
||||
return split.length > 1 ? split[1].trim() : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
//检查退信邮件
|
||||
// Folder folder = ...; //打开收件箱
|
||||
// Message[] messages = folder.getMessages();
|
||||
// for (Message message : messages) {
|
||||
// if (message.getSubject().contains("Delivery Status Notification")) {
|
||||
// System.out.println("Delivery failed for recipient: " + message.getRecipients()[0]);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user