9

In my Android app, I am able to programmatically open up the default email editor with To, Subject, and Message using the following:

Intent emailIntent=new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, toemail);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
emailIntent.setType("text/plain");
emailIntent.setClassName("com.android.email", "com.android.email.activity.MessageCompose");
startActivity(emailIntent);

This works great, but I need to wait in my app until the user finishes with the email screen and also know whether the email was sent or discarded.

Anyone know how to do this?

Nicholas
  • 447
  • 1
  • 4
  • 18
  • You should use `emailIntent.setType("message/rfc822");`… Reference: [Internet media type](http://en.wikipedia.org/wiki/Internet_media_type#Type_message). – Lincoln Hawk Jul 09 '12 at 03:54

2 Answers2

2

Normally, one could use startActivityForResult() which starts the second activity as a sub-activity. However, in the case of the email activity this doesn't appear to work, likely because of the internal implementation. Try searching before posting questions:

how can we use startActivityforResult() for Email intent?

The actual sending of an email is asynchronous by design, so the activity will likely return before the email is actually sent. I haven't tested this case specifically, but from the above link it seems that the activity returns once the user hits the send button. If this suffices for your use case then super, if you need to know if the email was actually sent you might be SOL.

Community
  • 1
  • 1
jfelectron
  • 1,033
  • 9
  • 6
  • Yes, as EboMike suggested you can bypass the email clients using JavaMail, which appears to work on Android: http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-android-ap – jfelectron Dec 14 '10 at 19:34
  • 1
    I always search, naturally, but didn't see that post. Good starting point. I don't actually care if the email got sent, I'm only interested in whether the user pressed Send or Discard. I'll tinker with this... – Nicholas Dec 15 '10 at 21:53
1

This is going to be tricky. There is no standardized return value for email sending, and depending on the user's settings, the mail could be sent with the Email app, the Gmail app, or one of the many 3rd party email apps. They most likely all differ in how they handle ending the emails.

As for your question in general, you can use startActivityForResult() to start an activity and then continue after it finishes, with a return value indicating how things went. However, I'm pretty sure that most email apps won't give you a proper result here.

I'm tempted to say that you may need to handle the sending of the email yourself, i.e. write a simple function that connects to the SMTP and sends the mail out. I'm sure there are lots of libraries out there that handle all the heavy-lifting.

(You can, of course, experiment with startActivityForResult first - MAYBE the most common email apps do give you a return value.)

EboMike
  • 76,846
  • 14
  • 164
  • 167
  • 1
    Hmm, thought about SMTP or JavaMail, but in the end my app isn't a mail app. Mail is just used to "tell a friend" and I'd like the view to dismiss automatically if the user completes it. Very minor feature but it would make the UI smoother. – Nicholas Dec 15 '10 at 21:56