Java Code Examples for com.google.api.services.gmail.model.Profile

The following examples show how to use com.google.api.services.gmail.model.Profile. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: camel-quarkus   Source File: GoogleMailResource.java    License: Apache License 2.0 6 votes vote down vote up
private Message createMessage(String body) throws MessagingException, IOException {
    Session session = Session.getDefaultInstance(new Properties(), null);

    Profile profile = producerTemplate.requestBody("google-mail://users/getProfile?inBody=userId", USER_ID, Profile.class);
    MimeMessage mm = new MimeMessage(session);
    mm.addRecipients(TO, profile.getEmailAddress());
    mm.setSubject(EMAIL_SUBJECT);
    mm.setContent(body, "text/plain");

    Message message = new Message();
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        mm.writeTo(baos);
        message.setRaw(Base64.getUrlEncoder().encodeToString(baos.toByteArray()));
    }
    return message;
}
 
Example 2
Source Project: wildfly-camel   Source File: GoogleMailIntegrationTest.java    License: Apache License 2.0 6 votes vote down vote up
private static Message createMessage(ProducerTemplate template, String subject)
        throws MessagingException, IOException {

    Profile profile = template.requestBody("google-mail://users/getProfile?inBody=userId", CURRENT_USERID,
            Profile.class);
    Session session = Session.getDefaultInstance(new Properties(), null);
    MimeMessage mm = new MimeMessage(session);
    mm.addRecipients(javax.mail.Message.RecipientType.TO, profile.getEmailAddress());
    mm.setSubject(subject);
    mm.setContent("Camel rocks!\n" //
            + DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()) + "\n" //
            + "user: " + System.getProperty("user.name"), "text/plain");

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    mm.writeTo(baos);
    String encodedEmail = Base64.getUrlEncoder().encodeToString(baos.toByteArray());
    Message message = new Message();
    message.setRaw(encodedEmail);
    return message;
}
 
Example 3
Source Project: mail-importer   Source File: Authorizer.java    License: Apache License 2.0 4 votes vote down vote up
public Credential get() {
  try {
    GoogleClientSecrets clientSecrets = loadGoogleClientSecrets(jsonFactory);

    DataStore<StoredCredential> dataStore = getStoredCredentialDataStore();

    // Allow user to authorize via url.
    GoogleAuthorizationCodeFlow flow =
        new GoogleAuthorizationCodeFlow.Builder(
            httpTransport,
            jsonFactory,
            clientSecrets,
            ImmutableList.of(
                GmailScopes.GMAIL_MODIFY,
                GmailScopes.GMAIL_READONLY))
            .setCredentialDataStore(dataStore)
            .setAccessType("offline")
            .setApprovalPrompt("auto")
            .build();

    // First, see if we have a stored credential for the user.
    Credential credential = flow.loadCredential(user.getEmailAddress());

    // If we don't, prompt them to get one.
    if (credential == null) {
      String url = flow.newAuthorizationUrl()
          .setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI)
          .build();
      System.out.println("Please open the following URL in your browser then "
          + "type the authorization code:\n" + url);

      // Read code entered by user.
      System.out.print("Code: ");
      System.out.flush();
      BufferedReader br = new BufferedReader(
          new InputStreamReader(System.in));
      String code = br.readLine();

      // Generate Credential using retrieved code.
      GoogleTokenResponse response = flow.newTokenRequest(code)
          .setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI)
          .execute();

      credential =
          flow.createAndStoreCredential(response, user.getEmailAddress());
    }

    Gmail gmail = new Gmail.Builder(httpTransport, jsonFactory, credential)
        .setApplicationName(GmailServiceModule.APP_NAME)
        .build();

    Profile profile = gmail.users()
        .getProfile(user.getEmailAddress())
        .execute();

    System.out.println(profile.toPrettyString());
    return credential;
  } catch (IOException exception) {
    throw new RuntimeException(exception);
  }
}