Error when trying to send an email using Gmail API in Java
<p>I want to send an email using Gmail API (not JavaMail). I read many similar topics on forums, but I have still a problem.</p>

<p>First I read this: <a href="https://developers.google.com/gmail/api/guides/sending" rel="nofollow">https://developers.google.com/gmail/api/guides/sending</a></p>

<p>and I impelemented methods: MimeMessage createEmail, Message createMessageWithEmail and Message sendMessage</p>

<p>Then I noticed that I don't have definition of object Gmail service, so I needed a class GmailQuickstart, which is here: <a href="https://developers.google.com/gmail/api/quickstart/java" rel="nofollow">https://developers.google.com/gmail/api/quickstart/java</a></p>

<p>and I implemented: class GmailQuickstart, method Credential authorize() and method Gmail getGmailService()</p>

<p>Finally I wrote a main:</p>

<pre><code>public static void main(final String[] args) throws MessagingException, IOException {
    String APPLICATION_NAME = "gmailProject";
    HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
    JsonFactory JSON_FACTORY = new JacksonFactory();
    Credential credential = GmailQuickstart.authorize();

    String to = "frommail@gmail.com";
    String from = "tomail@gmail.com";
    String subject = "Subject";
    String bodyText = "Body";
    MimeMessage emailcontent = createEmail(to, from, subject, bodyText);
    createMessageWithEmail(emailcontent);
    Gmail service = new com.google.api.services.gmail.Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME).build();
    sendMessage(service, "me", emailcontent);
}
</code></pre>

<p>After this I have a long lists of errors:</p>

<pre><code>Exception in thread "main"com.google.api.client.googleapis. json.GoogleJsonResponseException: 403 Forbidden
{
  "code" : 403,
  "errors" : [ {
  "domain" : "global",
  "message" : "Insufficient Permission",
  "reason" : "insufficientPermissions"
} ],
"message" : "Insufficient Permission"
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJs nResponseException.java:146)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
    at org.actuarlib.outsourcing.mail.Mail.sendMessage(Mail.java:78)
    at org.actuarlib.outsourcing.mail.Mail.main(Mail.java:195)
</code></pre>

<p>I know that on stack there are many topics about similar error, but I don't know how to correct it.
Anyone can tell me what's wrong or do you know another way to send an email using Gmail API?</p>