Send email through Gmail API Google App Engine
<p>Trying to send email using the <code>Gmail API</code> into <code>GAE</code>. Previously I created a Service account key into Credentials page, it generates a <code>.P12 file</code> which is in <code>setServiceAccountPrivateKeyFromP12File</code> parameter. It has a ID key joined to the account <code>example@appspot.gserviceaccount.com</code> into Service account page. The code:</p>

<pre><code>    /* Application name. */
    private static final String APPLICATION_NAME = "appnamefromappengine";

    String emailAddress = "somename@appspot.gserviceaccount.com";
    JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    try {
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();

        Set&lt;String&gt; scopes = new HashSet&lt;String&gt;();
        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("somename@appspot.gserviceaccount.com")
            .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("someemail@gmail.com"));
        message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress("recipient@gmail.com"));
        message.setSubject("Test Mail");
        message.setText("Test Mail");

        Message msg = createMessageWithEmail(message); //createMessageWithEmail function from Gmail API
        msg = gmail.users().messages().send(emailAddress, msg).execute();

        System.out.println("Mail was sent. Message id: " + msg.getId());

    } 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();
    }
</code></pre>

<p>It returns me this error: </p>

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

<p>I'm not sure which parameter I'm setting wrong here in code or into Google cloud console. What else can I try ?</p>