引入依赖
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
package com.shucha.deveiface.biz.test;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
/**
* @author tqf
* @Description
* @Version 1.0
* @since 2022-01-20 15:19
*/
public class EmailTest {
public static void main(String[] args) throws AddressException, MessagingException, UnsupportedEncodingException {
// 再创建 MimeMessageHelper 对象之前加上
System.getProperties().setProperty("mail.mime.splitlongparameters", "false");
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");// 连接协议
properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
properties.put("mail.smtp.port", 465);// 端口号
properties.put("mail.smtp.auth", "true");
// properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
//properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
// 得到回话对象
Session session = Session.getInstance(properties);
// 获取邮件对象
Message message = new MimeMessage(session);
// 设置发件人邮箱地址
message.setFrom(new InternetAddress("123456789@qq.com", "发件人姓名", "UTF-8"));
// 设置收件人邮箱地址
//message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("xxx@qq.com"),new InternetAddress("xxx@qq.com"),new InternetAddress("xxx@qq.com")});
/*设置收件人
To收件人 CC 抄送 BCC密送*/
message.setRecipient(Message.RecipientType.TO, new InternetAddress("123456@qq.com"));//一个收件人
message.setRecipient(Message.RecipientType.CC, new InternetAddress("12345878@qq.com"));//一个收件人
// 多个收件人
/*String[] split = emails.split(";");
InternetAddress[] address = new InternetAddress[split.length];
for (int i = 0; i < split.length; i++) {
address[i] = new InternetAddress(split[i]);
}
message.setRecipients(Message.RecipientType.TO, address);
*/
// 设置邮件标题
message.setSubject("测试发送邮件");
// 设置邮件内容
// message.setText("邮件内容邮件内容邮件内容xmqtest");
// 创建消息部分
BodyPart messageBodyPart = new MimeBodyPart();
// 消息内容
messageBodyPart.setText("邮件发送测试,带附件测试");
// 创建多重消息
Multipart multipart = new MimeMultipart();
// 设置文本消息部分
multipart.addBodyPart(messageBodyPart);
// // 附件部分
// messageBodyPart = new MimeBodyPart();
// //把文件,添加到附件1中
// //数据源
// DataSource source = new FileDataSource(new File("D:\\log.xls"));
// //设置第一个附件的数据
// messageBodyPart.setDataHandler(new DataHandler(source));
// //设置附件的文件名
// messageBodyPart.setFileName("日志文件");
//创建附件节点 读取本地文件,并读取附件名称
MimeBodyPart file = new MimeBodyPart();
DataHandler dataHandler = new DataHandler(new FileDataSource("D:\\log.xls"));
file.setDataHandler(dataHandler);
// file.setFileName("日志文件");
file.setFileName(MimeUtility.encodeText(dataHandler.getName()));
MimeBodyPart file1 = new MimeBodyPart();
DataHandler dataHandler1 = new DataHandler(new FileDataSource("D:\\123.xls"));
file1.setDataHandler(dataHandler1);
file1.setFileName(MimeUtility.encodeText("用户信息.xls"));
MimeBodyPart file2 = new MimeBodyPart();
DataHandler dataHandler2 = new DataHandler(new FileDataSource("D:\\解密之后的数据.xls"));
file2.setDataHandler(dataHandler2);
file2.setFileName(MimeUtility.encodeText(dataHandler2.getName()));
multipart.addBodyPart(file);
multipart.addBodyPart(file1);
multipart.addBodyPart(file2);
message.setSentDate(new Date());
message.setContent(multipart);
// 得到邮差对象
Transport transport = session.getTransport();
// 连接自己的邮箱账户
transport.connect("123456789@qq.com", "********");// 密码为QQ邮箱开通的stmp服务后得到的客户端授权码
// 发送邮件
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
}
邮件工具类
package com.shucha.smartreport.util;
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.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author tqf
* @Description 邮件发送
* @Version 1.0
* @since 2022-05-25 10:51
*/
public class EmailUtils {
// 发件人邮箱号
public static String senderAddress = "123@qq.com";
// 授权码
public static String authorizationCode = "*******88";
// 发件人姓名
public static String personalName = "智能报表";
// 编码方式
public static String charset = "UTF-8";
// 邮箱验证规则
private static String REGEX_EMAIL = "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$";
/**
* 报告分享邮件发送
* @param contentMessage 邮件内容
* @param emails 收件人邮箱,多个邮箱使用分号;拼接
* @throws UnsupportedEncodingException
* @throws MessagingException
*/
public static void sendEmail(String contentMessage,String emails) throws UnsupportedEncodingException, MessagingException {
// 再创建 MimeMessageHelper 对象之前加上
// System.getProperties().setProperty("mail.mime.splitlongparameters", "false");
Properties properties = new Properties();
// 连接协议
properties.put("mail.transport.protocol", "smtp");
// 主机名
properties.put("mail.smtp.host", "smtp.qq.com");
// 端口号
properties.put("mail.smtp.port", 465);
properties.put("mail.smtp.auth", "true");
// 设置是否使用ssl安全连接 ---一般都使用
// properties.put("mail.smtp.ssl.enable", "true");
// 设置是否显示debug信息 true 会在控制台显示相关信息
//properties.put("mail.debug", "true");
// 得到回话对象
Session session = Session.getInstance(properties);
// 获取邮件对象
Message message = new MimeMessage(session);
// MimeMessage mimeMessage =new MimeMessage(session);
// 设置发件人邮箱地址
message.setFrom(new InternetAddress(senderAddress, personalName, charset));
// 设置收件人邮箱地址
//message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("xxx@qq.com"),new InternetAddress("xxx@qq.com"),new InternetAddress("xxx@qq.com")});
/*设置收件人
To收件人 CC 抄送 BCC密送*/
// message.setRecipient(Message.RecipientType.TO, new InternetAddress("1234@qq.com"));//一个收件人
// message.setRecipient(Message.RecipientType.CC, new InternetAddress("1234@qq.com"));//一个收件人
// message.setRecipient(Message.RecipientType.BCC, new InternetAddress("1234@qq.com"));//一个收件人
String[] split = emails.split(";");
InternetAddress[] address = new InternetAddress[split.length];
for (int i = 0; i < split.length; i++) {
address[i] = new InternetAddress(split[i]);
}
message.setRecipients(Message.RecipientType.TO, address);
// message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("1234@qq.com"),new InternetAddress("1234@qq.com"),new InternetAddress("1234@qq.com")});
// 设置邮件标题
message.setSubject("报告分享");
// 设置邮件内容
// message.setText("邮件内容邮件内容邮件内容xmqtest");
// 创建消息部分
BodyPart messageBodyPart = new MimeBodyPart();
// 发送的消息内容
messageBodyPart.setText(contentMessage);
// 创建多重消息
Multipart multipart = new MimeMultipart();
// 设置文本消息部分
multipart.addBodyPart(messageBodyPart);
// // 附件部分
// messageBodyPart = new MimeBodyPart();
// //把文件,添加到附件1中
// //数据源
// DataSource source = new FileDataSource(new File("D:\\log.xls"));
// //设置第一个附件的数据
// messageBodyPart.setDataHandler(new DataHandler(source));
// //设置附件的文件名
// messageBodyPart.setFileName("日志文件");
//创建附件节点 读取本地文件,并读取附件名称
/*MimeBodyPart file = new MimeBodyPart();
DataHandler dataHandler = new DataHandler(new FileDataSource("D:\\log.xls"));
file.setDataHandler(dataHandler);
// file.setFileName("日志文件");
file.setFileName(MimeUtility.encodeText(dataHandler.getName()));
MimeBodyPart file1 = new MimeBodyPart();
DataHandler dataHandler1 = new DataHandler(new FileDataSource("D:\\123.xls"));
file1.setDataHandler(dataHandler1);
file1.setFileName("用户信息.xls");
MimeBodyPart file2 = new MimeBodyPart();
DataHandler dataHandler2 = new DataHandler(new FileDataSource("D:\\解密之后的数据.xls"));
file2.setDataHandler(dataHandler2);
file2.setFileName(MimeUtility.encodeText(dataHandler2.getName()));
multipart.addBodyPart(file);
multipart.addBodyPart(file1);
multipart.addBodyPart(file2);*/
message.setSentDate(new Date());
message.setContent(multipart);
// 得到邮差对象
Transport transport = session.getTransport();
// 连接自己的邮箱账户
// 密码为QQ邮箱开通的stmp服务后得到的客户端授权码
transport.connect(senderAddress, authorizationCode);
// 发送邮件
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
/**
* 校验邮箱格式
* @param email
* @return
*/
public static Boolean regexEmail(String email){
Boolean b;
if(email.length() == 0){
b = false;
}else{
// 编译正则表达式
Pattern pattern = Pattern.compile(REGEX_EMAIL);
// 忽略大小写的写法
// Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(email);
// 字符串是否与正则表达式相匹配
boolean isMatch = matcher.matches();
if(isMatch){
b = true;
} else {
b = false;
}
}
return b;
}
public static void main(String[] args) throws UnsupportedEncodingException, MessagingException {
String url = "http://192.168.0.119:8080/reportShare?id=12469611d03a4bd79f7471264777891e";
String passWord = "RF8Y";
// 收件人邮箱 多个是的;拼接
String emails = "123@qq.com;2365@qq.com";
// 消息内容
StringBuffer buffer = new StringBuffer();
buffer.append("链接:"+url+" \n");
buffer.append("提取码:"+passWord+" \n");
sendEmail(buffer.toString(),emails);
}
}