Search in sources :

Example 1 with Message

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

the class GmailUsersMessagesIntegrationTest method createTestEmail.

private Message createTestEmail() throws MessagingException, IOException {
    com.google.api.services.gmail.model.Profile profile = requestBody("google-mail://users/getProfile?inBody=userId", CURRENT_USERID);
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    MimeMessage mm = new MimeMessage(session);
    mm.addRecipients(javax.mail.Message.RecipientType.TO, profile.getEmailAddress());
    mm.setSubject("Hello from camel-google-mail");
    mm.setContent("Camel rocks!", "text/plain");
    Message createMessageWithEmail = createMessageWithEmail(mm);
    return createMessageWithEmail;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) Message(com.google.api.services.gmail.model.Message) MimeMessage(javax.mail.internet.MimeMessage) Properties(java.util.Properties) Session(javax.mail.Session)

Example 2 with Message

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

the class GmailUsersMessagesIntegrationTest method testMessages.

@Test
public void testMessages() throws Exception {
    // ==== Send test email ====
    Message testEmail = createTestEmail();
    Map<String, Object> headers = new HashMap<String, Object>();
    // 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);
    com.google.api.services.gmail.model.Message result = requestBodyAndHeaders("direct://SEND", null, headers);
    assertNotNull("send result", result);
    String testEmailId = result.getId();
    // ==== Search for message we just sent ====
    headers = new HashMap<String, Object>();
    headers.put("CamelGoogleMail.q", "subject:\"Hello from camel-google-mail\"");
    // using String message body for single parameter "userId"
    ListMessagesResponse listOfMessages = requestBody("direct://LIST", CURRENT_USERID);
    assertTrue(idInList(testEmailId, listOfMessages));
    // ===== trash it ====
    headers = new HashMap<String, Object>();
    // parameter type is String
    headers.put("CamelGoogleMail.userId", CURRENT_USERID);
    // parameter type is String
    headers.put("CamelGoogleMail.id", testEmailId);
    requestBodyAndHeaders("direct://TRASH", null, headers);
    // ==== Search for message we just trashed ====
    headers = new HashMap<String, Object>();
    headers.put("CamelGoogleMail.q", "subject:\"Hello from camel-google-mail\"");
    // using String message body for single parameter "userId"
    listOfMessages = requestBody("direct://LIST", CURRENT_USERID);
    assertFalse(idInList(testEmailId, listOfMessages));
    // ===== untrash it ====
    headers = new HashMap<String, Object>();
    // parameter type is String
    headers.put("CamelGoogleMail.userId", CURRENT_USERID);
    // parameter type is String
    headers.put("CamelGoogleMail.id", testEmailId);
    requestBodyAndHeaders("direct://UNTRASH", null, headers);
    // ==== Search for message we just trashed ====
    headers = new HashMap<String, Object>();
    headers.put("CamelGoogleMail.q", "subject:\"Hello from camel-google-mail\"");
    // using String message body for single parameter "userId"
    listOfMessages = requestBody("direct://LIST", CURRENT_USERID);
    assertTrue(idInList(testEmailId, listOfMessages));
    // ===== permanently delete it ====
    headers = new HashMap<String, Object>();
    // parameter type is String
    headers.put("CamelGoogleMail.userId", CURRENT_USERID);
    // parameter type is String
    headers.put("CamelGoogleMail.id", testEmailId);
    requestBodyAndHeaders("direct://DELETE", null, headers);
    // ==== Search for message we just deleted ====
    headers = new HashMap<String, Object>();
    headers.put("CamelGoogleMail.q", "subject:\"Hello from camel-google-mail\"");
    // using String message body for single parameter "userId"
    listOfMessages = requestBody("direct://LIST", CURRENT_USERID);
    assertFalse(idInList(testEmailId, listOfMessages));
}
Also used : ListMessagesResponse(com.google.api.services.gmail.model.ListMessagesResponse) MimeMessage(javax.mail.internet.MimeMessage) Message(com.google.api.services.gmail.model.Message) HashMap(java.util.HashMap) Message(com.google.api.services.gmail.model.Message) Test(org.junit.Test)

Example 3 with Message

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

the class GmailUsersMessagesIntegrationTest method createMessageWithEmail.

private Message createMessageWithEmail(MimeMessage email) throws MessagingException, IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    email.writeTo(baos);
    String encodedEmail = Base64.encodeBase64URLSafeString(baos.toByteArray());
    Message message = new Message();
    message.setRaw(encodedEmail);
    return message;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) Message(com.google.api.services.gmail.model.Message) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with Message

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

the class GmailUsersThreadsIntegrationTest method createMessageWithEmail.

private Message createMessageWithEmail(MimeMessage email) throws MessagingException, IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    email.writeTo(baos);
    String encodedEmail = Base64.encodeBase64URLSafeString(baos.toByteArray());
    Message message = new Message();
    message.setRaw(encodedEmail);
    return message;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) Message(com.google.api.services.gmail.model.Message) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with Message

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

the class GoogleMailService method importItem.

@Override
public void importItem(MailModelWrapper model) throws IOException {
    log("importItem, model: %s", model);
    for (MailMessageModel message : model.getMessages()) {
        // TODO: Avoid re-looking up lable on each fetch
        String labelId = getMigratedLabelId();
        Message newMessage = new Message().setRaw(message.getRawString()).setLabelIds(ImmutableList.of(labelId));
        gmail.users().messages().insert(USER, newMessage).execute();
    }
}
Also used : Message(com.google.api.services.gmail.model.Message) MailMessageModel(org.dataportabilityproject.dataModels.mail.MailMessageModel)

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