Search in sources :

Example 6 with Message

use of com.google.api.services.gmail.model.Message in project data-transfer-project by google.

the class GoogleMailService method export.

// This currently exports each message with associated labels
@Override
public MailModelWrapper export(ExportInformation exportInformation) throws IOException {
    Messages.List request = gmail.users().messages().list(USER).setMaxResults(MAX_RESULTS_PER_REQUEST);
    if (exportInformation.getPaginationInformation().isPresent()) {
        request.setPageToken(((StringPaginationToken) exportInformation.getPaginationInformation().get()).getId());
    }
    ListMessagesResponse response = request.execute();
    List<MailMessageModel> results = new ArrayList<>(response.getMessages().size());
    // as we can't store all the mail messagess in memory at once.
    for (Message listMessage : response.getMessages()) {
        Message getResponse = gmail.users().messages().get(USER, listMessage.getId()).setFormat("raw").execute();
        // TODO: note this doesn't transfer things like labels
        results.add(new MailMessageModel(getResponse.getRaw(), getResponse.getLabelIds()));
    }
    PaginationInformation pageInfo = null;
    if (response.getNextPageToken() != null) {
        pageInfo = new StringPaginationToken(response.getNextPageToken());
    }
    // TODO: export by label or by message?
    return new MailModelWrapper(null, results, new ContinuationInformation(null, pageInfo));
}
Also used : ListMessagesResponse(com.google.api.services.gmail.model.ListMessagesResponse) ContinuationInformation(org.dataportabilityproject.dataModels.ContinuationInformation) Messages(com.google.api.services.gmail.Gmail.Users.Messages) Message(com.google.api.services.gmail.model.Message) ArrayList(java.util.ArrayList) MailModelWrapper(org.dataportabilityproject.dataModels.mail.MailModelWrapper) MailMessageModel(org.dataportabilityproject.dataModels.mail.MailMessageModel) PaginationInformation(org.dataportabilityproject.dataModels.PaginationInformation) StringPaginationToken(org.dataportabilityproject.shared.StringPaginationToken)

Example 7 with Message

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

the class GoogleMailIntegrationTest method threads.

@SuppressWarnings("serial")
@Test
public void threads() 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 = "threads";
            // 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 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 trash
            from("direct://TRASH").to("google-mail://" + pathPrefix + "/trash");
            // test route for untrash
            from("direct://UNTRASH").to("google-mail://" + pathPrefix + "/untrash");
        }
    });
    try {
        camelctx.start();
        final String subject = getClass().getSimpleName() + ".threads " + UUID.randomUUID().toString();
        ProducerTemplate template = camelctx.createProducerTemplate();
        Message m1 = createThreadedMessage(null, subject, template);
        final String threadId = m1.getThreadId();
        createThreadedMessage(threadId, subject, template);
        // using String message body for single parameter "userId"
        ListThreadsResponse result = template.requestBodyAndHeaders("direct://LIST", CURRENT_USERID, Collections.singletonMap("CamelGoogleMail.q", "subject:\"" + subject + "\""), ListThreadsResponse.class);
        Assert.assertNotNull("list result", result);
        Assert.assertTrue(result.getThreads().size() > 0);
        // ===== trash it ====
        template.requestBodyAndHeaders("direct://TRASH", null, new HashMap<String, Object>() {

            {
                put("CamelGoogleMail.userId", CURRENT_USERID);
                put("CamelGoogleMail.id", threadId);
            }
        });
        // ==== Search for message we just trashed ====
        result = template.requestBodyAndHeaders("direct://LIST", CURRENT_USERID, Collections.singletonMap("CamelGoogleMail.q", "subject:\"" + subject + "\""), ListThreadsResponse.class);
        Assert.assertNotNull("list result", result);
        Assert.assertTrue(result.getThreads() == null || result.getThreads().stream().noneMatch(t -> threadId.equals(t.getId())));
        /* For some reason the thread deletion often needs some delay to succeed */
        int attemptCount = 0;
        for (; ; ) {
            try {
                template.requestBodyAndHeaders("direct://DELETE", null, new HashMap<String, Object>() {

                    {
                        put("CamelGoogleMail.userId", CURRENT_USERID);
                        put("CamelGoogleMail.id", threadId);
                    }
                });
                break;
            /* success */
            } catch (Exception e) {
                if (attemptCount >= 5) {
                    throw e;
                /* too many attempts */
                } else {
                    /* retry */
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e1) {
                        Thread.currentThread().interrupt();
                    }
                }
                attemptCount++;
            }
        }
    } 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) ListThreadsResponse(com.google.api.services.gmail.model.ListThreadsResponse) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) Test(org.junit.Test)

Example 8 with Message

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

the class GoogleMailIntegrationTest method idInList.

private static boolean idInList(String testEmailId, ListMessagesResponse listOfMessages) {
    Assert.assertNotNull("list result", listOfMessages);
    List<Message> messages = listOfMessages.getMessages();
    if (messages != null) {
        for (Message m : listOfMessages.getMessages()) {
            if (testEmailId.equals(m.getId())) {
                return true;
            }
        }
    }
    return false;
}
Also used : Message(com.google.api.services.gmail.model.Message) MimeMessage(javax.mail.internet.MimeMessage)

Example 9 with Message

use of com.google.api.services.gmail.model.Message in project jbpm-work-items by kiegroup.

the class SendMailWorkitemHandler method sendMessage.

public Message sendMessage(Gmail service, String to, String from, String subject, String bodyText, Document attachment) throws MessagingException, IOException {
    MimeMessage mimeMessage = createEmailWithAttachment(to, from, subject, bodyText, attachment);
    Message message = service.users().messages().send(from, createMessageWithEmail(mimeMessage)).execute();
    return message;
}
Also used : Message(com.google.api.services.gmail.model.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage)

Example 10 with Message

use of com.google.api.services.gmail.model.Message in project jbpm-work-items by kiegroup.

the class GoogleMailWorkitemHandlerTest method setUp.

@Before
public void setUp() {
    try {
        when(auth.getGmailService(anyString(), anyString())).thenReturn(gmailService);
        when(gmailService.users()).thenReturn(gmailUsers);
        when(gmailUsers.messages()).thenReturn(gmailUserMessages);
        when(gmailUserMessages.send(anyString(), anyObject())).thenReturn(gmailUserMessagesSend);
        when(gmailUserMessagesSend.execute()).thenReturn(new Message());
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : Message(com.google.api.services.gmail.model.Message) Before(org.junit.Before)

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