Trivial: Get confirmation of email sent in android

2019-01-07 23:42发布

After starting an email intent how can I get confirmation that the email has sent or there has been an error back into the activity it was called from?

        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setType("png/image");

        String subject = "Email Subject";

        String body = "Message Body"; 

        emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
        emailIntent.putExtra(Intent.EXTRA_TEXT, body);
        emailIntent.putExtra(Intent.EXTRA_STREAM,
                Uri.parse("file:///sdcard/" + IMAGE_FILENAME));

        startActivity(Intent.createChooser(emailIntent, "Send email...")); 

        //Here I need to do something on a successfully sent email

Maybe start activityForResult? But what result should I expect back if any?

4条回答
forever°为你锁心
2楼-- · 2019-01-08 00:15

You can NOT do this.

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.

查看更多
做个烂人
3楼-- · 2019-01-08 00:21

you cannot get any useful resultcode from an email intent. onActivityResult always return 0 as soon as sending starts or sending is canceled.

Additionaly if you attach files, onActivityResult is called BEFORE those files are read.

查看更多
We Are One
4楼-- · 2019-01-08 00:22
疯言疯语
5楼-- · 2019-01-08 00:24

That really depends on the app that is launched by your Intent. It could be the Gmail app, it could be the Email app, or it could be any third-party app. Because of this, there is no 100% reliable way to determine whether the user actually pressed Send or not.

The only thing you can do is check if the Gmail and Email apps return anything relevant when called via startActivityForResult and rely on that. But beware that is not reliable because, again, there could be third party apps. Also, since these apps do not specify publicly what they return, they might change that at some point without any notice.

查看更多
登录 后发表回答