java - 解析 .p7m 文件时缺少附件

标签 java certificate jakarta-mail exchangewebservices

I have a p7m file in my disk which contains 1 jpg picture, 1 txt file and a email body context. Fortunately, I can get the email body text and the p7s file. I can verify the p7s file(certificate), but I can not find the two attachments. It looks like missing. I don't know why? Anyone has meet this issue? And the p7m file is from EWS(Outlook).

  public static void main(String args[]) {
try {
  DataSource source = new FileDataSource("C:/Users/zhontao/Desktop/smime.p7m");
  MimeMultipart multi1 = new MimeMultipart(source);
  for (int i = 0; i < multi1.getCount(); i++) {
    Part part1 = multi1.getBodyPart(i);
    if (part1.getContent() instanceof Multipart) {
      Multipart multi2 = (Multipart) part1.getContent();
      for (int j = 0; j < multi2.getCount(); j++) {
        Part part2 = multi2.getBodyPart(i);
        String contentType = part2.getContentType();
        System.out.println(contentType);
        // generally if the content type multipart/alternative, it is email text.
        if (part2.isMimeType("multipart/alternative")) {
          if (part2.getContent() instanceof Multipart) {
            Multipart multi3 = (Multipart) part2.getContent();
            for (int k = 0; k < multi3.getCount(); k++) {
              Part part4 = multi3.getBodyPart(k);
              String contentType1 = part4.getContentType();
              System.out.println(contentType1);
              if (part4.isMimeType("text/plain") && !Part.ATTACHMENT.equalsIgnoreCase(part4.getDisposition())) {
                System.out.println(part4.getContent().toString());
              } else if (part4.isMimeType("text/html") && !Part.ATTACHMENT.equalsIgnoreCase(part4.getDisposition())) {
                System.out.println(part4.getContent().toString());
              }
            }
          }
        }
      }
    } else {
      String contentType = part1.getContentType();
      System.out.println(contentType);
      String disposition = part1.getDisposition();
      System.out.println(disposition);
      System.out.println(part1.getFileName());
      // this.saveFile(part1.getFileName(), part1.getInputStream());
    }
  }
} catch (MessagingException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
} catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

}

最佳答案

Part part2 = multi2.getBodyPart(i);

应该是

Part part2 = multi2.getBodyPart(j);

关于java - 解析 .p7m 文件时缺少附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35077552/

相关文章:

java - 如何让 Aspect 触发带注释的函数?

java - 从常规列表中删除特定值

java - 使用 javax.mail(或类似的)查找可用的邮件存储、可用空间等

winapi - 在 CertAddEncodedCertificateToSystemStore 对面从 Windows 证书存储中获取 ASN1 证书?

java - 如何使用 groovy 将 html 模板作为邮件发送

session - 如何在带有 GMail 的 Websphere 中使用默认 Javamail session

java - 如何在 spring mvc 中为 LocalDate 注册全局数据绑定(bind)?

java - 无法让 JFrame 刷新图像

wcf - 开发人员证书与为 WCF 购买的证书

powershell - 未获取所需状态配置凭据私钥