1

I'm building a very simple android application that creates a string based on a bunch of user inputs. I want to give the user the ability to email themselves the string from the app.

I come from a php background where this is very straight forward: there's a function that takes the "to" address, body, subject etc and conveniently sends the email from php:

mail($to,$subject,$message,$headers)

This is basically what I'd like to replicate in the Android environment. I've had some success with things like the below but this just opens the users email client

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
String emailTo = userEmail;
String emailSubject = "Subject Line";
String emailBody = userString;
emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,emailTo);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,emailSubject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(emailBody));
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

Finally i tried calling out to a php script to do the send:

String phpSend = "http://www.MyPHPSendScript.com?emailbody=userString";
try {
  URL url = new URL(phpSend);
  url.openConnection().getContent();
  } catch (Exception e) {
      out.println("Failed to send email");
  }

But a) this failed and b) it seems like a bit of a hack...

Does anyone have any thoughts/suggestions?

Thanks, The Grinch

The Grinch
  • 11
  • 1
  • 2
  • 1
    I think Android suggest using the emailapplication as an Intent to make the user being able to control mails that are sent so basicaly you can write the mail for him but he has to press the send button. – Alexander Fuchs Jun 09 '12 at 10:43

2 Answers2

1

I don't have enough reputation to make this a comment rather than an answer, but following this tutorial should get you on the right track, http://www.developerfusion.com/code/1975/sending-email-using-smtp-and-java/ although in all honesty I have not done this before myself. I do know from the android docs that Socket, DataInputStream, and DataOutputStream are all supported, so don't see any reason why it wouldn't work.

Michael
  • 2,189
  • 16
  • 23
  • Thanks, some progress using this: http://www.developerfusion.com/code/1975/sending-email-using-smtp-and-java/ but, having made the connection, the response back from the host is "We do not authorize the use of this system to transport unsolicited and/or bulk e-mail." is there anyway i could set up a gmail account and connect to that? – The Grinch Jun 09 '12 at 11:23
  • Setting up a gmail account should work fine, just create an account and then go to mail settings, forwarding and POP/IMAP tab, check that POP is enabled. Then use the SMTP server "smtp.gmail.com" and port 465. You will need to use ssl so see [link](http://docs.oracle.com/javase/1.4.2/docs/api/javax/net/ssl/SSLSocket.html). Also gmail smtp server requires AUTH command after HELO, details are here: [link](https://developers.google.com/google-apps/gmail/oauth_protocol) – Michael Jun 10 '12 at 06:46
  • Also if you haven't already read it, just stumbled across this relevant question [http://stackoverflow.com/questions/4910412/looking-for-android-gmail-smtp-oauth-example](http://stackoverflow.com/questions/4910412/looking-for-android-gmail-smtp-oauth-example) – Michael Jun 10 '12 at 06:52
0

I decided to go with the PHP solution mentioned in my original post. I'm afraid I couldn't get any of the email classes working. I wasn't aware that you have to explicitly assign permission for the app to access the internet! I added the below to my manifest file and the PHP worked a treat.

Thanks for all your help and guidance!
The Grinch

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
The Grinch
  • 11
  • 1
  • 2