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()); } } } }
Gmail gmail = new Gmail.Builder(httpTransport, jsonFactory, credential) .setApplicationName(GmailServiceModule.APP_NAME) .build();
/** * Constructor. * * <p> * Use {@link Builder} if you need to specify any of the optional parameters. * </p> * * @param transport HTTP transport, which should normally be: * <ul> * <li>Google App Engine: * {@code com.google.api.client.extensions.appengine.http.UrlFetchTransport}</li> * <li>Android: {@code newCompatibleTransport} from * {@code com.google.api.client.extensions.android.http.AndroidHttp}</li> * <li>Java: {@link com.google.api.client.googleapis.javanet.GoogleNetHttpTransport#newTrustedTransport()} * </li> * </ul> * @param jsonFactory JSON factory, which may be: * <ul> * <li>Jackson: {@code com.google.api.client.json.jackson2.JacksonFactory}</li> * <li>Google GSON: {@code com.google.api.client.json.gson.GsonFactory}</li> * <li>Android Honeycomb or higher: * {@code com.google.api.client.extensions.android.json.AndroidJsonFactory}</li> * </ul> * @param httpRequestInitializer HTTP request initializer or {@code null} for none * @since 1.7 */ public Gmail(com.google.api.client.http.HttpTransport transport, com.google.api.client.json.JsonFactory jsonFactory, com.google.api.client.http.HttpRequestInitializer httpRequestInitializer) { this(new Builder(transport, jsonFactory, httpRequestInitializer)); }
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(); }