2

I've been searching to find a solution for my problem for days without result. The answers I found come close to what I want, but it's not enough to make it actually work.

What do I want?

I want to give the users of my program the possibility to send an email using their own Gmail account after giving my program that privilege with oauth2. My program will then send an email, using that account, on certain conditions (programatically) to the desired receivers (this can also be yourself). This means, after the user is giving permission using oauth2, my program will then make the decision when to email the desired receivers inclusive the contents of that email. This is of course only in the best interest of the user who will be fully aware of this functionality when he/she turns it on.

What have I done so far?

Of course, I have turned on the Gmail API at console.developers.google.com and tried different kind of credentials (native/android ...)

I've been reading and trying and reading and trying...

https://developers.google.com/gmail/api/

Handling expired access token in Android without user concent

looking for Android Gmail SMTP Oauth example

How to Login into Gmail using OAuth in Android Application?

https://developers.google.com/accounts/docs/OAuth2

http://developer.android.com/google/auth/http-auth.html

https://developers.google.com/api-client-library/java/apis/gmail/v1

https://developers.google.com/api-client-library/java/google-api-java-client/oauth2

https://developers.google.com/accounts/docs/OAuth2ForDevices

Plus the first 20 like Google results for every combination of search criteria using the words, Android, Gmail, oauth, email, send etc.

The code I used is nowhere different from the sources I used, it is possible to select a google account in my program (app). Below is a part of the piece of (mind the ugliness at this point) code after an account has been selected. Depending on what part of the code I use (uncomment) I get different error codes, like 403: access not configured or 401: unauthorized

@Override
protected Void doInBackground(Void... params) {

    String token = null;

    try {

        token = GoogleAuthUtil.getToken(activity, userId, scope);

        if (token != null) {
            //Log.e("token", token);

            // send test mail
            HttpTransport transport = new NetHttpTransport();
            JsonFactory jsonFactory = new JacksonFactory();
            // load secret  
            //Reader secret = new InputStreamReader(activity.getAssets().open("secret.json"));
            //GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory, secret);
            // add scopes
            //Collection<String> scopes = new ArrayList<String>();
            //scopes.add(OAuthScopes.GMAIL.COMPOSE.getLocation());

            //GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, clientSecrets, scopes)
            //  .setAccessType("online").setApprovalPrompt("auto").build();

            //String url = flow.newAuthorizationUrl().setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI).build();

            //GoogleCredential credential = new GoogleCredential().setAccessToken(token);

            Reader secret = new InputStreamReader(activity.getAssets().open("secret.json"));
            GoogleCredential credential = new GoogleCredential.Builder().setClientSecrets(GoogleClientSecrets.load(jsonFactory, secret))
                    .build();

            Gmail gm = new Gmail.Builder(transport, jsonFactory, credential).setApplicationName("Test").build();

            //Gmail gm = new Gmail(httpTrans, jsf, null); 

            Properties properties = new Properties();
            properties.put("mail.smtp.starttls.enable", "true");
            properties.put("mail.smtp.starttls.required", "true");
            properties.put("mail.smtp.sasl.enable", "false");

// etc

Hope someone can help me out here!

Community
  • 1
  • 1
vaultboy
  • 173
  • 1
  • 1
  • 12
  • I have the same need: sending an email without user interaction. The user only presses a button and the email is sent. I managed to do it using a simple "javax.mail.Authenticator" and storing the account's password in a file, but it's not how I want to do as it's not safe. If I find a solution, I post it here. Please do the same in case you find something. Good luck :) – marco Mar 14 '15 at 09:32
  • Sounds like a deal! Good luck to you too :-) – vaultboy Mar 16 '15 at 00:08
  • As it seems really difficult to send *background* emails using the account, I have *"solved"* in another way. I created a gmail account and setup the server to send emails using that account (msmtp + php). When a user clicks the button, an email is sent to the receiver and the email contains an html "reply" link to the sender. If the receiver replies, they are connected. This fulfills 90% of my needs but probably not yours. What I don't like is that the receiver is able to reply modifying the subject and that I don't know if he replied or not but I'm working on it... – marco Mar 18 '15 at 12:37

0 Answers0