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;
}
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;
}
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);
}
}
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;
}
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);
}
Aggregations