Mail and Calendar in Android apps

While there isn’t a dedicated API in the Android SDK for sending email messages or adding events to a user’s calendar, there is a way to perform either task using specially constructed intents.
In both cases the idea is to start an activity that manipulates a specific data type: the advantage of this approach is that such code will make use of whatever app is installed on the device for mail and calendar events, without the developer needing to know the specific details of such apps or override the user’s preferences.

Mail

This snippet of code allows you to request that an email activity be started and a new message form filled in with the given data.

Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, recipientStringArray);
email.putExtra(Intent.EXTRA_TEXT, messageString);
email.putExtra(Intent.EXTRA_SUBJECT, subjectString);
email.setType("message/rfc822");
startActivity(email);

If more apps that handle this intent, the user will be given a choice. If you want to add an attachment to your message, prepare the intent with the following addition

email.putExtra(android.content.Intent.EXTRA_STREAM,Uri.parse(fileUrlString));

Email Alternatives

The way indicated above uses not just the application(s) found on the phone, but also the configuration that goes with it (or them). If you prefer to have more control over the composing and sending process, or want to allow sending the message from any email address rather than the ones users have configured in their email apps, then you can just send the message manually using the SMTP protocol (check out this stackoverflow response) or through an HTTP request to a support web page that handles the sending of the message with a server-side language like PHP.

Calendar

The intent that adds a new event to a calendar application on an Android device should be configured like in this example

Calendar cal = Calendar.getInstance();
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
startActivity(intent);

Again, this will open whatever calendar app is available on the Phone, or give the user a list to choose from if more than one are found.

Calendar Alternatives

There is an undocumented content provider that offers access to the default calendar app available on all Android devices. Though the API is more straightforward then using intents, it is not significantly simpler and it has the big disadvantage of being not official. It can (and has) changed between releases and could be removed at any time. You can find more information about it on this web page, but do check out the comments to verify if there have been any updates in the SDK.

Leave a comment

Your email address will not be published. Required fields are marked *


*