Search in sources :

Example 1 with Gmail

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);
        }
    }
}
Also used : Gmail(com.google.api.services.gmail.Gmail)

Example 2 with Gmail

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;
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) ListMessagesResponse(com.google.api.services.gmail.model.ListMessagesResponse) MimeMessage(javax.mail.internet.MimeMessage) Message(com.google.api.services.gmail.model.Message) Gmail(com.google.api.services.gmail.Gmail) MimeMessage(javax.mail.internet.MimeMessage)

Example 3 with Gmail

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);
}
Also used : Message(com.google.api.services.gmail.model.Message) MimeMessage(javax.mail.internet.MimeMessage) Gmail(com.google.api.services.gmail.Gmail) Document(org.jbpm.document.Document) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException)

Aggregations

Gmail (com.google.api.services.gmail.Gmail)3 Message (com.google.api.services.gmail.model.Message)2 MimeMessage (javax.mail.internet.MimeMessage)2 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 ListMessagesResponse (com.google.api.services.gmail.model.ListMessagesResponse)1 IOException (java.io.IOException)1 MessagingException (javax.mail.MessagingException)1 Document (org.jbpm.document.Document)1