Пример #1
0
  public Map getBareGmailMessageDetails(String messageId) {
    Map<String, Object> messageDetails = new HashMap<String, Object>();
    try {
      Gmail service =
          new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
              .setApplicationName("Gmail Quickstart")
              .build();
      Message message =
          service
              .users()
              .messages()
              .get("me", messageId)
              .setFormat("full")
              .setFields("id,payload,sizeEstimate,snippet,threadId")
              .execute();
      List<MessagePartHeader> headers = message.getPayload().getHeaders();
      for (MessagePartHeader header : headers) {
        if (header.getName().equals("From")
            || header.getName().equals("Date")
            || header.getName().equals("Subject")
            || header.getName().equals("To")
            || header.getName().equals("CC")) {
          messageDetails.put(header.getName().toLowerCase(), header.getValue());
        }
      }
      messageDetails.put("snippet", message.getSnippet());
      messageDetails.put("threadId", message.getThreadId());
      messageDetails.put("id", message.getId());
      // messageDetails.put("body",message.getPayload().getBody().getData());

    } catch (IOException ex) {
      Logger.getLogger(GoogleAuthHelper.class.getName()).log(Level.SEVERE, null, ex);
    }
    return messageDetails;
  }
Пример #2
0
  public Map getBareMessageDetails(String messageId) {
    Map<String, Object> messageDetails = new HashMap<String, Object>();
    try {
      Gmail service =
          new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
              .setApplicationName("Gmail Quickstart")
              .build();
      Message message = service.users().messages().get("me", messageId).setFormat("raw").execute();

      byte[] emailBytes = Base64.decodeBase64(message.getRaw());

      Properties props = new Properties();
      Session session = Session.getDefaultInstance(props, null);

      MimeMessage email = new MimeMessage(session, new ByteArrayInputStream(emailBytes));
      messageDetails.put("subject", email.getSubject());
      messageDetails.put("from", email.getSender() != null ? email.getSender().toString() : "None");
      messageDetails.put(
          "time", email.getSentDate() != null ? email.getSentDate().toString() : "None");
      messageDetails.put("snippet", message.getSnippet());
      messageDetails.put("threadId", message.getThreadId());
      messageDetails.put("id", message.getId());
      messageDetails.put("body", getText(email));

    } catch (MessagingException ex) {
      Logger.getLogger(GoogleAuthHelper.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
      Logger.getLogger(GoogleAuthHelper.class.getName()).log(Level.SEVERE, null, ex);
    }
    return messageDetails;
  }