JAVA數(shù)據(jù)寫(xiě)入生成excel文件和發(fā)送郵件
JAVA數(shù)據(jù)寫(xiě)入生成excel文件和發(fā)送郵件流程:
? 先導(dǎo)包 => 郵箱開(kāi)啟配置 => java寫(xiě)好配置類(lèi) => 測(cè)試發(fā)送 => 數(shù)據(jù)寫(xiě)入excel => 郵件帶附件發(fā)送
郵箱jar包
<dependencies>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>郵箱開(kāi)啟配置
我這個(gè)是163的,開(kāi)啟之后會(huì)生成授權(quán)碼,記得復(fù)制粘貼到自己的項(xiàng)目配置文件里面去

郵箱配置類(lèi)
public final class JavaMailUtil {
//文件地址 上面是linux地址,下面是我自己本地測(cè)試用的
//public final static String FILEPATH = "/wenjie/javaProject/bomexcelfiles";
public final static String FILEPATH = "e:/Users/liuwenj/Desktop/";
private JavaMailUtil() {
}
public static Session createSession() {
//賬號(hào)信息
String username = "";//郵箱發(fā)送賬號(hào)
String password = "";//郵箱授權(quán)碼
//創(chuàng)建一個(gè)配置文件,并保存
Properties props = new Properties();
//SMTP服務(wù)器連接信息
//126——smtp.126.com
//163——smtp.163.com
props.put("mail.smtp.host", "smtp.126.net");//SMTP主機(jī)名
//126——25
//163——645 如果645一直連不上,可以換成25試一試
props.put("mail.smtp.port", "25");// 主機(jī)端口號(hào)
props.put("mail.smtp.auth", "true");// 是否需要用戶(hù)認(rèn)證
props.put("mail.smtp.starttls.enable", "true");// 啟用TlS加密
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
return new PasswordAuthentication(username, password);
}
});
//控制臺(tái)打印調(diào)試信息
session.setDebug(true);
return session;
}
}
測(cè)試發(fā)送純文本
//創(chuàng)建Session會(huì)話(huà)
Session session = JavaMailUtil.createSession();
//創(chuàng)建郵件對(duì)象
MimeMessage message = new MimeMessage(session);
message.setSubject("主題");
message.setFrom(new InternetAddress("發(fā)送者郵箱"));
//發(fā)送給一個(gè)人
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("接收者郵箱"));
//發(fā)送給多個(gè)人
//message.setRecipients(MimeMessage.RecipientType.CC, new InternetAddress[] {new InternetAddress("接收者郵箱")});
//純文本信息
message.setText("文本信息:來(lái)自于公司PLM系統(tǒng)測(cè)試");
//發(fā)送
Transport.send(message);
數(shù)據(jù)寫(xiě)入excel,并生成文件
先導(dǎo)jar
<!-- Apache POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
寫(xiě)入數(shù)據(jù)到excel
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Bom");
//第一行的數(shù)據(jù)名
List<String> labelList = Arrays.asList("物料編碼", "描述");
Row row = sheet.createRow(0);
for (int i = 0; i < labelList.size(); i++) {
row.createCell(i).setCellValue(labelList.get(i));
}
int rowNo = 1;
//獲取數(shù)據(jù)然后放入Excel
List<Map<String, Object>> bomList = plmDataService.getBomNotPerfect();
for (Map<String, Object> bom : bomList) {
String no = (String) bom.get("ITEM_NUMBER");
String desc = (String) bom.get("DESCRIPTION");
Row tempRow = sheet.createRow(rowNo);
rowNo++;
tempRow.createCell(0).setCellValue(no);
tempRow.createCell(1).setCellValue(desc);
}
// 寫(xiě)入數(shù)據(jù)到Excel
try (FileOutputStream outputStream = new FileOutputStream(JavaMailUtil.FILEPATH + fileName)) {
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
workbook.close(); //關(guān)閉工作簿
} catch (IOException e) {
e.printStackTrace();
}
}
一切準(zhǔn)備就緒之后就可以發(fā)送郵件了
發(fā)送郵件(帶附件)
//創(chuàng)建會(huì)話(huà)
Session session = JavaMailUtil.createSession();
//創(chuàng)建郵件對(duì)象
MimeMessage message = new MimeMessage(session);
message.setSubject("主題");
message.setFrom(new InternetAddress("發(fā)送者郵箱"));
//發(fā)送給一個(gè)人
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("接收者郵箱"));
//發(fā)送給多個(gè)人
//message.setRecipients(MimeMessage.RecipientType.CC, new InternetAddress[] {new InternetAddress("接收者郵箱")});
//郵件主體
BodyPart textPart = new MimeBodyPart();
textPart.setContent("文本信息:來(lái)自于公司PLM系統(tǒng)測(cè)試", "text/html;charset=utf-8");
//郵件附件
BodyPart filePart = new MimeBodyPart();
filePart.setFileName(fileName);
//提交附件文件
filePart.setDataHandler(new DataHandler(new ByteArrayDataSource(Files.readAllBytes(Paths.get(JavaMailUtil.FILEPATH + fileName)), "application/octet-stream")));
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(textPart);
multipart.addBodyPart(filePart);
//將郵件裝入信封
message.setContent(multipart);
//發(fā)送
Transport.send(message);
如果有發(fā)送一些特殊需求,比如需要內(nèi)嵌圖片HTML
發(fā)送郵件(內(nèi)嵌圖片HTML)
PS: 沒(méi)有試過(guò),copy的代碼,貼上去以后如果有需求了再驗(yàn)證
Session session = JavaMailUtils.createSession(); //創(chuàng)建session對(duì)象
MimeMessage message = new MimeMessage(session); //創(chuàng)建message對(duì)象
message.setSubject("測(cè)試郵件"); //設(shè)置郵件標(biāo)題
message.setFrom(new InternetAddress("xxxxxx@163.com")); //設(shè)置發(fā)送方地址
message.setRecipient(RecipientType.TO, new InternetAddress("xxxxxx@qq.com")); //設(shè)置接收方地址
message.setRecipients(RecipientType.CC, new InternetAddress[] {new InternetAddress("xxxxxx@qq.com"),new InternetAddress("xxxxxx@qq.com")}); //群發(fā)(抄送多人)
//正文
BodyPart textPart=new MimeBodyPart();
StringBuilder contentText=new StringBuilder();
contentText.append("<h3>網(wǎng)易郵箱/h3>");
contentText.append("<p>給QQ郵箱發(fā)消息了!</p>");
contentText.append("<img src=\"cid:xxx\"/>");
textPart.setContent(contentText.toString(),"text/html;charset=utf-8");
//附件
BodyPart imagePart=new MimeBodyPart();
imagePart.setDataHandler(new DataHandler(
new ByteArrayDataSource(Files.readAllBytes(Paths.get("D://test//1.jpg")), "application/octet-stream"))); //
imagePart.setHeader("Content-ID", "xxx"); //圖片的內(nèi)容ID
Multipart multipart=new MimeMultipart(); //創(chuàng)建multipart對(duì)象
multipart.addBodyPart(textPart); //將textPart對(duì)象放入multipart
multipart.addBodyPart(filePart); //將filePartt對(duì)象放入multipart
message.setContent(multipart); //將multipart對(duì)象放入郵件
Transport.send(message); //發(fā)送郵件
到此這篇關(guān)于JAVA數(shù)據(jù)寫(xiě)入生成excel文件和發(fā)送郵件的文章就介紹到這了,更多相關(guān)JAVA生成excel和發(fā)送郵件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java動(dòng)態(tài)修改配置即時(shí)生效的方式WatchService
這篇文章給大家分享了Java動(dòng)態(tài)修改配置即時(shí)生效的方式WatchService的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友可以參考學(xué)習(xí)下。2018-06-06
java定長(zhǎng)隊(duì)列的實(shí)現(xiàn)示例
定長(zhǎng)隊(duì)列是一種有限容量的隊(duì)列,對(duì)于某些應(yīng)用場(chǎng)景非常有用,本文主要介紹了java定長(zhǎng)隊(duì)列的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
jar運(yùn)行報(bào)錯(cuò)no main manifest attribute原因分析及解決
文章主要討論了在使用`java -jar`命令運(yùn)行jar包時(shí)遇到的“nomainmanifestattribute”錯(cuò)誤,并提供了可能的解決方案,包括檢查pom.xml配置,添加打包插件配置,以及使用IDEA進(jìn)行打包等方法2026-04-04
Java 高并發(fā)十: JDK8對(duì)并發(fā)的新支持詳解
本文主要介紹Java 高并發(fā)JDK8的支持,這里整理了詳細(xì)的資料及1. LongAdder 2. CompletableFuture 3. StampedLock的介紹,有興趣的小伙伴可以參考下2016-09-09
將項(xiàng)目上傳到Maven中央倉(cāng)庫(kù)(2023最新版)
本文主要介紹了將項(xiàng)目上傳到Maven中央倉(cāng)庫(kù)(2023最新版),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
SpringBoot實(shí)現(xiàn)對(duì)超大文件進(jìn)行異步壓縮下載的使用示例
在Web應(yīng)用中,文件下載功能是一個(gè)常見(jiàn)的需求,本文介紹了SpringBoot實(shí)現(xiàn)對(duì)超大文件進(jìn)行異步壓縮下載的使用示例,具有一定的參考價(jià)值,感興趣的可以了解一下,2023-09-09

