如何使用JavaMail

时间:2018-03-07 15:57:54

标签: java javamail

我需要获取cid的值:从阅读时电子邮件正文中包含的图像能够替换cid的值:通过图像的base64,但我无法得到这个价值,有人有任何提示吗?

            try {
       MimeMultipart multipart = (MimeMultipart) msg.getDataHandler().getContent();
       int temp = 0, temp2 = 0;
       for (int k = 0; k < multipart.getCount(); k++) {
          MimeBodyPart bodyPart = (MimeBodyPart) multipart.getBodyPart(k);
          String splited[] = new String[15];
          splited = bodyPart.getContentType().split(" ");

          if (!splited[0].equals("text/plain;") && !splited[0].equals("text/html;")) {
             MimeBodyPart part = bodyPart;
             MimeMultipart p = (MimeMultipart) part.getDataHandler().getContent();
             int count = 0;
             for (int i = 0; i < p.getCount(); i++) {
                String mimeSplited[] = new String[15];
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                Base64 decoder64 = new Base64();
                StringBuilder s = new StringBuilder(eMail.getCorpoEmail());

                System.out.println("CID NAME" + i + " :" + p.getBodyPart(i).getFileName());
                System.out.println("CID TYPE" + i + " :" + p.getBodyPart(i).getContentType());

                if (i != p.getCount() - 1) {
                   p.getBodyPart(i + 1).getDataHandler().writeTo(bos);
                   mimeSplited = p.getBodyPart(i + 1).getContentType().split(" ");
                   s.insert(s.indexOf(":part" + (i + 1)) - 3, "data:" + mimeSplited[0] + " base64," + decoder64.encodeToString(bos.toByteArray()));
                } else {
                   p.getBodyPart(i).getDataHandler().writeTo(bos);
                   mimeSplited = p.getBodyPart(i).getContentType().split(" ");
                   s.insert(s.indexOf(":part" + i) - 3, "data:" + mimeSplited[0] + " base64," + decoder64.encodeToString(bos.toByteArray()));

                }
                eMail.setCorpoEmail(s.toString());
             }
          } else {
          }

这是我的代码,直到现在。

我的新代码:

            try {
           MimeMultipart multipart = (MimeMultipart) msg.getDataHandler().getContent();
           int temp = 0, temp2 = 0;
           System.out.println("EMAIL BODY ->\n"+eMail.getCorpoEmail());
           for (int k = 0; k < multipart.getCount(); k++) {
              MimeBodyPart bodyPart = (MimeBodyPart) multipart.getBodyPart(k);
              String splited[] = new String[15];
              splited = bodyPart.getContentType().split(" ");

              ByteArrayOutputStream bos = new ByteArrayOutputStream();
              String mimeSplited[] = new String[15];
              Base64 decoder64 = new Base64();
              StringBuilder s = new StringBuilder(eMail.getCorpoEmail());
              String editedBody = "";
              int tempPosition = 0;
              if (bodyPart.getContentType().contains("image/")) {
                 MimeBodyPart part = bodyPart;
                 MimeMultipart p = (MimeMultipart) part.getDataHandler().getContent();

                 //THAT FUNCTION RETURNS NULL EVERY TIME
                 System.out.println("ID -> "+part.getContentID());

                 for (int i = 0; i < p.getCount(); i++) {
                    if (i != p.getCount() - 1) {
                       p.getBodyPart(i + 1).getDataHandler().writeTo(bos);
                       mimeSplited = p.getBodyPart(i + 1).getContentType().split(" ");

                       if (mimeSplited[0].equals("text/html;") || mimeSplited[0].equals("text/plain;")) {
                          s.insert(s.indexOf("src=") + 5, "data:" + "image/png;" + " base64," + decoder64.encodeToString(bos.toByteArray()));

                       } else {
                          s.insert(s.indexOf("src=") + 5, "data:" + mimeSplited[0] + " base64," + decoder64.encodeToString(bos.toByteArray()));

                       }
                       tempPosition = (s.indexOf("src=") + 5) + 23 + decoder64.encodeToString(bos.toByteArray()).length();
                       editedBody = editedBody.concat(s.substring(0, tempPosition));
                       s.delete(0, tempPosition);

                    } else {
                       p.getBodyPart(i).getDataHandler().writeTo(bos);
                       mimeSplited = p.getBodyPart(i).getContentType().split(" ");
//                       s.insert(s.indexOf("src=") + 5, "data:" + mimeSplited[0] + " base64," + decoder64.encodeToString(bos.toByteArray()));
                       tempPosition = (s.indexOf("src=") + 5) + 23 + decoder64.encodeToString(bos.toByteArray()).length();
                       editedBody = editedBody.concat(s.toString());
                    }
                    eMail.setCorpoEmail(editedBody);
                    bos.reset();
                 }
              } else {
              }
           }
        } catch (Exception e) {
           System.out.println("error: " + e);
        }

电子邮件正文

&#13;
&#13;
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word"
   xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">

<head>
   <meta http-equiv=Content-Type content="text/html; charset=iso-8859-1">
   <meta name=Generator content="Microsoft Word 14 (filtered medium)">
   <!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
   o\:* {behavior:url(#default#VML);}
   w\:* {behavior:url(#default#VML);}
   .shape {behavior:url(#default#VML);}
   </style><![endif]-->
   <style>
      <!--
      /* Font Definitions */

      @font-face {
         font-family: Calibri;
         panose-1: 2 15 5 2 2 2 4 3 2 4;
      }

      @font-face {
         font-family: Tahoma;
         panose-1: 2 11 6 4 3 5 4 4 2 4;
      }

      /* Style Definitions */

      p.MsoNormal,
      li.MsoNormal,
      div.MsoNormal {
         margin: 0cm;
         margin-bottom: .0001pt;
         font-size: 11.0pt;
         font-family: "Calibri", "sans-serif";
         mso-fareast-language: EN-US;
      }

      a:link,
      span.MsoHyperlink {
         mso-style-priority: 99;
         color: blue;
         text-decoration: underline;
      }

      a:visited,
      span.MsoHyperlinkFollowed {
         mso-style-priority: 99;
         color: purple;
         text-decoration: underline;
      }

      p.MsoAcetate,
      li.MsoAcetate,
      div.MsoAcetate {
         mso-style-priority: 99;
         mso-style-link: "Texto de balão Char";
         margin: 0cm;
         margin-bottom: .0001pt;
         font-size: 8.0pt;
         font-family: "Tahoma", "sans-serif";
         mso-fareast-language: EN-US;
      }

      span.EstiloDeEmail17 {
         mso-style-type: personal-compose;
         font-family: "Calibri", "sans-serif";
         color: windowtext;
      }

      span.TextodebaloChar {
         mso-style-name: "Texto de balão Char";
         mso-style-priority: 99;
         mso-style-link: "Texto de balão";
         font-family: "Tahoma", "sans-serif";
      }

      .MsoChpDefault {
         mso-style-type: export-only;
         font-family: "Calibri", "sans-serif";
         mso-fareast-language: EN-US;
      }

      @page WordSection1 {
         size: 612.0pt 792.0pt;
         margin: 70.85pt 3.0cm 70.85pt 3.0cm;
      }

      div.WordSection1 {
         page: WordSection1;
      }

      -->
   </style>
   <!--[if gte mso 9]><xml>
   <o:shapedefaults v:ext="edit" spidmax="1026" />
   </xml><![endif]-->
   <!--[if gte mso 9]><xml>
   <o:shapelayout v:ext="edit">
   <o:idmap v:ext="edit" data="1" />
   </o:shapelayout></xml><![endif]-->
</head>

<body lang=PT-BR link=blue vlink=purple>
   <div class=WordSection1>
      <p class=MsoNormal>Teste
         <o:p></o:p>
      </p>
      <p class=MsoNormal>Teste
         <o:p></o:p>
      </p>
      <p class=MsoNormal>Teste
         <o:p></o:p>
      </p>
      <p class=MsoNormal>
         <o:p>&nbsp;</o:p>
      </p>
      <p class=MsoNormal>
         <span style='mso-fareast-language:PT-BR'>
            <img width=1678 height=1009 id="Imagem_x0020_2" src="cid:image001.png@01D3BA15.6E238EE0">
         </span>
         <o:p></o:p>
      </p>
      <p class=MsoNormal>
         <o:p>&nbsp;</o:p>
      </p>
      <p class=MsoNormal>Teste
         <o:p></o:p>
      </p>
      <p class=MsoNormal>Teste
         <o:p></o:p>
      </p>
      <p class=MsoNormal>Teste
         <o:p></o:p>
      </p>
      <p class=MsoNormal>
         <span style='mso-fareast-language:PT-BR'>
            <img width=1679 height=595 id="Imagem_x0020_3" src="cid:image002.png@01D3BA15.6E238EE0">
         </span>
         <o:p></o:p>
      </p>
      <p class=MsoNormal>
         <o:p>&nbsp;</o:p>
      </p>
      <p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>
         <span style='font-size:12.0pt;font-family:"Times New Roman","serif";mso-fareast-language:PT-BR'>Atenciosamente,
            <o:p></o:p>
         </span>
      </p>
      <table class=MsoNormalTable border=0 cellpadding=0>
         <tr>
            <td style='padding:.75pt .75pt .75pt .75pt'>
               <p class=MsoNormal style='margin-top:15.0pt'>
                  <span style='font-size:8.5pt;mso-fareast-language:PT-BR'>
                     <img width=35 height=115 id="Imagem_x0020_1" src="cid:image003.jpg@01D3BA15.6E238EE0" alt="Descrição: http://www.kmm.com.br/_imagens/logo_assinatura.jpg">
                     <o:p></o:p>
                  </span>
               </p>
            </td>
            <td style='padding:.75pt .75pt .75pt 15.0pt'>
               <p class=MsoNormal style='margin-top:15.0pt'>
                  <b>
                     <span lang=EN-US style='font-size:8.5pt;color:#990000;mso-fareast-language:PT-BR'>Jeferson Luis Indejejczak</span>
                  </b>
                  <span lang=EN-US style='font-size:8.5pt;color:#333333;mso-fareast-language:PT-BR'>
                     <br>
                  </span>
                  <b>
                     <span lang=EN-US style='font-size:8.5pt;color:black;mso-fareast-language:PT-BR'>Teste</span>
                  </b>
                  <span lang=EN-US style='font-size:8.5pt;color:#333333;mso-fareast-language:PT-BR'>
                     <br>
                  </span>
                  <span style='font-size:8.5pt;color:#333333;mso-fareast-language:PT-BR'>
                     <a href="mailto:jeferson.indejejczak@kmm.com.br">
                        <span lang=EN-US style='color:blue'>jeferson.indejejczak@kmm.com.br</span>
                     </a>
                  </span>
                  <span style='font-size:8.5pt;color:#333333;mso-fareast-language:PT-BR'>
                     <span lang=EN-US>
                        <o:p></o:p>
                     </span>
                  </span>
               </p>
               <p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>
                  <b>
                     <span style='font-size:8.5pt;color:#333333;mso-fareast-language:PT-BR'>KMM Engenharia de Sistemas</span>
                  </b>
                  <span style='font-size:8.5pt;color:#333333;mso-fareast-language:PT-BR'>
                     <br>Rua Padre Anacleto, 396 &#8211; Nova Rússia
                     <br>CEP 84.070-320 &#8211; Ponta Grossa &#8211; PR
                     <br>Tel: (42) 3026-3000
                     <br>
                     <a href="http://www.kmm.com.br/">
                        <span style='color:blue'>www.kmm.com.br</span>
                     </a>
                     <o:p></o:p>
                  </span>
               </p>
            </td>
         </tr>
      </table>
      <p class=MsoNormal>
         <span style='mso-fareast-language:PT-BR'>
            <o:p>&nbsp;</o:p>
         </span>
      </p>
      <p class=MsoNormal>
         <o:p>&nbsp;</o:p>
      </p>
   </div>
</body>

</html>
error: java.lang.ClassCastException: com.sun.mail.util.BASE64DecoderStream cannot be cast to javax.mail.internet.MimeMultipart
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可能希望尝试使用ContentType类来解析ContentType,而不是使用String.split。

可以通过MimeBodyPart.getContentID方法访问每个图像正文部分的Content-ID标头。