2

Why is it so hard to find an Android example of sending email using Oauth and Google Mail?

I am new to Java and Android and am having a hard time working this out. It is possible?

I have found this Gmail (or POP3) library for Android development

Which links to this http://code.google.com/apis/gmail/oauth/code.html

But no working Android examples to be found anywhere.

I would this this is possible. I have an app that sends email from the users Gmail account using this code

http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_(no_Intents)_in_Android

But my users do not like to enter their Google password in my app. And I don't blame them. In this day and age you would think that there would be an easy solution with many examples.

So, are there examples out there and I am just missing them?

TIA

Community
  • 1
  • 1
Doug
  • 1,991
  • 4
  • 25
  • 35
  • "Why is it so hard to find an Android example of sending email using Oauth and Google Mail?" -- perhaps because developers would rather use `ACTION_SEND` to support the user's choice of mail client. – CommonsWare Feb 06 '11 at 00:25
  • I do use ACTION_SEND in one part of my application but for my specific question above I need to accomplish the task without showing any user interface. The user clicks a button and the email is sent in the background. Make sense? – Doug Feb 06 '11 at 00:30
  • Your desired feature is comprehensible. If for some reason I were going to implement it, I would do so in a Web service using my own mail account, and call that Web service from the Android app. Your proposed implementation only makes sense for people who actively use Gmail, which is a modest percentage of the Android user base. Now, perhaps there is something about your app that will only attract Gmail users, and perhaps there is something about your app that mandates the email go out under the user's own account -- I have no way to know that based on your question as phrased. – CommonsWare Feb 06 '11 at 00:40
  • @CommonsWare: I am starting to think that a web service is going to be the best way to go. I just wanted to have the app be self contained and not rely on something outside of it like a web service. I assumed that all Android users had a Google account. I am learning that is not necessarily the case. Now my big question is what web host to use that will let me send out quite a bit of email from my web service. Any recommendations? I guess that will be my next StackOverflow question. – Doug Feb 06 '11 at 00:51

2 Answers2

3

XOAUTH and Android with source code:

Implementing SMTP or IMAP XOAuth

HTH

ldx
  • 3,984
  • 23
  • 28
  • ldx, what is the next step in the process? I have finally successfully gotten through the oAuth piece. Now how do I send an email? Looking into this further I am starting to think that once I have authenticated via oAuth that I will then need to use an external library like javamail to then send the email and somehow pass the oAuth credentials. Do you have any code to share for this next important piece? Thanks! – Doug Apr 16 '12 at 15:29
  • @Doug have you seen this: http://nilvec.com/sending-email-without-user-interaction-in-android/ Just authenticate with XOAUTH, then use the process described in this article to actually send an email. Let me know if it works out for you. – ldx Apr 16 '12 at 18:51
  • no, I had not seen that thanks. Just read it. So the solution is to authenticate with XOAUTH and then download the patched jakarta commons Net library and then determine how to send an email with it? OK, I will give it a shot. Thanks! – Doug Apr 16 '12 at 22:15
  • I did find some code that you posted for using the mail lib. And I can successfully use it to send an email... but only if I use my Gmail username and password. How do I send in the XOAUTH credentials? Any code samples? Thanks! – Doug Apr 17 '12 at 12:22
  • Using `SMTPClient`, instead of `login()`, use something like `client.sendCommand("AUTH XOAUTH " + xoauthString);`. – ldx Apr 17 '12 at 12:51
  • @Doug can you please share a sample code or guide me how you achieve this in steps.Thanks in advance – Talha Q Jul 15 '14 at 01:29
  • @TalhaQ, I never did get this working and ended up scratching this feature. Let me know if you happen to get a working example up. – Doug Jul 15 '14 at 11:54
  • @Doug thanks for the clarification..because you accepted the given solution as the right answer so i thought that it might worked for you.Well it has been few days i am trying to achieve this but couldn't succeeded.If i find a solution i will definitely post the solution with working code.Thanks :) – Talha Q Jul 15 '14 at 17:15
0

I actually asked if there was a "production quality" OAuth implementation around in another question:

Is there a production quality OAuth sample for Android?

Looks like that example I linked to is the best that's floating around. I've ended up having to tweek it a bit to make it really work as expected (have the back button work as expected for example). The best technique I've been able to dig up so far is to have the activity which launches the OAuth activity set as singleTask, and when you swap back to it from the OAuth activity set the FLAG_ACTIVITY_CLEAR_TOP flag in the intent.

Community
  • 1
  • 1
mikerowehl
  • 2,222
  • 1
  • 17
  • 20
  • Thanks Mike, but that is part of my issue. I can't find anything for sending Gmail. I see a bunch of OAuth Twitter examples but like you said, they are rough around the edges. And just for clarification. I guess I am not hung up on OAuth. If xOAuth or some other method exists that would allow me to send email without using a web service and without having to store the users password in my app... I am all for it. This seems crazy that this is so hard. – Doug Feb 06 '11 at 00:36
  • @Doug I just did that (Gmail SMTP/IMAP with XOAuth) a few days ago. The first part is here: http://nilvec.com/implementing-client-side-oauth-on-android/ I've also added a method to generate an XOAUTH signature, I can put up the source code for the class if you're interested. – ldx Feb 09 '11 at 10:58
  • @ldx Yes! Yes! I would be indebted to you forever! How about posting what you got as an answer and I can mark it as THE answer? – Doug Feb 10 '11 at 02:57
  • @Idx How did you manage the access tokens that would expire after an hour? – n0rm1e Aug 08 '12 at 06:46