I am using Gmail API java for sending mails to the users from a specific account of my company
. A StoredCredential
file/token is also stored for the said account. But using 'me', as mentioned here, is not working for me.
Code:
//set the Mimemessage
MimeMessage email = new MimeMessage(session);
email.setFrom(new InternetAddress("me")); //from
email.addRecipient(javax.mail.Message.RecipientType.TO,
new InternetAddress(to)); //to
//sending mail
Message message = createMessage(email);
message = service.users().messages().send(userId, message).execute();
This says:
message: Delegation denied for user@mycompany.com
As far as I understand, if I have the said token and required permissions, I should be able to send the mails, right?
Also, when I set the 'from' to a mail-address of my company account instead of 'me', the error remains.
How can I overcome this? Any workaround for this? Thanks.
Edit
Actually I'm using 4 Google APIs(Google Sheets, Drive, Gmail, and Script API), and I'm using a single oAuth check.
Code:
For Credentials
final String TOKENS_DIRECTORY_PATH = "mydirectory\\tokens";
final List<String> SCOPES = Arrays.asList(SheetsScopes.SPREADSHEETS,DriveScopes.DRIVE,DriveScopes.DRIVE_FILE,DriveScopes.DRIVE_METADATA,GmailScopes.GMAIL_COMPOSE,GmailScopes.GMAIL_SEND,GmailScopes.GMAIL_LABELS,ScriptScopes.MAIL_GOOGLE_COM);
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
// build GoogleClientSecrets from JSON file
final String CREDENTIALS_FILE_PATH = "/credentials.json";
// Load client secrets.
InputStream in = GoogleAuthorizeUtil.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
// build Credential object
Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
return credential;
The above-mentioned code is returning Google Credentials Object. Next Step is to create service for each API seperately:
Creating Services
public static Gmail getGMailService() throws IOException, GeneralSecurityException {
Credential credential = GoogleAuthorizeUtil.authorize();
return new Gmail.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
JacksonFactory.getDefaultInstance(), credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
public static Gmail setup() throws GeneralSecurityException, IOException {
Gmail gmailService;
gmailService = getGMailService();
return gmailService;
}
The above returns Gmail service (similar code is done for all four API's service).
Now using this service to send mails:
Using the Service object to send mails
Gmail gservice = setup(); //returns Gmail service
// finally using the mail service---------------------
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
String user = "me";
MimeMessage email = new MimeMessage(session);
email.setFrom(new InternetAddress(user));
email.addRecipient(javax.mail.Message.RecipientType.TO,new InternetAddress(to));
email.setSubject(subject);
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(bodyText, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);
email.setContent(multipart);
Message message = createMessage(email);
message = service.users().messages().send(userId, message).execute();
And all of this is called in a servlet when I click a submit
button.
This should give the idea about the issue but please do tell if any more info/code is needed.
Thanks.