淺析JavaMail發(fā)送郵件后再通過JavaMail接收格式問題
復雜郵件發(fā)送問題
關(guān)于 JavaMail 如何發(fā)送郵件這里就不贅述了,網(wǎng)上有很多例子。其中最復雜的郵件發(fā)送莫過于 html郵件包含內(nèi)嵌圖片以及附件,最近項目中的這項功能我在發(fā)送郵件時就出現(xiàn)了一系列問題。
我在使用 JavaMail 發(fā)送了郵件之后,會再次通過 JavaMail 將其獲取回來進行解析,由于發(fā)送操作不當,導致了解析就不是那么回事了。
接下來先看看正常的解析過程吧。關(guān)于郵件的解析,網(wǎng)上依然有很多例子。
private static void multipartMailParser(Multipart mail) throws Exception { int count = mail.getCount(); System.out.println("part count: " + count); for (int i = 0; i < count; i++) { BodyPart bodyPart = mail.getBodyPart(i); //cid 解析 //cid 示例: <4a85b9c9$1$16af704cfc3$Coremail$user_taohan$163.com> String[] cids = bodyPart.getHeader("Content-Id"); System.out.println("=========> cids: " + (cids == null ? "NULL" : cids.length)); String content = "", cid = ""; if (cids != null && cids.length > 0) { content = cids[0]; if (content.startsWith("<") && content.endsWith(">")) { cid = "cid:" + content.substring(1, content.length() - 1); } else { cid = "cid:" + content; } } System.out.println(content+"---"+cid); System.out.println(bodyPart.getContentType()); if (bodyPart.isMimeType("text/plain")) { System.out.println("純文本郵件: " + bodyPart.getContent()); } else if (bodyPart.isMimeType("text/html")) { System.out.println("html郵件: " + bodyPart.getContent()); } else if (bodyPart.isMimeType("multipart/*")) { Multipart part = (Multipart)bodyPart.getContent(); multipartMailParser(part); } else if (bodyPart.isMimeType("application/octet-stream")) { String disposition = bodyPart.getDisposition(); System.out.println("任意的二進制數(shù)據(jù): " + disposition); //這里就是對附件進行解析 if (disposition.equalsIgnoreCase(BodyPart.ATTACHMENT)) { String fileName = bodyPart.getFileName(); System.out.println("------------------------保存附件 " + fileName); InputStream is = bodyPart.getInputStream(); File file = new File("C:\\Users\\AB\\Desktop\\mail\\"+fileName); copy(is, new FileOutputStream(file)); } } else if (bodyPart.isMimeType("image/*") && !("".equals(cid))) { //內(nèi)嵌圖片處理 DataHandler dataHandler = bodyPart.getDataHandler(); String name = dataHandler.getName(); System.out.println("內(nèi)嵌圖片 NAME: " + name); InputStream is = dataHandler.getInputStream(); File file = new File("C:\\Users\\AB\\Desktop\\mail\\"+name); copy(is, new FileOutputStream(file)); } } } private static void copy(InputStream is, OutputStream os) throws IOException { byte[] bytes = new byte[1024]; int len = 0; while ((len=is.read(bytes)) != -1 ) { os.write(bytes, 0, len); } if (os != null) os.close(); if (is != null) is.close(); }
使用以上方法對郵件進行解析是沒有問題的,內(nèi)嵌圖片、附件都是可以分開解析的。但在我解析通過 JavaMail 發(fā)送的郵件時就出現(xiàn)了問題。
先看看最初是怎么發(fā)送的吧。這里就不貼完整代碼了,我就是按照文章開始鏈接對應(yīng)的文章進行了修改。
//這里只給出附件節(jié)點創(chuàng)建方法吧 //給出參數(shù)accessory(附件信息)格式為: zxd.jpg-C:/Users/AB/Desktop/zxd.jpg,lyf.jpg-C:/Users/AB/Desktop/lyf.jpg,htmlFile-C:/Users/AB/Desktop/file.html,golang-C:/Users/AB/Desktop/Demo.go private List<MimeBodyPart> mailAttachmentParts(String accessory) throws MessagingException, UnsupportedEncodingException { //附件節(jié)點集合 List<MimeBodyPart> attachments = new ArrayList<>(); MimeBodyPart attachment; DataHandler dh; String[] accs = accessory.split(","); for (final String acc : accs) { String[] ac = acc.split("-"); //按照網(wǎng)上文章的例子,這里只需要進行如下設(shè)置即可 attachment = new MimeBodyPart(); dh = new DataHandler(new FileDataSource(ac[1])); attachment.setDataHandler(dh); attachments.add(attachment); } return attachments; }
發(fā)送后,查看去郵件服務(wù)器中查看,郵件是正常的。但是我再通過 JavaMail 獲取就出現(xiàn)問題了。輸出如下:(//…為我給出的注釋)
part count: 5 =========> cids: NULL --- multipart/related; boundary="----=_Part_2_1562389956.1559641692502" part count: 2 =========> cids: NULL --- text/html; charset=UTF-8 //郵件內(nèi)容正常獲取 html郵件: <html><body><h1>這是郵件發(fā)送測試十二</h1><b>這依然是劉亦菲</b><br><br><br><img src='cid:liuyifei' /></body></html> //內(nèi)嵌圖片也正常獲取 =========> cids: 1 liuyifei---cid:liuyifei image/jpeg; name=lyf2.jpg 內(nèi)嵌圖片 NAME: lyf2.jpg =========> cids: NULL --- image/jpeg; name=zxd.jpg //附件圖片獲取失敗, 可以看見前面為 image/jpeg,也就是說 JavaMail 并沒有將其作為附件進行處理 =========> cids: NULL --- image/jpeg; name=lyf.jpg //附件圖片獲取失敗 =========> cids: NULL --- //最可笑的是居然將我的 file.html 文件當做了 text/html 來進行了處理, 沒有將 html 文件保存到本地,而是直接輸出了其中的內(nèi)容 //我在手機郵件app中查看這封郵件的時候, 我的郵件內(nèi)容并不是上面打印的內(nèi)容, 而是這個 html 文件中的內(nèi)容 text/html; charset=us-ascii; name=file.html html郵件: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>file upload</title> </head> <body> <div> <form action="http://localhost:8080/api/mail/record/accessory/upload" method="POST" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="Submit"> </form> </div> </body> </html> //但奇怪的是 Demo.go 這個文件又被正常的當做了附件處理 //到這里就大概知道其中的原因了 //上面的 圖片和html 文件都是比較特殊的, 但是 Demo.go 就不一樣了 =========> cids: NULL --- application/octet-stream; name=Demo.go 任意的二進制數(shù)據(jù): attachment ------------------------保存附件 Demo.go
于是,我將需要發(fā)送的郵件保存至本地, message.writeTo(new FileOutputStream("D/mail.eml")) 即可;
打開查看其中的內(nèi)容發(fā)現(xiàn)
附件都有如下內(nèi)容:
Content-Type: image/jpeg; name=lyf.jpg Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=lyf.jpg ————————————————————— Content-Type: application/octet-stream; name=Demo.go Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=Demo.go
而對比上面獲取郵件的輸出內(nèi)容,只有 Demo.go 這一個文件輸出了 application/octet-stream 字樣, 所以問題就已經(jīng)變得很明顯了。
只需要在創(chuàng)建附件節(jié)點時,為附件節(jié)點設(shè)置以下幾樣東西即可
attachment.setFileName(MimeUtility.encodeText(dh.getName())); //附件名稱 attachment.setDisposition(BodyPart.ATTACHMENT); //設(shè)置 disposition 為 attachment (附件標識) attachment.setHeader("content-disposition", "attachment; filename="+dh.getName()); //設(shè)置以下兩個 header attachment.setHeader("content-type", "application/octet-stream; name="+dh.getName());
再看獲取郵件輸出:
part count: 5 =========> cids: NULL ---multipart/related; boundary="----=_Part_2_1714832523.1559700934372" part count: 2 =========> cids: NULL --- text/html; charset=UTF-8 html郵件: <html><body><h1>這是郵件發(fā)送測試十四</h1><b>這依然是劉亦菲</b><br><br><br><img src='cid:liuyifei' /></body></html> =========> cids: 1 liuyifei---cid:liuyifei image/jpeg; name=lyf2.jpg 內(nèi)嵌圖片 NAME: lyf2.jpg =========> cids: NULL --- application/octet-stream; name=zxd.jpg 任意的二進制數(shù)據(jù): attachment ------------------------保存附件 zxd.jpg =========> cids: NULL --- application/octet-stream; name=lyf.jpg 任意的二進制數(shù)據(jù): attachment ------------------------保存附件 lyf.jpg =========> cids: NULL --- application/octet-stream; name=file.html 任意的二進制數(shù)據(jù): attachment ------------------------保存附件 file.html =========> cids: NULL --- application/octet-stream; name=Demo.go 任意的二進制數(shù)據(jù): attachment ------------------------保存附件 Demo.go
這下就正常了,其實解決辦法就是添加上面幾樣配置即可。
總結(jié)
以上所述是小編給大家介紹的JavaMail發(fā)送郵件后再通過JavaMail接收格式問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Spring的Bean注入解析結(jié)果BeanDefinition詳解
這篇文章主要介紹了Spring的Bean注入解析結(jié)果BeanDefinition詳解,BeanDefinition描述了一個bean實例,擁有屬性值、構(gòu)造參數(shù)值和具體實現(xiàn)的其他信息,其是一個bean的元數(shù)據(jù),xml中配置的bean元素會被解析成BeanDefinition對象,需要的朋友可以參考下2023-12-12Java中的Set接口實現(xiàn)類HashSet和LinkedHashSet詳解
這篇文章主要介紹了Java中的Set接口實現(xiàn)類HashSet和LinkedHashSet詳解,Set接口和java.util.List接口一樣,同樣繼承自Collection接口,它與Collection接口中的方法基本一致,并沒有對Collection接口進行功能上的擴充,只是比Collection接口更加嚴格了,需要的朋友可以參考下2024-01-01MyEclipse設(shè)置Console輸出到文件的實現(xiàn)方法
下面小編就為大家?guī)硪黄狹yEclipse設(shè)置Console輸出到文件的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07Intellij Idea 多模塊Maven工程中模塊之間無法相互引用問題
這篇文章主要介紹了Intellij Idea 多模塊Maven工程中模塊之間無法相互引用問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01