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; } }
public static void main(String... args) throws IOException, GeneralSecurityException { // Build a new authorized API client service. final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)) .setApplicationName(APPLICATION_NAME) .build(); // 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.isEmpty()) { System.out.println("No labels found."); } else { System.out.println("Labels:"); for (Label label : labels) { System.out.printf("- %s\n", label.getName()); } } } }
.setApplicationName(GmailServiceModule.APP_NAME) .build();
public void updateCredential(SharedPreferences prefs){ credential = GoogleAccountCredential.usingOAuth2( getApplicationContext(), Arrays.asList(GMConstants.SCOPES)) .setBackOff(new ExponentialBackOff()) .setSelectedAccountName(prefs.getString(GMConstants.PREF_ACCOUNT_NAME, null)); if(credential != null && credential.getSelectedAccount() != null) { gmailService = new com.google.api.services.gmail.Gmail.Builder( transport, jsonFactory, credential) .setApplicationName("Shortyz") .build(); } else { gmailService = null; } }
public Gmail getGmailService(String appName, String clientSecretJSON) { try { HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Credential credential = authorize(clientSecretJSON); return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) .setApplicationName(appName) .build(); } catch (Exception e) { throw new IllegalArgumentException(e); } }
Gmail getServiceWithRetries() { HttpRequestInitializer httpRequestInitializer = request -> { credential.initialize(request); new UnsuccessfulResponseHandlerChainer().chain( request.getUnsuccessfulResponseHandler(), new HttpBackOffUnsuccessfulResponseHandler( backOffProvider.get())); }; return new Gmail.Builder(httpTransport, jsonFactory, httpRequestInitializer) .setApplicationName(GmailServiceModule.APP_NAME) .build(); }
/** * Builds and returns an authorized Gmail client service. */ Gmail makeGmailService() throws IOException { Credential credential = authorizeAndCreateCredentials(); return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) .setApplicationName("teammates") .build(); }
private synchronized Gmail makeGmailService(TokensAndUrlAuthData authData) { Credential credential = credentialFactory.createCredential(authData); return new Gmail.Builder( credentialFactory.getHttpTransport(), credentialFactory.getJsonFactory(), credential) .setApplicationName(GoogleStaticObjects.APP_NAME) .build(); } }
private synchronized Gmail makeGmailService(TokensAndUrlAuthData authData) { Credential credential = credentialFactory.createCredential(authData); return new Gmail.Builder( credentialFactory.getHttpTransport(), credentialFactory.getJsonFactory(), credential) .setApplicationName(GoogleStaticObjects.APP_NAME) .build(); }