2

How can I send email from a Gmail account using Google-App-Engine? The sender address is the problem, this is understandably restricted because of spam. (Restrictions are here: https://developers.google.com/appengine/docs/java/mail/usingjavamail#Senders_and_Recipients )

Aim: I want users to come to the web site, register to use the service (via OAuth). And the service will be able to send email with the "sender address" set to the users email address. (Service: spreadsheet formula that sends email)

Related question: Accessing Gmail account from Google App Engine

Options:

1) Avoid JavaMail: Email via HTTP looks like it would work for a small fee. Could use: Amazon SES aws.amazon.com/ses/

not possible 2) contextIO - some people have suggested contextIO. Update: does not send email, see http://context.io/docs/2.0

3) OAuth - it looks like you can use OpenID and send email as the current logged in user. So maybe offline sending with OAuth is possible. (Users API https://developers.google.com/appengine/docs/java/users/)

4) sender verification - (Only usable to test things out, not a production quality solution) App Engine has "Invite a user to collaborate on this application" as viewer. Maybe there is a API for adding collaborators.

Picked 5) Sockets/SMTP trial - (authenticated SMTP only) in Sep 2012 google posted http://googleappengine.blogspot.com.au/2012_09_01_archive.html

Community
  • 1
  • 1
eddyparkinson
  • 3,680
  • 4
  • 26
  • 52
  • 1) no, AFAIK; also http://stackoverflow.com/q/4000748/257568 2) the easiest path, IMHO 3) only if there is an HTTP api and I don't see one 4) sounds a wrong way – ArtemGr Apr 03 '13 at 07:41
  • thanks, did more hunting. contextIO does not send email :( – eddyparkinson Apr 04 '13 at 02:02
  • 1
    >the Sockets API lets you use SMTP (authenticated SMTP only). From here: https://groups.google.com/forum/?fromgroups=#!topic/google-appengine/YNEnaGQqqZE – eddyparkinson Apr 04 '13 at 03:05
  • What is wrong with the Mail API again? Why can't you simply add the account as a developer in the admin console, and use the Mail API? – Ezra Apr 06 '13 at 14:40
  • @Ezra thanks for asking, short term a developer account is ok, but long term I need a better solution. 2 Reasons: Security and Sign up process. Security is an issue because they can see the logs, an so I would need to disable logs in production. The sign up process is for a apps developer, not a spreadsheet service. – eddyparkinson Apr 08 '13 at 01:53
  • Is it sufficient to set the "reply-to" field instead of the from? I think you're going to have to bend over backwards to send as your clients. Even when it's all set up, you could have all kinds of problems with SPAM filters since setting up/verifying with Sender Policy Framework will be impossible and they might tag you as a spoofer and sent you to the Junk folder. Not to mention that, as a customer of your service, you sending email as me would kind of freak me out. I think the above solution is what most services use, or just a "on behalf of" note in the email somewhere. – Ezra Apr 09 '13 at 17:13

4 Answers4

2

The good news is that with the arrival of GAE SDK 1.7.7 it will be possible to talk directly to IMAP and SMTP, see the blog post http://googleappengine.blogspot.be/2013/04/app-engine-177-released.html

The critical new feature (in preview) is "Outbound sockets"

koma
  • 6,486
  • 2
  • 27
  • 53
  • looks promising. Next step, use OAuth + gmail to send email on app engine: http://stackoverflow.com/questions/14107563/get-oauth-access-to-gmail – eddyparkinson Apr 11 '13 at 05:15
  • Update: got "Outbound sockets" working (i.e. enabling billing removed FeatureNotEnabledException). But OAuth + SMTP + GMail is not working, same error as here: http://stackoverflow.com/questions/14064454/send-mail-via-smtp-gmail-oauth2-issue – eddyparkinson Apr 12 '13 at 05:22
  • 1
    Update: To send the email with OAuth, this code worked: http://stackoverflow.com/questions/12503303/javamail-api-in-android-using-xoauth/12821612#12821612 – eddyparkinson Apr 16 '13 at 02:25
1

I have not tried this on production, but I've set up the GAE dev server to bypass google's mail system and talk to an SMTP server directly. See my answer on another question. If you can do this on production, all you'll need is an SMTP server somewhere that will send your emails.

Community
  • 1
  • 1
Patrick Simpson
  • 554
  • 4
  • 12
1

In a nutshell - you will not be able to do that via OAuth. The main reason is that it's generally imposible to (reliably, continuously) send mails in volume on behalf of random users, i.e. users that are not on the domain that you control.

  1. If you use OAuth than you will need to ask for access to users profile/email during OAuth procedure. This varies from provider to provider. Some providers never make email available (Twitter). See pac4j library that provides unified API to retrieve user's OAuth profile from different providers.

  2. When (and if) you get email adress from OAuth, than you will need to send email on behalf of that user. Since your SMTP server is not authorised to send emails on behalf of random users (see SPF and DKIM) you will quickly get on spam lists and be blocked. If this was easy then spammers would have easy life.

  3. You can send email on users behalf from AppEngine, but only if users log in via Users Java API, which is only available to Gmail or Google Apps accounts.

  4. On GAE you can easily use external SMPT server, via the new Outbound Sockets API (this just went from trusted tester feature to experimental feature in sdk 1.7.7). We have this setup and it works with no problem, using a large external SMTP service. But this does not help much with what you want to achieve, considering the point 2 above.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • The docs I have seen suggest it is possible to send gmail via SMTP using oauth: https://developers.google.com/google-apps/gmail/xoauth2_protocol#oauth_20_scopes Where the "sender" email address is the same account as the oauth token. What did I miss? – eddyparkinson Apr 11 '13 at 23:50
  • I did not know about that one - good find. Well it seems that in the case of OAuth+Gmail it's possible. Just use data provided in your link and test it out on OAuth playground: https://developers.google.com/oauthplayground/ – Peter Knego Apr 12 '13 at 05:41
  • Hmm, it seems that the new GAE Outbound Sockets feature does not allow connecting to Google IP ranges, including Gmails SMTP server: https://developers.google.com/appengine/docs/java/sockets/overview – Peter Knego Apr 12 '13 at 05:42
  • Peter, thanks for the link. um, it says: except those whitelisted: Gmail SMTPS: smtp.gmail.com port 465 and 587 – eddyparkinson Apr 12 '13 at 05:59
  • Ehh, you are right. Damn my speedreading. It seems you have all pieces available and it's possible to do what you planned. – Peter Knego Apr 12 '13 at 06:19
  • We are using Outbound Sockets to talk to external SMTP (non Google) and it works, so in principle you should be able to do that. – Peter Knego Apr 12 '13 at 06:21
  • @PeterKnego what are the restrictions, it is said that Port 25 is blocked? – quarks Apr 22 '13 at 08:51
1

We ended up to use external Amazon SES service for emails as Google limited us - in "from:" field was allowed only app admin email address. We tried to switch DKIM or SPF records but this did not helped us, google is very strict in this.

Martin V.
  • 3,560
  • 6
  • 31
  • 47