简体   繁体   中英

How can I tell if my email has been sent?

I'm using the following code to send an email in my app.

public void sendEmail() {
    emailSent = 1;
    String to = toEmail.getText().toString();
    String subject = getUser() + " - User Feedback";
    String message = bodyTxt;

    Intent email = new Intent(Intent.ACTION_SEND);
    email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
    email.putExtra(Intent.EXTRA_SUBJECT, subject);
    email.putExtra(Intent.EXTRA_TEXT, message);

    email.setType("message/rfc822");
    try {
        startActivity(Intent.createChooser(email, "Send Email..."));
    } catch (android.content.ActivityNotFoundException ex) {
        String msg = ex.getMessage().toString();
        emailSent = 2;
        Toast.makeText(this, "There was a problem sending this email, please try again.", Toast.LENGTH_SHORT).show();
    } 
}

The email.setType() presents a screen offering options for which method to send the message including email. This works fine if the user selects email as a copy of the email comes up and the user can then send it. However, if the user doesn't do anything and simply uses the back space it returns to the app and assumes the email has been sent. How can I test for the user not sending the email?

You could startActivityForResult() and listen for a RESULT_OK in onActivityResults . But there is no framework requirement that forces the email app to setResult appropriately. So your mileage may vary based on the email client app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM