溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何解決JavaMail發(fā)送郵件后再通過JavaMail接收格式的問題

發(fā)布時間:2021-07-24 09:22:01 來源:億速云 閱讀:178 作者:小新 欄目:編程語言

小編給大家分享一下如何解決JavaMail發(fā)送郵件后再通過JavaMail接收格式的問題,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

復(fù)雜郵件發(fā)送問題

關(guān)于 JavaMail 如何發(fā)送郵件這里就不贅述了,網(wǎng)上有很多例子。其中最復(fù)雜的郵件發(fā)送莫過于 html郵件包含內(nèi)嵌圖片以及附件,最近項目中的這項功能我在發(fā)送郵件時就出現(xiàn)了一系列問題。

我在使用 JavaMail 發(fā)送了郵件之后,會再次通過 JavaMail 將其獲取回來進(jìn)行解析,由于發(fā)送操作不當(dāng),導(dǎo)致了解析就不是那么回事了。

接下來先看看正常的解析過程吧。關(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("任意的二進(jìn)制數(shù)據(jù): " + disposition);
      //這里就是對附件進(jìn)行解析
      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();
}

使用以上方法對郵件進(jìn)行解析是沒有問題的,內(nèi)嵌圖片、附件都是可以分開解析的。但在我解析通過 JavaMail 發(fā)送的郵件時就出現(xiàn)了問題。

先看看最初是怎么發(fā)送的吧。這里就不貼完整代碼了,我就是按照文章開始鏈接對應(yīng)的文章進(jìn)行了修改。

//這里只給出附件節(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)上文章的例子,這里只需要進(jìn)行如下設(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><h2>這是郵件發(fā)送測試十二</h2><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 并沒有將其作為附件進(jìn)行處理
=========> cids: NULL
---
image/jpeg; name=lyf.jpg  //附件圖片獲取失敗
=========> cids: NULL
---
//最可笑的是居然將我的 file.html 文件當(dāng)做了 text/html 來進(jìn)行了處理, 沒有將 html 文件保存到本地,而是直接輸出了其中的內(nèi)容
//我在手機(jī)郵件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 這個文件又被正常的當(dāng)做了附件處理
//到這里就大概知道其中的原因了
//上面的 圖片和html 文件都是比較特殊的, 但是 Demo.go 就不一樣了
=========> cids: NULL
---
application/octet-stream; name=Demo.go
任意的二進(jìn)制數(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è)置以下幾樣?xùn)|西即可

attachment.setFileName(MimeUtility.encodeText(dh.getName())); //附件名稱
attachment.setDisposition(BodyPart.ATTACHMENT); //設(shè)置 disposition 為 attachment (附件標(biāo)識)
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><h2>這是郵件發(fā)送測試十四</h2><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
任意的二進(jìn)制數(shù)據(jù): attachment
------------------------保存附件 zxd.jpg
=========> cids: NULL
---
application/octet-stream; name=lyf.jpg
任意的二進(jìn)制數(shù)據(jù): attachment
------------------------保存附件 lyf.jpg
=========> cids: NULL
---
application/octet-stream; name=file.html
任意的二進(jìn)制數(shù)據(jù): attachment
------------------------保存附件 file.html
=========> cids: NULL
---
application/octet-stream; name=Demo.go
任意的二進(jìn)制數(shù)據(jù): attachment
------------------------保存附件 Demo.go

這下就正常了,其實解決辦法就是添加上面幾樣配置即可。

看完了這篇文章,相信你對“如何解決JavaMail發(fā)送郵件后再通過JavaMail接收格式的問題”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI