6

I have an application where I send EMails using an intent as shown below:

//TODO attach and send here
try {           

    Log.i(getClass().getSimpleName(), "send  task - start");

    String address = "emailHere@yahoo.com";
    String subject = "Order of " + customer + " for " + date;
    String emailtext = "Please check the attached file. Attached file contains order of " + customer;

    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
    emailIntent.setType("plain/text");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { address });
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext);

    ArrayList<Uri> uris = new ArrayList<Uri>();
    Uri uriList = Uri.fromFile(orderListFile);
    uris.add(uriList);

    emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

    this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));

} 
catch (Throwable t) {
    Toast.makeText(this, "Request failed: " + t.toString(),
    Toast.LENGTH_LONG).show();
}

Now, the user chooses which application he or she wants to use in order to send that EMail. However, once the selected Email application takes over, I know there's no way to figure out if the email was sent properly or not. It has been discussed in several questions here that using startActivityForIntent() does not help since RESULT_OK is never sent by the EMail or GMail Ap so I wouldn't know if the user sent, discarded, or saved the email as a draft.

However, one possible work around is to check the Sent Items of that email account and check from there if the user sent an email or not. Now, is there a way to know the sent items of an email account in Android? I've been doing a google search for the past hour and I can't seem to get anything.

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
Razgriz
  • 7,179
  • 17
  • 78
  • 150
  • 4
    1 is not possible; 2 used to be possible some time ago, but not anymore (AFAIK). Is using JavaMail (i.e. sending e-mail programmatically, without user interaction) a valid alternative for 3? It's a solution for a different use case than what you were targeting, but if you don't need UI and the e-mails don't necessarily have to be sent from the app user's address, then perhaps it could be used. – matiash Jun 14 '14 at 04:11
  • You can get the sent items using `Uri.parse("content://sms/sent");`..Did you try that??? – Lal Jun 15 '14 at 17:57
  • 4
    To be honest I think you're trying to solve the wrong problem. I personally would not want to use an app that starts digging through my emails. Technical difficulties aside, what's in my mailbox is None Of Your Business. If you need this sort of control to check whether the email was actually sent, you should submit the emails to your own backend system and send them from there. – Barend Jun 15 '14 at 19:00
  • 1
    There's no way to get the status of sent emails since there's no common API to do that check. Some email clients have an API which would allow you to check the sent folder but since you don't know which email client the user uses and since only certain clients have an API there's just no reliable way to do what you want to do. – Emanuel Moecklin Jun 17 '14 at 13:20
  • 1
    I also agree with Barend that this is the wrong approach. If you decide to use another app to send the mails, it's shoot and forget. If you need to make sure the mail is really sent, set up your own SMTP server and send it through that server. The client side of SMTP is fairly easy to implement and there are tons of libraries for that too. – Emanuel Moecklin Jun 17 '14 at 13:22
  • The protection level is signature – Moonhead Jun 21 '14 at 00:55
  • I forgot to update this damn. I ended up using JavaMail for Android. – Razgriz Jun 21 '14 at 03:45
  • @Razgriz And you don't you think maybe I should be awarded the bounty? :( – matiash Jun 21 '14 at 06:46
  • I will award the bounty in a few moments, I've been a bit busy lately. – Razgriz Jun 21 '14 at 13:02

2 Answers2

6

You cannot check the content of the Email ContentProvider, because this requires a permission that only system apps can request. This is defined in AndroidManifest for Email:

<permission
    android:name="com.android.email.permission.ACCESS_PROVIDER"
    android:protectionLevel="signature"
    android:label="@string/permission_access_provider_label"
    android:description="@string/permission_access_provider_desc"/>
…
<application>
    …
    <!-- This provider MUST be protected by strict permissions, as granting access to
         it exposes user passwords and other confidential information. -->
    <provider
        android:name=".provider.EmailProvider"
        android:authorities="com.android.email.provider;com.android.email.notifier"
        android:exported="true"
        android:permission="com.android.email.permission.ACCESS_PROVIDER"
        android:label="@string/app_name"
        />
    …
</application>
rds
  • 26,253
  • 19
  • 107
  • 134
1

For the first option, neither the standard AOSP email app nor the Gmail app behave like this. They do not return any different information if the email was sent, saved so a draft, or discarded altogether.

Even if they did, the user might have installed a different email app, and you'd have no guarantee that it would play nice. And even if all email apps behaved this way, the user might've edited the subject, body, or "to" field, so it wouldn't even be a guarantee that "the email you intended to be sent" was actually sent.

As for the second option, email apps do not generally export content providers. For example the manifest for the AOSP email app shows that the com.android.email.permission.ACCESS_PROVIDER permission has a protectionLevel of signature, which means only apps signed by the same party (Google in this case) are able to access them.

Gmail used to have a content provider (via the com.google.android.gm.permission.READ_GMAIL permission), but this was changed a long time ago (around Android 2.2, as per the comments on this answer to have protectionLevel="signature" as well) and now only provides information on labels (see the documentation).

Given the privacy risks involved, I would say it's unlikely that other e-mail clients expose content providers on the e-mail themselves.

Depending on your actual use case, an alternative third option would be to send these emails without user interaction (e.g. with JavaMail or a similar library). This would work, for example, if you're using these emails as a way to communicate with the backend (although if that were the case, an http post would be far more suitable).

Community
  • 1
  • 1
matiash
  • 54,791
  • 16
  • 125
  • 154