How to wait for email intent to complete and get the result?

In my Android app, I can programmatically open the default email editor using 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 fine, but I need to wait in the application until the user finishes working with the mailbox, and also knows whether the letter was sent or sent.

Does anyone know how to do this?

+7
source share
2 answers

You can usually use startActivityForResult (), which starts the second action as a sub-activity. However, in the case of email activity, this does not seem to work, probably due to an internal implementation. Try searching before submitting questions:

how can we use startActivityforResult () for email intent?

Actual email sending is asynchronous in design, so activity is likely to return before email is sent. I have not tested this case on purpose, but from the link above it seems that the action returns when the user clicks the submit button. If this is enough for your use case, then super, if you need to know if the letter is really sent, you can be SOL.

+2
source

It will be hard. There is no default return value for sending email, and depending on your preferences, mail can be sent using the email application, the Gmail application, or one of many third-party email applications. Most likely, they all differ in how they handle the end of letters.

As for your question in general, you can use startActivityForResult() to start the action and then continue after it completes, with a return value indicating how everything went. However, I am sure that most email applications will not give you the proper result.

I am tempted to say that you may have to process the sending of the letter yourself, i.e. write a simple function that connects to SMTP and sends mail. I am sure that there are many libraries that handle all the hard work.

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

+1
source

All Articles