Trying to send email using the Gmail API into App Engine. Previously I 
created a Service account key into Credentials page, it generates a .P12 
file which is in setServiceAccountPrivateKeyFromP12File parameter. It has a 
ID key joined to the account [email protected] into 
Service account page. The code:

/* Application name. */
 private static final String APPLICATION_NAME = "appnamefromappengine";

 String emailAddress = "[email protected]";
 JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
 try {
 HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();

 Set<String> scopes = new HashSet<String>();
 scopes.add(GmailScopes.GMAIL_SEND);
 scopes.add(GmailScopes.GMAIL_COMPOSE);
 scopes.add(GmailScopes.MAIL_GOOGLE_COM);
 scopes.add("https://www.googleapis.com/auth/admin.directory.user";);

 GoogleCredential credential = new GoogleCredential.Builder()
 .setTransport(httpTransport)
 .setJsonFactory(JSON_FACTORY)
 .setServiceAccountId(emailAddress)
 .setServiceAccountPrivateKeyFromP12File(new File(
"/home/myuser/Test/src/main/webapp/resources/**somename**cd30e7118ad5.p12"))
 .setServiceAccountScopes(scopes)
 .setServiceAccountUser("[email protected]")
 .build();
 Gmail gmail = new Gmail
 .Builder(httpTransport, JSON_FACTORY, credential)
 .setApplicationName(APPLICATION_NAME)
 .build();

 Properties props = new Properties();
 Session session = Session.getDefaultInstance(props, null);
 MimeMessage message = new MimeMessage(session);
 message.setFrom(new InternetAddress("[email protected]"));
 message.addRecipient(javax.mail.Message.RecipientType.TO, new 
InternetAddress("[email protected]"));
 message.setSubject("Test Mail");
 message.setText("Test Mail");
 sendMessage(gmail, "[email protected]", message);

 Message message = createMessageWithEmail(email); //createMessageWithEmail 
function from Gmail API
 message = service.users().messages().send(userId, message).execute();

 } catch (GeneralSecurityException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (MessagingException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }


It returns me this error:


com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad 
Request { "code" : 400,
 "errors" : [ { "domain" : "global", "message" : "Bad Request", "reason" : 
"failedPrecondition" } ],
 "message" : "Bad Request" }




 

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-appengine/f6905e5a-0dca-4564-b85c-d79b8178841f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to