Search in sources :

Example 11 with Message

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

the class GMailUtils method createMessageWithEmail.

/**
 * Create a message from an email.
 *
 * @param emailContent Email to be set to raw of message
 * @return a message containing a base64url encoded email
 * @throws IOException when something goes wrong
 * @throws MessagingException when something goes wrong
 */
private static Message createMessageWithEmail(MimeMessage emailContent) throws MessagingException, IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    emailContent.writeTo(buffer);
    byte[] bytes = buffer.toByteArray();
    String encodedEmail = Base64.encodeBase64URLSafeString(bytes);
    Message message = new Message();
    message.setRaw(encodedEmail);
    log.info("Email encoded into base64 message");
    return message;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) Message(com.google.api.services.gmail.model.Message) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 12 with Message

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

the class GMailUtils method getMessagesMatchingQuery.

/**
 * List all Messages of the user's mailbox matching the query.
 * Query options can be found here: https://support.google.com/mail/answer/7190
 *
 * @param userId User's email address. The special value "me"
 * can be used to indicate the authenticated user.
 * @param query String used to filter the Messages listed.
 * @throws IOException when something goes wrong
 */
public List<Message> getMessagesMatchingQuery(String userId, String query) throws IOException {
    ListMessagesResponse response = getClient().users().messages().list(userId).setQ(query).execute();
    List<Message> messages = new ArrayList<>();
    while (response.getMessages() != null) {
        log.info("Processing message...");
        messages.addAll(response.getMessages());
        if (response.getNextPageToken() != null) {
            String pageToken = response.getNextPageToken();
            response = getClient().users().messages().list(userId).setQ(query).setPageToken(pageToken).execute();
        } else {
            break;
        }
    }
    return messages;
}
Also used : ListMessagesResponse(com.google.api.services.gmail.model.ListMessagesResponse) MimeMessage(javax.mail.internet.MimeMessage) Message(com.google.api.services.gmail.model.Message) ArrayList(java.util.ArrayList)

Example 13 with Message

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

the class GMailUtils method deleteMessages.

public void deleteMessages(String from, String subject) {
    try {
        List<Message> messages = getMessagesMatchingQuery("me", "subject:" + subject + " AND from:" + from);
        if (messages.size() == 0) {
            log.info("No messages found.");
            return;
        }
        log.info("Found # of messages: " + messages.size());
        for (Message m : messages) {
            trashMessage(m.getId());
        }
    } catch (IOException e) {
        fail("Exception was thrown while deleting messages.", e);
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) Message(com.google.api.services.gmail.model.Message) IOException(java.io.IOException)

Example 14 with Message

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

the class GmailUsersMessagesIntegrationTest method idInList.

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

Example 15 with Message

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

the class GmailUsersThreadsIntegrationTest method createThreadedTestEmail.

private Message createThreadedTestEmail(String previousThreadId) 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);
    if (previousThreadId != null) {
        createMessageWithEmail.setThreadId(previousThreadId);
    }
    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", createMessageWithEmail);
    return requestBodyAndHeaders("google-mail://messages/send", null, headers);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) Message(com.google.api.services.gmail.model.Message) MimeMessage(javax.mail.internet.MimeMessage) HashMap(java.util.HashMap) Properties(java.util.Properties) Session(javax.mail.Session)

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