@Before public void setup() throws IOException { googleMailExporter = new GoogleMailExporter(googleCredentialFactory, gmail); when(gmail.users()).thenReturn(users); when(users.messages()).thenReturn(messages); when(messages.list(Matchers.anyString())).thenReturn(messageListRequest); when(messageListRequest.setMaxResults(Matchers.anyLong())).thenReturn(messageListRequest); when(messages.get(Matchers.anyString(), Matchers.anyString())).thenReturn(get); when(get.setFormat(Matchers.anyString())).thenReturn(get); verifyZeroInteractions(googleCredentialFactory); }
@Before public void setUp() throws IOException { Label label = new Label(); label.setId(LABEL1); label.setName(LABEL1); labelsListResponse = new ListLabelsResponse().setLabels(Collections.singletonList(label)); jobStore = new LocalJobStore(); Monitor monitor = new Monitor() {}; googleMailImporter = new GoogleMailImporter(googleCredentialFactory, jobStore, gmail, monitor); when(gmail.users()).thenReturn(users); when(users.messages()).thenReturn(messages); when(messages.insert(Matchers.anyString(), Matchers.any(Message.class))).thenReturn(insert); when(users.labels()).thenReturn(labels); when(labels.list(Matchers.anyString())).thenReturn(labelsList); when(labelsList.execute()).thenReturn(labelsListResponse); when(labels.create(Matchers.anyString(), Matchers.any(Label.class))).thenReturn(labelsCreate); when(labelsCreate.execute()).thenReturn(label); verifyZeroInteractions(googleCredentialFactory); }
.setLabelIds(importedLabelIds.build()); try { getOrCreateGmail(authData).users().messages().insert(USER, newMessage).execute(); } catch (IOException e) { return new ImportResult(e);
.messages() .modify(user.getEmailAddress(), message.getId(), new ModifyMessageRequest()
.collect(Collectors.joining(", ")), gmailMessage.getId()); gmailApi.users().messages().modify( user.getEmailAddress(), gmailMessage.getId(),
LOGGER.info("Running query: "+query); ListMessagesResponse response = gmailService.users() .messages() .list("me") .setQ(query) String singleFilename = ""; Message fetched = gmailService.users().messages().get("me", message.getId()) .execute(); LOGGER.fine("==Fetched message "+fetched.getId()); if(!destination.exists()){ byte[] data = entry.getValue().getBody().getAttachmentId() != null ? gmailService.users().messages().attachments().get("me", message.getId(), entry.getValue().getBody().getAttachmentId()).execute().decodeData() : entry.getValue().getBody().decodeData(); Puzzle puzzle = singleFilename.endsWith("jpz") ?
try { Gmail.Users.Messages.GmailImport r = gmail.users() .messages() .gmailImport(user.getEmailAddress(), new Message(),
gmail.users().messages().list(user.getEmailAddress()) .setQ("rfc822msgid:" + localMessage.getMessageId()) .setFields("messages(id)")
gmail.users().messages().get(user.getEmailAddress(), message.getId()) .setFields("id,labelIds") .queue(batch, new JsonBatchCallback<Message>() {
public Message sendMessage(Gmail service, String to, String from, String subject, String bodyText, Document attachment) throws MessagingException, IOException { MimeMessage mimeMessage = createEmailWithAttachment(to, from, subject, bodyText, attachment); Message message = service.users().messages().send(from, createMessageWithEmail(mimeMessage)).execute(); return message; }
@Before public void setUp() { try { when(auth.getGmailService(anyString(), anyString())).thenReturn(gmailService); when(gmailService.users()).thenReturn(gmailUsers); when(gmailUsers.messages()).thenReturn(gmailUserMessages); when(gmailUserMessages.send(anyString(), anyObject())).thenReturn(gmailUserMessagesSend); when(gmailUserMessagesSend.execute()).thenReturn(new Message()); } catch (Exception e) { fail(e.getMessage()); } }
/** * Retrieves the registration key among the unread emails * with {@code courseId} and {@code courseName} sent to the Gmail inbox. * * <p>After retrieving, marks the email as read. * * <p>If multiple emails of the same course are in the inbox, return the registration key presented in one of them. * * @return registration key (null if cannot be found). */ public String getRegistrationKeyFromUnreadEmails(String courseName, String courseId) throws IOException, MessagingException { List<Message> messageStubs = getListOfUnreadEmailOfUser(); for (Message messageStub : messageStubs) { Message message = service.users().messages().get(username, messageStub.getId()).setFormat("raw") .execute(); MimeMessage email = convertFromMessageToMimeMessage(message); if (isStudentCourseJoinRegistrationEmail(email, courseName, courseId)) { String body = getTextFromEmail(email); markMessageAsRead(messageStub); return getKey(body); } } return null; }
public static boolean sendMessage(String email, String subject, String text) { if (email.isEmpty()) { logger.info("Email is not sent because the address is empty"); return false; } try { Gmail gmail = new Builder(httpTransport, JSON_FACTORY, credential).setApplicationName("XMage Server").build(); MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties())); mimeMessage.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(email)); mimeMessage.setSubject(subject); mimeMessage.setText(text); ByteArrayOutputStream baos = new ByteArrayOutputStream(); mimeMessage.writeTo(baos); Message message = new Message(); message.setRaw(Base64.encodeBase64URLSafeString(baos.toByteArray())); gmail.users().messages().send(ConfigSettings.instance.getGoogleAccount() + (ConfigSettings.instance.getGoogleAccount().endsWith("@gmail.com") ? "" : "@gmail.com"), message).execute(); return true; } catch (MessagingException | IOException ex) { logger.error("Error sending message", ex); } return false; } }
/** * Returns an empty list if there is no unread email of the user. */ private List<Message> getListOfUnreadEmailOfUser() throws IOException { List<Message> messageStubs = service.users().messages().list(username).setQ("is:UNREAD").execute().getMessages(); return messageStubs == null ? new ArrayList<>() : messageStubs; }
private void markMessageAsRead(Message messageStub) throws IOException { ModifyMessageRequest modifyMessageRequest = new ModifyMessageRequest() .setRemoveLabelIds(Collections.singletonList("UNREAD")); service.users().messages().modify(username, messageStub.getId(), modifyMessageRequest).execute(); }