use of com.google.api.services.gmail.Gmail in project OsmAnd-tools by osmandapp.
the class ExceptionAnalyzerMain method downloadAttachments.
private static void downloadAttachments() throws IOException {
// Build a new authorized API client service.
Gmail service = getGmailService();
// Print the labels in the user's account.
String user = "me";
ListLabelsResponse listResponse = service.users().labels().list(user).execute();
List<Label> labels = listResponse.getLabels();
if (labels.size() == 0) {
System.out.println("No labels found.");
}
System.out.println("Labels:");
for (Label label : labels) {
if (label.getName().toUpperCase().equals(LABEL.toUpperCase())) {
List<String> trash = new ArrayList<>();
trash.add(label.getId());
List<Message> result = listMessagesWithLabels(service, user, trash);
System.out.println("Messages in " + LABEL + ": " + result.size());
getAttachments(result, user, service);
}
}
}
use of com.google.api.services.gmail.Gmail 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.Gmail 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);
}
Aggregations