示例#1
0
  /**
   * Split mail message into several messages. Each body and each attachment results in separate
   * message invoked on endpoint adapter. Mail message response if any should be sent only once
   * within test case. However latest mail response sent by test case is returned, others are
   * ignored.
   *
   * @param bodyPart
   * @param messageHeaders
   */
  private Message split(BodyPart bodyPart, Map<String, Object> messageHeaders) {
    MailMessage mailMessage = createMailMessage(messageHeaders);
    mailMessage.setBody(new BodyPart(bodyPart.getContent(), bodyPart.getContentType()));

    StringResult result = new StringResult();
    Stack<Message> responseStack = new Stack<Message>();
    if (bodyPart instanceof AttachmentPart) {
      marshaller.marshal(mailMessage, result);
      fillStack(
          getEndpointAdapter()
              .handleMessage(
                  new DefaultMessage(result.toString(), messageHeaders)
                      .setHeader(
                          CitrusMailMessageHeaders.MAIL_CONTENT_TYPE, bodyPart.getContentType())
                      .setHeader(
                          CitrusMailMessageHeaders.MAIL_FILENAME,
                          ((AttachmentPart) bodyPart).getFileName())),
          responseStack);
    } else {
      marshaller.marshal(mailMessage, result);
      fillStack(
          getEndpointAdapter()
              .handleMessage(
                  new DefaultMessage(result.toString(), messageHeaders)
                      .setHeader(
                          CitrusMailMessageHeaders.MAIL_CONTENT_TYPE, bodyPart.getContentType())),
          responseStack);
    }

    if (bodyPart.hasAttachments()) {
      for (AttachmentPart attachmentPart : bodyPart.getAttachments().getAttachments()) {
        fillStack(split(attachmentPart, messageHeaders), responseStack);
      }
    }

    return responseStack.isEmpty() ? null : responseStack.pop();
  }
  public static void readTrainingMail(String fileNameWithPath) {
    InputStream is;

    try {
      is = new FileInputStream(fileNameWithPath);

      Properties props = System.getProperties();
      props.put("mail.host", "stmp.gmail.com");
      props.put("mail.transport.protocol", "stmp");
      Session mailSession = Session.getDefaultInstance(props, null);

      MimeMessage message = new MimeMessage(mailSession, is);

      if (message.getSubject() != null) {
        System.out.println("subject ---> " + message.getSubject());
      }

      if (message.getFrom() != null) {
        System.out.println(message.getFrom()[0].toString());
      }

      /**
       * The Message.RecipientType is divided into TO , the primary recipients -- CC ,carbon copy
       * recipients -- 抄送 BCC, blind carbon copy recipients-- 发送邮件副本,又不想让原始接收人知道,密抄送
       */
      if (message.getRecipients(MimeMessage.RecipientType.CC) != null) {
        System.out.println("CC : found " + message.getRecipients(Message.RecipientType.CC));
      }

      if (message.getRecipients(Message.RecipientType.BCC) != null) {
        System.out.println("BCc : Found " + message.getRecipients(Message.RecipientType.BCC));
      }

      if (message.getRecipients(Message.RecipientType.TO) != null) {
        System.out.println("To Found " + message.getRecipients(Message.RecipientType.TO));
        Address[] AddressList = message.getRecipients(Message.RecipientType.TO);

        for (Address internetAddress : AddressList) {
          InternetAddress addr = (InternetAddress) internetAddress;
          System.out.println(addr.getAddress());
        }
      }

      if (message.getSentDate() != null) {
        System.out.println("message Date : " + message.getSentDate());
      }

      System.out.println("here is the email 's content type name");
      System.out.println(message.getContentType());
      System.out.println("here is the email content ");
      System.out.println(message.getContent());

      if (message.getContentType().startsWith("multipart")) {
        Multipart multiPart = (Multipart) message.getContent();

        for (int x = 0; x < multiPart.getCount(); x++) {
          BodyPart bodyPart = multiPart.getBodyPart(x);
          String disposition = bodyPart.getDisposition();

          if (disposition != null) {
            String content = html2text((String) bodyPart.getContent()).trim();

            System.out.println("Content : " + content);
            System.out.println("disposition " + disposition);
          }

          System.out.println("is email contains disposition ? ");
          if (disposition != null && disposition.equals(BodyPart.ATTACHMENT))
            System.out.println("yes");
          else System.out.println("no");
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public static List<String> getText(BytesWritable value, Boolean tokenizep)
      throws InterruptedException {
    Session s = Session.getDefaultInstance(new Properties());
    InputStream is = new ByteArrayInputStream(value.getBytes());
    List<String> out = new ArrayList<String>();
    try {
      MimeMessage message = new MimeMessage(s, is);
      message.getAllHeaderLines();

      Analyzer standard_analyzer = new StandardAnalyzer(Version.LUCENE_43);
      Analyzer email_analyzer = new UAX29URLEmailAnalyzer(Version.LUCENE_43);

      Address[] fromAddrs = message.getFrom();
      String fromAddrstr = "";
      if (fromAddrs != null) {
        for (Address addr : fromAddrs) {
          fromAddrstr += (addr.toString() + " ");
        }
      }

      Address[] toAddrs = message.getAllRecipients();
      String toAddrstr = "";
      if (toAddrs != null) {
        for (Address addr : toAddrs) {
          toAddrstr += (addr.toString() + " ");
        }
      }

      String subject = message.getSubject();

      String body = "";
      try {
        Object content = message.getContent();
        // System.err.println(content.getContentType());
        if (content instanceof String) {
          body = (String) content;
        } else if (content instanceof Multipart) {
          Multipart mp = (Multipart) content;
          for (int i = 0; i < mp.getCount(); i++) {
            BodyPart bp = mp.getBodyPart(i);
            // System.err.println(bp.getContentType());
            Object c = bp.getContent();
            if (c instanceof String) {
              body = (String) c;
            }
          }
        }
        // people do really evil things with email, we're not sorting through it all now
      } catch (DecodingException e) {
        System.err.println("DecodingException");
      } catch (UnsupportedEncodingException e) {
        System.err.println("UnsuportedEncodingException");
      } catch (IOException e) {
        System.err.println("IOException");
      }

      if (tokenizep) {
        List<String> fromData = new ArrayList<String>();
        List<String> toData = new ArrayList<String>();
        List<String> subjectData = new ArrayList<String>();
        List<String> bodyData = new ArrayList<String>();

        if (fromAddrstr != null) {
          fromData = tokenizeString(email_analyzer, fromAddrstr);
        }
        if (toAddrstr != null) {
          toData = tokenizeString(email_analyzer, toAddrstr);
        }
        if (subject != null) {
          subjectData = tokenizeString(standard_analyzer, subject);
        }
        if (body != null) {
          bodyData = tokenizeString(standard_analyzer, body);
        }

        out.add("FROM ");
        out.addAll(fromData);

        out.add("TO ");
        out.addAll(toData);

        out.add("SUBJECT ");
        out.addAll(subjectData);

        out.add("BODY ");
        out.addAll(bodyData);
      } else {
        // if not tokenizep, return list with from and subject fields only
        out.add(fromAddrstr);
        out.add(subject);
      }

    } catch (MessagingException e) {
      System.err.println("MessagineException");
    }

    return out;
  }