use of com.google.api.services.gmail.model.Message in project syndesis-qe by syndesisio.
the class GMailSteps method checkMailExists.
private boolean checkMailExists(String from, String subject, String text) {
try {
List<Message> messages = gmu.getMessagesMatchingQuery("me", "subject:" + subject + " AND from:" + from);
// if messages list is empty, we haven't found anything, that's false
if (messages.size() == 0) {
return false;
}
MimeMessage mime = gmu.getMimeMessage(messages.get(0).getId());
// then we have found our message
if (mime.getSubject().equalsIgnoreCase(subject) && mime.getContent().toString().trim().equalsIgnoreCase(text.trim())) {
return true;
}
} catch (IOException | MessagingException e) {
log.debug("There has been an error while checking for existing mail", e);
}
return false;
}
use of com.google.api.services.gmail.model.Message in project teammates by TEAMMATES.
the class EmailAccount method getRegistrationKeyFromGmail.
/**
* Retrieve registration key sent to Gmail inbox. After retrieving, marks the email as read.
*
* @return registration key (null if cannot be found).
*/
public static String getRegistrationKeyFromGmail(String username, String courseName, String courseId) throws IOException, MessagingException {
Gmail service = new GmailServiceMaker(username).makeGmailService();
ListMessagesResponse listMessagesResponse;
while (true) {
try {
// Get last 5 emails received by the user as there may be other emails received. However, this may fail
// unexpectedly if there are 5 additional emails received excluding the one from TEAMMATES.
listMessagesResponse = service.users().messages().list(username).setMaxResults(5L).setQ("is:UNREAD").execute();
break;
} catch (GoogleJsonResponseException e) {
if (e.getDetails().getCode() == HttpStatusCodes.STATUS_CODE_FORBIDDEN) {
System.out.println(e.getDetails().getMessage());
service = new GmailServiceMaker(username, true).makeGmailService();
} else {
throw e;
}
}
}
final List<Message> messageStubs = listMessagesResponse.getMessages();
if (isEmpty(messageStubs)) {
return null;
}
for (Message messageStub : messageStubs) {
final Message message = service.users().messages().get(username, messageStub.getId()).setFormat("raw").execute();
final MimeMessage email = convertFromMessageToMimeMessage(message);
if (isStudentCourseJoinRegistrationEmail(email, courseName, courseId)) {
final String body = getTextFromEmail(email);
markMessageAsRead(service, username, messageStub);
return getKey(body);
}
}
return null;
}
use of com.google.api.services.gmail.model.Message in project jbpm-work-items by kiegroup.
the class SendMailWorkitemHandler method executeWorkItem.
public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
String paramTo = (String) workItem.getParameter("To");
String paramFrom = (String) workItem.getParameter("From");
String paramSubject = (String) workItem.getParameter("Subject");
String paramBodyText = (String) workItem.getParameter("BodyText");
Document paramAttachment = (Document) workItem.getParameter("Attachment");
try {
Gmail gmailService = auth.getGmailService(appName, clientSecret);
Message outEmailMessage = sendMessage(gmailService, paramTo, paramFrom, paramSubject, paramBodyText, paramAttachment);
} catch (Exception e) {
handleException(e);
}
workItemManager.completeWorkItem(workItem.getId(), null);
}
use of com.google.api.services.gmail.model.Message in project jbpm-work-items by kiegroup.
the class SendMailWorkitemHandler method createMessageWithEmail.
public 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);
return message;
}
Aggregations