예제 #1
0
  /**
   * Send an email from the user's mailbox to its recipient.
   *
   * @param service Authorized Gmail API instance.
   * @param userId User's email address. The special value "me" can be used to indicate the
   *     authenticated user.
   * @param email Email to be sent.
   * @throws MessagingException
   * @throws IOException
   */
  public String sendMessage(String to, String from, String subject, String body)
      throws MessagingException, IOException {

    Gmail service =
        new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
            .setApplicationName("Gmail Quickstart")
            .build();
    MimeMessage email = createMimeMessage(to, from, subject, body);
    Message message = createMessageWithEmail(email);
    message = service.users().messages().send("me", message).execute();

    System.out.println("Message id: " + message.getId());
    System.out.println(message.toPrettyString());
    if (message.getId() != null) {
      return "success";
    } else {
      return "fail";
    }
  }
예제 #2
0
  @Override
  public void crawl(Map<String, Object> confidg) {

    try {
      Gmail service = GmailOAuth2Service.getGmailService();
      final String userId = "me";

      ListMessagesResponse response = service.users().messages().list(userId).execute();

      List<Message> messages = new ArrayList<>();

      while (response.getMessages() != null) {
        messages.addAll(response.getMessages());

        if (response.getNextPageToken() != null) {
          String pageToken = response.getNextPageToken();
          response = service.users().messages().list(userId).setPageToken(pageToken).execute();
        } else {
          break;
        }
      }

      for (Message message : messages) {

        try {
          final Message msg = service.users().messages().get(userId, message.getId()).execute();

          System.out.println(msg.toPrettyString());

        } catch (GoogleJsonResponseException ex) {
          System.out.println("Erro na thread id: " + message.getId());
          System.out.println("Erro: " + ex.getMessage());
        }
      }

    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }