暂存
This commit is contained in:
@@ -159,6 +159,21 @@
|
||||
<version>3.1.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.activation</groupId>
|
||||
<artifactId>activation</artifactId>
|
||||
<version>1.1.1</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
|
||||
<dependency>
|
||||
<groupId>javax.mail</groupId>
|
||||
<artifactId>mail</artifactId>
|
||||
<version>1.4.7</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,187 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
|
||||
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.mail.MessagingException;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* @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;
|
||||
@Value("${spring.mail.password}")
|
||||
private String passwordOut;
|
||||
@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 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;
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
//package com.ruoyi.common.utils;
|
||||
//
|
||||
//
|
||||
//import jakarta.mail.internet.MimeMessage;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.core.io.FileSystemResource;
|
||||
//import org.springframework.mail.javamail.JavaMailSender;
|
||||
//import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//
|
||||
//import java.io.File;
|
||||
//import java.util.List;
|
||||
//
|
||||
//@Component
|
||||
//public class SendMailUtils {
|
||||
// private static final String SENDER = "xxx@163.com";
|
||||
// @Autowired
|
||||
// private JavaMailSender mailSender;
|
||||
// /**
|
||||
// * 发送带附件的邮件
|
||||
// *
|
||||
// * @param to 收件人
|
||||
// * @param subject 主题
|
||||
// * @param content 内容
|
||||
// * @param fileList 附件
|
||||
// */
|
||||
// public void sendFileMail(String to, String subject, String content, List<File> fileList) {
|
||||
// MimeMessage message = mailSender.createMimeMessage();
|
||||
// try {
|
||||
// //true表示需要创建一个multipart message
|
||||
// MimeMessageHelper helper = new MimeMessageHelper(message, true);
|
||||
// helper.setFrom(SENDER);
|
||||
// helper.setTo(to);
|
||||
// helper.setSubject(subject);
|
||||
// helper.setText(content, true);
|
||||
//
|
||||
// if (fileList != null && fileList.size() > 0) {
|
||||
// for (File file : fileList) {
|
||||
// FileSystemResource fileSystemResource = new FileSystemResource(file);
|
||||
// String fileName = fileSystemResource.getFilename();
|
||||
// helper.addAttachment(fileName, fileSystemResource);
|
||||
// }
|
||||
// }
|
||||
// mailSender.send(message);
|
||||
// } catch (MessagingException e) {
|
||||
// System.out.println("发送带附件的邮件时发生异常!" + e);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
Reference in New Issue
Block a user