5

I am using the below code to send the mail

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL, new String[] { "abc@yahoo.com" });
i.putExtra(Intent.EXTRA_CC, new String[] { bcc_string });
i.putExtra(Intent.EXTRA_SUBJECT, "Video Suggest");
i.putExtra(Intent.EXTRA_TEXT, url_link); 

try {
   startActivityForResult(Intent.createChooser(i, "Send Mail..."), 1);
} catch (android.content.ActivityNotFoundException ex) {
   Toast.makeText(AllVideos.this, "There are no email clients installed.", Toast.LENGTH_SHORT)
   .show();
}

and in my on activity result i am using the following code

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // System.out.println("inactivity");
    // Toast.makeText(AllVideos.this, "Mail Send", 10).show();

    System.out.println("inside activity result");

    if (requestCode == 1) {
        if (requestCode == 1 && resultCode == RESULT_OK) {
            Toast.makeText(this, "Mail sent.", Toast.LENGTH_SHORT).show();

        } else if (requestCode == 1 && resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Mail canceled.", Toast.LENGTH_SHORT)
                    .show();
        } else {
            Toast.makeText(this, "Plz try again.", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

But everytime i send or discard the message i get "mail cancelled" toast. Please help me put with this.

Thanks in advance.

Stelian Matei
  • 11,553
  • 2
  • 25
  • 29
Shrikant
  • 1,560
  • 1
  • 15
  • 32

2 Answers2

3

as per link

You can't, this is not part of the API. It returns once you have pressed send button even if it is not sent

ACTION_SEND does NOT have any output as a result you always get the default value which is RESULT_CANCELED.

Also you can NOT check it with Intent data coming back because it is always null either mail send or discard.

Community
  • 1
  • 1
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
-1

Try this way.

 if (requestCode == 1) 
   {
        if (resultCode == RESULT_OK) 
        {
            Toast.makeText(this, "Mail sent.", Toast.LENGTH_SHORT).show();

        } 
        else if (resultCode == RESULT_CANCELED) 
        {
            Toast.makeText(this, "Mail canceled.", Toast.LENGTH_SHORT)
                    .show();
        } 
        else 
        {
            Toast.makeText(this, "Plz try again.", Toast.LENGTH_SHORT)
                    .show();
        }
  }
Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57
  • Tried your given code but it is still giving me Mail Canceled Toast. – Shrikant Jun 22 '12 at 11:13
  • I don;t think that it is specified any where that the email activity and SMS activity would return any result that can be traced..because i have tested that in my app that when a sms activity is called ...it always returns Zero...even if the message is sent or not. – Haresh Chaudhary Jun 22 '12 at 11:26
  • That might be the case for email as well, as it is always returning RESULT_CANCELED. And as android does not know whether mail is sent or not it will never return RESULT_OK. – Shrikant Jun 22 '12 at 11:39