Search in sources :

Example 16 with Message

use of com.google.api.services.gmail.model.Message in project camel by apache.

the class GmailUsersThreadsIntegrationTest method testList.

@Test
public void testList() throws Exception {
    Message m1 = createThreadedTestEmail(null);
    Message m2 = createThreadedTestEmail(m1.getThreadId());
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put("CamelGoogleMail.q", "subject:\"Hello from camel-google-mail\"");
    // using String message body for single parameter "userId"
    com.google.api.services.gmail.model.ListThreadsResponse result = requestBodyAndHeaders("direct://LIST", CURRENT_USERID, headers);
    assertNotNull("list result", result);
    assertTrue(result.getThreads().size() > 0);
    LOG.debug("list: " + result);
    headers = new HashMap<String, Object>();
    // parameter type is String
    headers.put("CamelGoogleMail.userId", CURRENT_USERID);
    // parameter type is String
    headers.put("CamelGoogleMail.id", m1.getThreadId());
    requestBodyAndHeaders("direct://DELETE", null, headers);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) Message(com.google.api.services.gmail.model.Message) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 17 with Message

use of com.google.api.services.gmail.model.Message in project wildfly-camel by wildfly-extras.

the class GoogleMailIntegrationTest method createThreadedMessage.

private static Message createThreadedMessage(String previousThreadId, String subject, ProducerTemplate template) throws MessagingException, IOException {
    Message message = createMessage(template, subject);
    if (previousThreadId != null) {
        message.setThreadId(previousThreadId);
    }
    Map<String, Object> headers = new HashMap<>();
    // parameter type is String
    headers.put("CamelGoogleMail.userId", CURRENT_USERID);
    // parameter type is com.google.api.services.gmail.model.Message
    headers.put("CamelGoogleMail.content", message);
    return template.requestBodyAndHeaders("google-mail://messages/send", null, headers, Message.class);
}
Also used : Message(com.google.api.services.gmail.model.Message) MimeMessage(javax.mail.internet.MimeMessage) HashMap(java.util.HashMap)

Example 18 with Message

use of com.google.api.services.gmail.model.Message in project wildfly-camel by wildfly-extras.

the class GoogleMailIntegrationTest method createMessage.

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;
}
Also used : Message(com.google.api.services.gmail.model.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) Profile(com.google.api.services.gmail.model.Profile) Session(javax.mail.Session)

Example 19 with Message

use of com.google.api.services.gmail.model.Message in project wildfly-camel by wildfly-extras.

the class GoogleMailIntegrationTest method messages.

@Test
public void messages() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    GoogleMailComponent gMailComponent = camelctx.getComponent("google-mail", GoogleMailComponent.class);
    GoogleApiEnv.configure(gMailComponent.getConfiguration(), getClass(), LOG);
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            final String pathPrefix = "messages";
            // test route for attachments
            from("direct://ATTACHMENTS").to("google-mail://" + pathPrefix + "/attachments");
            // test route for delete
            from("direct://DELETE").to("google-mail://" + pathPrefix + "/delete");
            // test route for get
            from("direct://GET").to("google-mail://" + pathPrefix + "/get");
            // test route for gmailImport
            from("direct://GMAILIMPORT").to("google-mail://" + pathPrefix + "/gmailImport");
            // test route for gmailImport
            from("direct://GMAILIMPORT_1").to("google-mail://" + pathPrefix + "/gmailImport");
            // test route for insert
            from("direct://INSERT").to("google-mail://" + pathPrefix + "/insert");
            // test route for insert
            from("direct://INSERT_1").to("google-mail://" + pathPrefix + "/insert");
            // test route for list
            from("direct://LIST").to("google-mail://" + pathPrefix + "/list?inBody=userId");
            // test route for modify
            from("direct://MODIFY").to("google-mail://" + pathPrefix + "/modify");
            // test route for send
            from("direct://SEND").to("google-mail://" + pathPrefix + "/send");
            // test route for send
            from("direct://SEND_1").to("google-mail://" + pathPrefix + "/send");
            // test route for trash
            from("direct://TRASH").to("google-mail://" + pathPrefix + "/trash");
            // test route for untrash
            from("direct://UNTRASH").to("google-mail://" + pathPrefix + "/untrash");
        }
    });
    try {
        camelctx.start();
        ProducerTemplate template = camelctx.createProducerTemplate();
        // ==== Send test email ====
        final String subject = getClass().getSimpleName() + ".messages " + UUID.randomUUID().toString();
        Message testEmail = createMessage(template, subject);
        Map<String, Object> headers = new HashMap<>();
        // parameter type is String
        headers.put("CamelGoogleMail.userId", CURRENT_USERID);
        // parameter type is com.google.api.services.gmail.model.Message
        headers.put("CamelGoogleMail.content", testEmail);
        Message result = template.requestBodyAndHeaders("direct://SEND", null, headers, Message.class);
        Assert.assertNotNull("send result", result);
        String testEmailId = result.getId();
        // ==== Search for message we just sent ====
        headers = new HashMap<>();
        headers.put("CamelGoogleMail.q", "subject:\"" + subject + "\"");
        // using String message body for single parameter "userId"
        ListMessagesResponse listOfMessages = template.requestBody("direct://LIST", CURRENT_USERID, ListMessagesResponse.class);
        Assert.assertTrue(idInList(testEmailId, listOfMessages));
        // ===== trash it ====
        headers = new HashMap<>();
        // parameter type is String
        headers.put("CamelGoogleMail.userId", CURRENT_USERID);
        // parameter type is String
        headers.put("CamelGoogleMail.id", testEmailId);
        template.requestBodyAndHeaders("direct://TRASH", null, headers);
        // ==== Search for message we just trashed ====
        headers = new HashMap<>();
        headers.put("CamelGoogleMail.q", "subject:\"" + subject + "\"");
        // using String message body for single parameter "userId"
        listOfMessages = template.requestBody("direct://LIST", CURRENT_USERID, ListMessagesResponse.class);
        Assert.assertFalse(idInList(testEmailId, listOfMessages));
        // ===== untrash it ====
        headers = new HashMap<>();
        // parameter type is String
        headers.put("CamelGoogleMail.userId", CURRENT_USERID);
        // parameter type is String
        headers.put("CamelGoogleMail.id", testEmailId);
        template.requestBodyAndHeaders("direct://UNTRASH", null, headers);
        // ==== Search for message we just untrashed ====
        headers = new HashMap<>();
        headers.put("CamelGoogleMail.q", "subject:\"" + subject + "\"");
        // using String message body for single parameter "userId"
        listOfMessages = template.requestBody("direct://LIST", CURRENT_USERID, ListMessagesResponse.class);
        Assert.assertTrue(idInList(testEmailId, listOfMessages));
        // ===== permanently delete it ====
        headers = new HashMap<>();
        // parameter type is String
        headers.put("CamelGoogleMail.userId", CURRENT_USERID);
        // parameter type is String
        headers.put("CamelGoogleMail.id", testEmailId);
        template.requestBodyAndHeaders("direct://DELETE", null, headers);
        // ==== Search for message we just deleted ====
        headers = new HashMap<>();
        headers.put("CamelGoogleMail.q", "subject:\"" + subject + "\"");
        // using String message body for single parameter "userId"
        listOfMessages = template.requestBody("direct://LIST", CURRENT_USERID, ListMessagesResponse.class);
        Assert.assertFalse(idInList(testEmailId, listOfMessages));
    } finally {
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) GoogleMailComponent(org.apache.camel.component.google.mail.GoogleMailComponent) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) Message(com.google.api.services.gmail.model.Message) MimeMessage(javax.mail.internet.MimeMessage) HashMap(java.util.HashMap) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) ListMessagesResponse(com.google.api.services.gmail.model.ListMessagesResponse) Test(org.junit.Test)

Example 20 with Message

use of com.google.api.services.gmail.model.Message in project syndesis-qe by syndesisio.

the class GMailUtils method modifyMessage.

/**
 * Modify the labels a message is associated with.
 *
 * @param userId User's email address. The special value "me"
 * can be used to indicate the authenticated user.
 * @param messageId ID of Message to Modify.
 * @param labelsToAdd List of label ids to add.
 * @param labelsToRemove List of label ids to remove.
 * @throws IOException when something goes wrong
 */
public void modifyMessage(String userId, String messageId, List<String> labelsToAdd, List<String> labelsToRemove) throws IOException {
    ModifyMessageRequest mods = new ModifyMessageRequest().setAddLabelIds(labelsToAdd).setRemoveLabelIds(labelsToRemove);
    Message message = getClient().users().messages().modify(userId, messageId, mods).execute();
    log.info("Message with id: " + message.getId() + "was modified");
    log.debug(message.toPrettyString());
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) Message(com.google.api.services.gmail.model.Message) ModifyMessageRequest(com.google.api.services.gmail.model.ModifyMessageRequest)

Aggregations

Message (com.google.api.services.gmail.model.Message)24 MimeMessage (javax.mail.internet.MimeMessage)21 ListMessagesResponse (com.google.api.services.gmail.model.ListMessagesResponse)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 MessagingException (javax.mail.MessagingException)4 Test (org.junit.Test)4 Properties (java.util.Properties)3 Session (javax.mail.Session)3 Gmail (com.google.api.services.gmail.Gmail)2 ArrayList (java.util.ArrayList)2 CamelContext (org.apache.camel.CamelContext)2 ProducerTemplate (org.apache.camel.ProducerTemplate)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 GoogleMailComponent (org.apache.camel.component.google.mail.GoogleMailComponent)2 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)2 MailMessageModel (org.dataportabilityproject.dataModels.mail.MailMessageModel)2 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 Messages (com.google.api.services.gmail.Gmail.Users.Messages)1