Skip to content

vahidhedayati/gmail-api-test

Repository files navigation

Gmail API Test running on a grails(2.4.4) application

When attempting to initiate your very first email from a given account, please ensure you are doing so from a development environment to be able to login to correct gmail account and give correct authority. When this step has been completed for the very first time a file is created under securityPath containing clientCredentials. This file is generated by gmail api once you have completed this step.

This demo site provides:

Message / HTML

above form can be sent as standard / HTML

or standard to multiple users (this will be the to and emailAccount) as the to listing

Attachment

this will send a standard text email with the attachment included

Inline image

This loads up c:\gmail-test\images.jpg (refer to service file for file)

when inline image loaded by default it provided:

<img src="cid:myimage" /> <h1>inline image</h1>

The myimage tag is then converted to c:\gmail-test\images.jpg. Purely used to demonstrate or recreate standards you would have with java mail API.

Provides verifyBounce action

Modified to work more like this where each failure is then read back from server to work out who/when. Older method to use internal concurrent hashMap still left for reference.

Getting started: You must get authorisation key from:

client_key explained here, Once you have the file store it in a location as per configuration

https://console.developers.google.com/apis/credentials/consent?createClient.. in here put in project name as the APPLICATION NAME you are setting below, also set privacy policy

In https://console.developers.google.com/apis/credentials/oauthclient?project= .. set the name to be TEST I think and redirect URIs to be as hardcord http://localhost:9091/Callback

On your development machine, you will need to create the securityPath and secretFolder

//securityPath is the root container of all your secret files/accounts
String securityPath = 'c:\\\\gmail-test\\\\accounts\\\\'
	
//The accounts folders per gmail account since the `dataStoragePath` gets created once per account
//PATH: c:\gmail-test\accounts\account1
String secretFolder = 'account1'
	
//This will be automatically created upon first login of account1 and will give us:
//PATH: c:\gmail-test\accounts\account1\vh
String dataStoragePath'vh'
	
//This is the file you saved through the web interface url: https://console.developers.google.com/apis/credentials/oauthclient?project=
//PATH: c:\gmail-test\accounts\account1\client_secret.json
String secretFile = 'client_secret.json'
	
//The appName you defined when creating above json file
String appName =  'TEST'

Above now binds into their own methods to create their internal files/folders

So when creating multiple accounts:

Account1 (Where you create c:\gmail-test\accounts\account1) and store client_secret.json inside it giving:

c:\gmail-test\accounts\account1\client_secret.json where c:\gmail-test\accounts\account1\vh will be auto created

Account2 (Where you create c:\gmail-test\accounts\account2) and store client_secret.json inside it giving:

c:\gmail-test\accounts\account2\client_secret.json where c:\gmail-test\accounts\account2\vh will be auto created

Then obviously you have a process that triggers changes to above secretFolder and if everything else is the same then it will trigger that account.

For each account you would go to developer console and generate a secret json file save and follow above process.

For the very attempt to send an email a verification step is required with the real account. So from a development machine ensure you are also logged in to correct account. Please visit http://stackoverflow.com/questions/42702921/gmail-api-oauth-two-accounts-possible-issue to see why In short I found after generating for account Y if I were logged in with account X during this last step. All emails would then be sent from account X (seems like a bug). This first step triggers the internal work within gmail API to create the c:\gmail-test\accounts\account2\vh\StoredCredential. This file created is identical for each account hence the separation of account folders.

Overriding / using multiple gmail accounts.

Looking at how getGmail works, it has been rather complicated to try and override a given account, none the less. Take a look at Dynamic Controller where it does such a thing.

where c:\gmail-test\accounts\account1\client_secret.json already exists and c:\gmail-test\accounts\account1\vh will be created to store credentials
where c:\gmail-test\accounts\account2\vh.json already exists and c:\gmail-test\accounts\account2\vh will be created to store credentials
Then you can verify each account like: (not working correctly)