How to Send an Email


Another common requirement is to send an email from your application. At first, this looks easy. You use an ACTION_SEND Intent as demonstrated by the example on OpenIntents

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "email text");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
sendIntent.setType("message/rfc822");
startActivity(Intent.createChooser(sendIntent, "Title:"));

However, this sends the email via the Android mail app in the phone, prompting you which email app if you have more than one configured. So, how do you send an email silently? It turns out that there’s no official way to send an email directly (via SMTP). The Google rationale is that, for security reasons, the user needs to know (and confirm) that the application is sending an email on their behalf.

However, what if you still want to send silent emails? One option is to port a 100% Java SMTP library. You might try JavaMail. There’s a .jar file on the Google groups but I wouldn’t use it if I were you as it doesn’t respect the original source code license and you don’t really know what you are including.

There’s another attempt at putting together bits and pieces with more details how to go about this yourself but it’s really lots of hacking which most people might like to avoid. Someone else has also claimed to port gnu inetlib.

If you want a cleaner way of sending via SMTP, without polluting your application with lots of unwanted classes, then one solution might be to get your server to do it via a simple php script and send up the information via a http GET or POST.

Comments are closed.