Great, I'll follow that thread and provide advice if possible. The other user has linked another question which mentions what I was saying about "domain-wide delegation" for your service account. You'll need to do that in order to send mail from an address on a domain.
On Friday, June 24, 2016 at 5:06:09 PM UTC-4, Angel Sanchez wrote: > > Thanks. > > El viernes, 24 de junio de 2016, 15:34:05 (UTC-5), Nick (Cloud Platform > Support) escribió: >> >> Link to doc on domain-wide delegation >> <https://developers.google.com/identity/protocols/OAuth2ServiceAccount>. >> >> On Friday, June 24, 2016 at 4:10:32 PM UTC-4, Nick (Cloud Platform >> Support) wrote: >>> >>> Hey Angel, >>> >>> This forum is meant for high level discussion of the platform and >>> services rather than specific-issue technical support. A question like this >>> should be posted to Stack Overflow <http://stackoverflow.com>, where we >>> are also very active. You are likely to be in touch with a much larger >>> number of potentially-helpful users there. My advice from looking at the >>> code, to help either resolve the issue or improve your post to Stack >>> Overflow: >>> >>> 1. service is used but doesn't seem to be defined. Where is it defined? >>> Shouldn't you call the method on: >>> >>> Gmail gmail = new Gmail >>> .Builder(httpTransport, JSON_FACTORY, credential) >>> .setApplicationName(APPLICATION_NAME) >>> .build(); >>> >>> 2. Does your service account have domain-wide-delegation for the address >>> you're attempting to sending the mail from? "reason" : >>> "failedPrecondition" can sometimes mean this. >>> >>> Let me know if you have any questions, otherwise feel free to post the >>> link to your Stack question here once you've made it. I'll be happy to take >>> a look. This forum, as mentioned, can be used for more high level >>> architecture question and general advice on accomplishing specific >>> use-cases on the platform. >>> >>> Cheers, >>> >>> Nick >>> Cloud Platform Community Support >>> >>> On Friday, June 24, 2016 at 12:18:03 PM UTC-4, Angel Sanchez wrote: >>>> >>>> >>>> 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/153efc8f-8043-4a97-a12f-295225d5b978%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
