my application need to send and receive email so using business GSuite I created an account my-app@mydomain.com and I want to use java gmail api to send and receive email.
I followed the google documentation to create and authorise the service account as follow:
- I created a service account
- I enabled G Suite Domain-wide Delegation(I think this is not needed as I'm not impersonate another user)
- I generated and downloaded the key in credentials.json
- Authorised the client in the admin console for scopes:
https://mail.google.com/
https://www.googleapis.com/auth/gmail.compose
https://www.googleapis.com/auth/gmail.insert
https://www.googleapis.com/auth/gmail.labels
https://www.googleapis.com/auth/gmail.metadata
https://www.googleapis.com/auth/gmail.modify
https://www.googleapis.com/auth/gmail.readonly
https://www.googleapis.com/auth/gmail.send
https://www.googleapis.com/auth/gmail.settings.basic
https://www.googleapis.com/auth/gmail.settings.sharing
Then try to send the email as follow:
public Credential getCredentials(final Set<String> scopes) throws IOException, GeneralSecurityException {
return GoogleCredential
.fromStream(GoogleCredentialServices.class.getResourceAsStream("credentials.json"))
.createScoped(scopes);
}
public void init() throws GeneralSecurityException, IOException {
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
gmailServices = new Gmail.Builder(
HTTP_TRANSPORT,
JSON_FACTORY,
googleCredentialServices.getCredentials(GmailScopes.all()))
.setApplicationName(applicationName)
.build();
}
public MimeMessage createEmail(final String to, final String from, final String subject, final String bodyText) throws MessagingException {
final Properties props = new Properties();
final Session session = Session.getDefaultInstance(props, null);
final MimeMessage email = new MimeMessage(session);
email.setFrom(new InternetAddress(from));
email.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
email.setSubject(subject);
email.setText(bodyText);
return email;
}
public Message createMessageWithEmail(final MimeMessage emailContent) throws MessagingException, IOException {
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
emailContent.writeTo(buffer);
final byte[] bytes = buffer.toByteArray();
final String encodedEmail = Base64.encodeBase64URLSafeString(bytes);
final Message message = new Message();
message.setRaw(encodedEmail);
return message;
}
public Message sendMessage(final MimeMessage emailContent) throws MessagingException, IOException {
final Message message = createMessageWithEmail(emailContent);
return gmailServices.users().messages().send("mail@mydomain.com", message).execute();
}
@Test
public void createAndSendGMailTest() throws MessagingException, IOException {
final MimeMessage emailContent = createEmail("dest@domain.com", "my-app@mydomain.com", "subject", "message body");
final Message message = sendMessage(emailContent);
System.out.println("message: " + message);
}
When I run the test I got 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"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:150)
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:1108)
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)
Anyone can help me to understand what causing this error?