Umesh Kumar Saraswat
Umesh Kumar Saraswat

Reputation: 768

How to send email and get confirmation in Android

I am trying to send email through intent, and I want to call the API or web service after mail is successfully sent. How can I do that?

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:?subject=" + emailSubject
        + "&body=" + genericText + " " + genericLink);
intent.setData(data);
startActivityForResult(intent, MAIL_CODE);  

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == RESULT_OK) {
        // do some task
    }
}

Upvotes: 0

Views: 1225

Answers (2)

CommonsWare
CommonsWare

Reputation: 1006674

I am trying to send email through intent, and I want to call the API or web service after mail is successfully sent. How can I do that?

You can't.

First, your Intent structure is not a great choice. Use ACTION_SENDTO and the appropriate extras for a mailto:-style request. Do not assume that all email clients will support ACTION_VIEW with your specific URL structure.

Second, neither ACTION_VIEW nor ACTION_SENDTO are designed to work with startActivityForResult(); their JavaDoc entries indicate that they have no output.

Third, the user does not actually have to send an email. All ACTION_SENDTO does is bring up an email client; what the user does at that point is up to the user, not you.

Upvotes: 0

Maveňツ
Maveňツ

Reputation: 1

Try This

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(requestCode==1)
    {
        if(requestCode==1 && resultCode==Activity.RESULT_OK)    
        {
            Log.e("Mail sent.", e+"");
        }
        else if (requestCode==1 && resultCode==Activity.RESULT_CANCELED)
        {
            Log.e("Mail Cancelled.", e+"");
        }
        else 
        {
            Toast.makeText(this, "Plz try again.", Toast.LENGTH_SHORT).show();
        }
    }   
}

Upvotes: 1

Related Questions