新增常量、发送邮件

This commit is contained in:
wenc000 2020-07-31 19:24:54 +08:00
parent 9441aa1c83
commit b137dbdd93
5 changed files with 360 additions and 0 deletions

View File

@ -236,6 +236,12 @@
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
</dependency>
<!-- email start -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
</dependency>
<!-- email end -->
</dependencies>
</project>

View File

@ -181,4 +181,15 @@ public interface ISystemConstant {
*/
String SESSION_WECHAT_ACCESS_TOKEN = "SESSION_WECHAT_ACCESS_TOKEN";
/**
* 日期格式化年月日
*/
String DATE_FORMATTER_YYYY_MM_DD = "yyyy-MM-dd";
/**
* 日期格式化年月日时分秒
*/
String DATE_FORMATTER_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
}

View File

@ -0,0 +1,27 @@
package com.cm.common.utils.email;
/**
* 发送邮件需要使用的基本信息
*
* @version 2.0
*/
import javax.mail.*;
public class MailAuthenticator extends Authenticator {
String userName = null;
String password = null;
public MailAuthenticator() {
}
public MailAuthenticator(String username, String password) {
this.userName = username;
this.password = password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.userName, this.password);
}
}

View File

@ -0,0 +1,180 @@
package com.cm.common.utils.email;
/**
* 发送邮件需要使用的基本信息
*
* @author FH QQ 313596790[青苔]
* 修改时间2015年7月27日
* @version 2.0
*/
import java.util.Arrays;
import java.util.Properties;
public class MailSenderInfo {
/**
* 发送邮件的服务器的IP和端口
*/
private String mailServerHost;
private Integer mailServerPort = 25;
/**
* 邮件发送者的地址
*/
private String fromAddress;
/**
* 邮件接收者的地址
*/
private String toAddress;
/**
* 抄送人
*/
private String toCc;
/**
* 登陆邮件发送服务器的用户名和密码
*/
private String userName;
private String password;
/**
* 是否需要身份验证
*/
private boolean validate = false;
/**
* 邮件主题
*/
private String subject;
/**
* 邮件的文本内容
*/
private String content;
/**
* 邮件附件的文件名
*/
private String[] attachFileNames;
/**
* 获得邮件会话属性
*/
public Properties getProperties() {
Properties properties = new Properties();
properties.put("mail.smtp.host", this.mailServerHost);
properties.put("mail.smtp.port", this.mailServerPort);
properties.put("mail.smtp.auth", validate ? "true" : "false");
return properties;
}
public String getMailServerHost() {
return mailServerHost == null ? "" : mailServerHost.trim();
}
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
public Integer getMailServerPort() {
return mailServerPort;
}
public void setMailServerPort(Integer mailServerPort) {
this.mailServerPort = mailServerPort;
}
public String getFromAddress() {
return fromAddress == null ? "" : fromAddress.trim();
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String getToAddress() {
return toAddress == null ? "" : toAddress.trim();
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
public String getToCc() {
return toCc == null ? "" : toCc.trim();
}
public void setToCc(String toCc) {
this.toCc = toCc;
}
public String getUserName() {
return userName == null ? "" : userName.trim();
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password == null ? "" : password.trim();
}
public void setPassword(String password) {
this.password = password;
}
public boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String getSubject() {
return subject == null ? "" : subject.trim();
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content == null ? "" : content.trim();
}
public void setContent(String content) {
this.content = content;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(String[] attachFileNames) {
this.attachFileNames = attachFileNames;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"mailServerHost\":")
.append("\"").append(mailServerHost).append("\"");
sb.append(",\"mailServerPort\":")
.append("\"").append(mailServerPort).append("\"");
sb.append(",\"fromAddress\":")
.append("\"").append(fromAddress).append("\"");
sb.append(",\"toAddress\":")
.append("\"").append(toAddress).append("\"");
sb.append(",\"toCc\":")
.append("\"").append(toCc).append("\"");
sb.append(",\"userName\":")
.append("\"").append(userName).append("\"");
sb.append(",\"password\":")
.append("\"").append(password).append("\"");
sb.append(",\"validate\":")
.append(validate);
sb.append(",\"subject\":")
.append("\"").append(subject).append("\"");
sb.append(",\"content\":")
.append("\"").append(content).append("\"");
sb.append(",\"attachFileNames\":")
.append(Arrays.toString(attachFileNames));
sb.append('}');
return sb.toString();
}
}

View File

@ -0,0 +1,136 @@
package com.cm.common.utils.email;
import org.apache.commons.lang3.StringUtils;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Properties;
/**
* 邮件发送器
*/
public class SimpleMailSenderUtil {
/**
* 以文本格式发送邮件
*
* @param mailInfo 待发送的邮件的信息
*/
public boolean sendTextMail(MailSenderInfo mailInfo) throws Exception {
Message mailMessage = getMessage(mailInfo);
// 设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
// 发送邮件
Transport.send(mailMessage);
return true;
}
/**
* 以HTML格式发送邮件
*
* @param mailInfo 待发送的邮件信息
*/
public boolean sendHtmlMail(MailSenderInfo mailInfo) throws Exception {
Message mailMessage = getMessage(mailInfo);
// MiniMultipart类是一个容器类包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
// 设置HTML内容
html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
// 发送邮件
Transport.send(mailMessage);
return true;
}
private Message getMessage(MailSenderInfo mailInfo) throws MessagingException {
// 判断是否需要身份认证
MailAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证则创建一个密码验证器
authenticator = new MailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址并设置到邮件消息中
InternetAddress[] to = InternetAddress.parse(mailInfo.getToAddress());
mailMessage.setRecipients(Message.RecipientType.TO, to);
if (!StringUtils.isBlank(mailInfo.getToCc())) {
InternetAddress[] cc = InternetAddress.parse(mailInfo.getToCc());
mailMessage.setRecipients(Message.RecipientType.CC, cc);
}
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
return mailMessage;
}
/**
* @param SMTP 邮件服务器
* @param PORT 端口
* @param EMAIL 本邮箱账号
* @param PAW 本邮箱密码
* @param toEMAIL 对方箱账号
* @param toCC 抄送账号
* @param TITLE 标题
* @param CONTENT 内容
* @param TYPE 1文本格式;2HTML格式
*/
public static boolean sendEmail(String SMTP, Integer PORT, String EMAIL, String PAW, String toEMAIL, String toCC, String TITLE, String CONTENT, String TYPE) throws Exception {
boolean isFlag = StringUtils.isBlank(SMTP) || PORT == null || StringUtils.isBlank(EMAIL) || StringUtils.isBlank(PAW) || StringUtils.isBlank(toEMAIL) || StringUtils.isBlank(TITLE) || StringUtils.isBlank(CONTENT) || StringUtils.isBlank(TYPE);
if (isFlag) {
return false;
}
//这个类主要是设置邮件
MailSenderInfo mailInfo = new MailSenderInfo();
mailInfo.setMailServerHost(SMTP);
mailInfo.setMailServerPort(PORT);
mailInfo.setValidate(true);
mailInfo.setUserName(EMAIL);
mailInfo.setPassword(PAW);
mailInfo.setFromAddress(EMAIL);
mailInfo.setToAddress(toEMAIL);
mailInfo.setToCc(toCC);
mailInfo.setSubject(TITLE);
mailInfo.setContent(CONTENT);
//这个类主要来发送邮件
SimpleMailSenderUtil sms = new SimpleMailSenderUtil();
if ("1".equals(TYPE)) {
return sms.sendTextMail(mailInfo);
}
return sms.sendHtmlMail(mailInfo);
}
public static void main(String[] args) throws Exception {
//这个类主要是设置邮件
MailSenderInfo mailInfo = new MailSenderInfo();
mailInfo.setMailServerHost("smtp.163.com");
mailInfo.setMailServerPort(25);
mailInfo.setValidate(true);
mailInfo.setUserName("dpdbd001@163.com");
mailInfo.setPassword("WAASUQNNVITVXDJS");
mailInfo.setFromAddress("dpdbd001@163.com");
mailInfo.setToAddress("450292408@qq.com ");
mailInfo.setSubject("123");
mailInfo.setContent("123");
SimpleMailSenderUtil simpleMailSenderUtil = new SimpleMailSenderUtil();
simpleMailSenderUtil.sendTextMail(mailInfo);
}
}