邮件发送案例流的形式文件不落盘
可以实际使用的
public static void main(String[] args) {
// 身份认证
Session session = authorUser();
// 收件人电子邮箱
String from = "邮箱";
// 发件人电子邮箱
String to = "邮箱";
send2User(session, from, to);
}
private static Session authorUser() {
// 获取系统属性
Properties properties = System.getProperties();
// 指定发送邮件的主机为 localhost
String host = "xamail.byd.com";
// 设置邮件服务器
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.port", "25");
properties.put("mail.smtp.auth", "true");
// 判断是否需要身份认证 如果需要身份认证,则创建一个密码验证器
MailAuthenticator authenticator = new MailAuthenticator("邮箱", "123@123");
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session session = Session.getDefaultInstance(properties, authenticator);
return session;
}
private static void send2User(Session session, String from, String to) {
try {
// 创建默认的 MimeMessage 对象
Message message = new MimeMessage(session);
// Set From: 头部头字段
message.setFrom(new InternetAddress(from));
// Set To: 头部头字段
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: 头部头字段
message.setSubject("某项目数据20240822");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("Message test bym!!!");
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment 附件发送
messageBodyPart = new MimeBodyPart();
// 方式一发送附件为文件的形式
// String filename = "D:\\test\\test.txt";
// DataSource source = new FileDataSource(filename);
// messageBodyPart.setDataHandler(new DataHandler(source));
// messageBodyPart.setFileName("test.txt");
// 方式二、发送附件为流的形式,好处:数据直接查询出来,不落文件
List<List<String>> head = head();
List<List<Object>> lists = dataList();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
EasyExcel.write(bos).sheet("sheet1").head(head).doWrite(lists);
byte[] array = bos.toByteArray();
ByteArrayDataSource byteArrayDataSource = new ByteArrayDataSource(array, "application/octet-stream");
messageBodyPart.setDataHandler(new DataHandler(byteArrayDataSource));
messageBodyPart.setFileName("test.xlsx");
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart);
// 发送消息
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
private static List<List<String>> head() {
List<List<String>> list = com.alibaba.excel.util.ListUtils.newArrayList();
List<String> head0 = com.alibaba.excel.util.ListUtils.newArrayList();
head0.add("字符串" + System.currentTimeMillis());
List<String> head1 = com.alibaba.excel.util.ListUtils.newArrayList();
head1.add("数字" + System.currentTimeMillis());
List<String> head2 = com.alibaba.excel.util.ListUtils.newArrayList();
head2.add("日期" + System.currentTimeMillis());
list.add(head0);
list.add(head1);
list.add(head2);
return list;
}
private static List<List<Object>> dataList() {
List<List<Object>> list = com.alibaba.excel.util.ListUtils.newArrayList();
for (int i = 0; i < 10; i++) {
List<Object> data = com.alibaba.excel.util.ListUtils.newArrayList();
data.add("字符串" + i);
data.add(0.56);
data.add(new Date());
list.add(data);
}
return list;
}