1

Possible Duplicates: StartActivityForResults always returns RESULT_CANCELLED for Intent.ACTION_SEND & Show Toast after email send in android?

I'd like to send an E-Mail from within my Android App.

I know you can do this with the following piece of code:

private void sendEmail(){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("message/rfc822");
    intent.putExtra(Intent.EXTRA_EMAIL, { "some_email1@gmail.com", "some_email2@gmail.com" });
    intent.putExtra(Intent.EXTRA_SUBJECT, "My Subject");
    intent.putExtra(Intent.EXTRA_TEXT, "My message");
    try{
        startActivity(Intent.createChooser(intent, "Send E-Mail"));
    }
    catch(ActivityNotFoundException ex){
        Toast.makeText(this, "There are no E-mail Clients installed on your Device.", Toast.LENGTH_LONG).show();
    }
}

What I want to know is whether or not the mail is successfully sent, or that the user has cancelled it (or something else went wrong).

I did try changing:

startActivity(Intent.createChooser(intent, "Send E-Mail"));

to

startActivityForResult(Intent.createChooser(intent, "Send E-Mail"), 123);

And adding the onActivityResult method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);

    // We come back after the starting Email Activity Client
    if(requestCode == 123){
        // Mail is successfully send:
        if(resultCode == Activity.RESULT_OK){
            // Do something
        }
        else
            // Do something else
        }
    }
}

But Intent.ACTION_SEND only gives a resultCode of 0 (which equals Activity.RESULT_CANCELED), no matter if it's successful or not.

So my question is: Does anyone know a way around this, to catch and use the result when using an E-Mail Intent. OR does someone know a completely different (custom) way to send an E-Mail from within an Android App, which enables me to use a result? (WITH code examples provided please.)

Thanks in advance for the responses.

Edit: Made the second part of the question bold, to make clear this isn't a complete duplicate question..

Community
  • 1
  • 1
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • @AleksG I know that and already added that at the start of the question.. >.> – Kevin Cruijssen May 27 '14 at 11:53
  • 1
    Then why are you even asking a duplicate question? The answer is the same - you cannot do it. If you need to get the result, then you have to implement the email mechanism yourself. It's not difficult and there are tons of information on the net - just search for [android send email via smtp](https://www.google.com/search?q=android+send+email+via+smtp&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&channel=fflb) – Aleks G May 27 '14 at 11:59
  • @AleksG Thanks for the edit, the second part of your comment is very promising. – Kevin Cruijssen May 27 '14 at 12:02
  • Ok, I ended up using the [JavaMail API](http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_%28no_Intents%29_in_Android). – Kevin Cruijssen Aug 19 '14 at 12:58

0 Answers0