Search in sources :

Example 21 with Message

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

Example 22 with Message

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;
}
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 23 with Message

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

Example 24 with Message

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;
}
Also used : Message(com.google.api.services.gmail.model.Message) MimeMessage(javax.mail.internet.MimeMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

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