Java Code Examples for

The following examples show how to use 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:    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.setContent(body, "text/plain");

    Message message = new Message();
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
    return message;
Example 2
Source Project: wildfly-camel   Source File:    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,
    Session session = Session.getDefaultInstance(new Properties(), null);
    MimeMessage mm = new MimeMessage(session);
    mm.addRecipients(javax.mail.Message.RecipientType.TO, profile.getEmailAddress());
    mm.setContent("Camel rocks!\n" //
            + DateTimeFormatter.ISO_OFFSET_DATE_TIME.format( + "\n" //
            + "user: " + System.getProperty(""), "text/plain");

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    String encodedEmail = Base64.getUrlEncoder().encodeToString(baos.toByteArray());
    Message message = new Message();
    return message;
Example 3
Source Project: mail-importer   Source File:    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(

    // 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()
      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: ");
      BufferedReader br = new BufferedReader(
          new InputStreamReader(;
      String code = br.readLine();

      // Generate Credential using retrieved code.
      GoogleTokenResponse response = flow.newTokenRequest(code)

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

    Gmail gmail = new Gmail.Builder(httpTransport, jsonFactory, credential)

    Profile profile = gmail.users()

    return credential;
  } catch (IOException exception) {
    throw new RuntimeException(exception);