JAVA數(shù)據(jù)寫入生成excel文件和發(fā)送郵件
JAVA數(shù)據(jù)寫入生成excel文件和發(fā)送郵件流程:
? 先導(dǎo)包 => 郵箱開啟配置 => java寫好配置類 => 測試發(fā)送 => 數(shù)據(jù)寫入excel => 郵件帶附件發(fā)送
郵箱jar包
<dependencies> <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency> </dependencies>
郵箱開啟配置
我這個是163的,開啟之后會生成授權(quán)碼,記得復(fù)制粘貼到自己的項(xiàng)目配置文件里面去
郵箱配置類
public final class JavaMailUtil { //文件地址 上面是linux地址,下面是我自己本地測試用的 //public final static String FILEPATH = "/wenjie/javaProject/bomexcelfiles"; public final static String FILEPATH = "e:/Users/liuwenj/Desktop/"; private JavaMailUtil() { } public static Session createSession() { //賬號信息 String username = "";//郵箱發(fā)送賬號 String password = "";//郵箱授權(quán)碼 //創(chuàng)建一個配置文件,并保存 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ī)端口號 props.put("mail.smtp.auth", "true");// 是否需要用戶認(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); } }); //控制臺打印調(diào)試信息 session.setDebug(true); return session; } }
測試發(fā)送純文本
//創(chuàng)建Session會話 Session session = JavaMailUtil.createSession(); //創(chuàng)建郵件對象 MimeMessage message = new MimeMessage(session); message.setSubject("主題"); message.setFrom(new InternetAddress("發(fā)送者郵箱")); //發(fā)送給一個人 message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("接收者郵箱")); //發(fā)送給多個人 //message.setRecipients(MimeMessage.RecipientType.CC, new InternetAddress[] {new InternetAddress("接收者郵箱")}); //純文本信息 message.setText("文本信息:來自于公司PLM系統(tǒng)測試"); //發(fā)送 Transport.send(message);
數(shù)據(jù)寫入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>
寫入數(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); } // 寫入數(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)建會話 Session session = JavaMailUtil.createSession(); //創(chuàng)建郵件對象 MimeMessage message = new MimeMessage(session); message.setSubject("主題"); message.setFrom(new InternetAddress("發(fā)送者郵箱")); //發(fā)送給一個人 message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("接收者郵箱")); //發(fā)送給多個人 //message.setRecipients(MimeMessage.RecipientType.CC, new InternetAddress[] {new InternetAddress("接收者郵箱")}); //郵件主體 BodyPart textPart = new MimeBodyPart(); textPart.setContent("文本信息:來自于公司PLM系統(tǒng)測試", "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: 沒有試過,copy的代碼,貼上去以后如果有需求了再驗(yàn)證
Session session = JavaMailUtils.createSession(); //創(chuàng)建session對象 MimeMessage message = new MimeMessage(session); //創(chuàng)建message對象 message.setSubject("測試郵件"); //設(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對象 multipart.addBodyPart(textPart); //將textPart對象放入multipart multipart.addBodyPart(filePart); //將filePartt對象放入multipart message.setContent(multipart); //將multipart對象放入郵件 Transport.send(message); //發(fā)送郵件
到此這篇關(guān)于JAVA數(shù)據(jù)寫入生成excel文件和發(fā)送郵件的文章就介紹到這了,更多相關(guān)JAVA生成excel和發(fā)送郵件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
servlet監(jiān)聽實(shí)現(xiàn)統(tǒng)計在線人數(shù)功能 附源碼下載
這篇文章主要為大家詳細(xì)介紹了servlet監(jiān)聽統(tǒng)計在線人數(shù)的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04關(guān)于SpringMVC的異常處理機(jī)制詳細(xì)解讀
這篇文章主要介紹了關(guān)于SpringMVC的異常處理機(jī)制詳細(xì)解讀,SpringMVC是目前主流的Web?MVC框架之一,本文將分析SpringMVC的異常處理內(nèi)容,需要的朋友可以參考下2023-05-05Java?ConcurrentHashMap實(shí)現(xiàn)線程安全的代碼示例
眾所周知ConcurrentHashMap是HashMap的多線程版本,HashMap?在并發(fā)操作時會有各種問題,而這些問題,只要使用ConcurrentHashMap就可以完美解決了,本文將給詳細(xì)介紹ConcurrentHashMap是如何保證線程安全的2023-05-05logback的FileAppender文件追加模式和沖突檢測解讀
這篇文章主要為大家介紹了logback的FileAppender文件追加模式和沖突檢測解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10Java實(shí)戰(zhàn)之鮮花商城系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了如何利用Java語言實(shí)現(xiàn)鮮花商城系統(tǒng),文中采用的技術(shù)有Spring、SpringMVC、Mybatis、JSP等,感興趣的小伙伴可以了解一下2022-05-05Java類如何實(shí)現(xiàn)一個類的障眼法(JadClipse的bug)
這篇文章主要介紹了Java類實(shí)現(xiàn)一個類的障眼法(JadClipse的bug),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12Java使用BigDecimal進(jìn)行運(yùn)算封裝的實(shí)際案例
今天小編就為大家分享一篇關(guān)于Java使用BigDecimal進(jìn)行運(yùn)算封裝的實(shí)際案例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12