0

I'm developing an app with the emulator, which includes an email send facility. The email needs to have a HTML attachment. The HTML file is being created in the app's cache area, and I've verified it exists, with correct content. The file is located in

/data/data/<package name>/cache/temp_file.html

The email is being sent fine, but there's no attachment. There's no error in the log, and no exceptions thrown. I thought it was related to file permissions, so I made sure the file was created with read access to everyone. I'm also granting READ_URI permission to the child activity (email client). So I don't see why the attachment isn't being sent with the email. After scouring many articles on this, I seem to be doing everything required, but it's not working. Note that I'm using a HTML file wrapper class as described in this article http://blog.blundell-apps.com/html-in-an-email-intent/ So getFilePath() returns the Uri of the file attachment.

private void sendToRecipients(String[] emails) {
      HtmlFile emailText = buildEmailContent();
      Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
      emailIntent.setType("text/html");
      emailIntent.putExtra(Intent.EXTRA_EMAIL, emails);
      emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Details");
      emailIntent.putExtra(Intent.EXTRA_TEXT, "Details");
      emailIntent.putExtra(Intent.EXTRA_STREAM, emailText.getFilePath());
      emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);     
      startActivity(Intent.createChooser(emailIntent, "email"));
}

I'm also using a content provider, which is declared in the manifest like this -

    <provider
        android:name="<package>.CacheFileProvider"
        android:authorities="<my authority>"
        android:enabled="true"
        android:exported="true"
        android:grantUriPermissions="true"
    />

UPDATE:

I've replaced my custom content provider with FileProvider, after reading this http://developer.android.com/reference/android/support/v4/content/FileProvider.html

So I can now see my html file is being attached in the Gmail message composer. All looks good, but when the message is received, there is no attachment!

Looking at the message source, it should be received with

Content-Type: multipart/alternative 

and an attached HTML section. But I don't see any of my attached HTML in the received message. All I get is just a single

Content-Type: text/plain

UPDATE:

I've decide to forget about using the Gmail client app. Just send the file (plus attachment) directly, without user intervention. This page has all the information you need

http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_%28no_Intents%29_in_Android

Andrew Fielden
  • 3,751
  • 3
  • 31
  • 47

2 Answers2

1

To summarise, the answer for me was to not use Intents and bypass the Gmail client app completely by sending out an email programmatically. This is my preferred way of doing it anyway. I followed the guide here

http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_%28no_Intents%29_in_Android

Andrew Fielden
  • 3,751
  • 3
  • 31
  • 47
0

This is probably because the email app cannot get it from your private space. Try putting it on external storage.

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
  • I thought that was the whole point of granting permissions in the Intent. Plus, external storage might not be available, whereas internal is always available. – Andrew Fielden Nov 24 '13 at 10:41
  • @AndrewFielden I meant as a test. You should have no need to make it world readable if you use content provider, but if you do that, instead of sending `content://` uri, just put normal file uri to skip provider phase. Also read this question: http://stackoverflow.com/questions/4179594/what-is-the-correct-permission-handling-when-sending-sensitive-app-data-as-email as it might help with other potential issue when using provider. – MaciejGórski Nov 24 '13 at 12:36
  • Ok, thanks I'll give this a go. What I find a little disappointing is the complete lack of error indication from the system. It just silently fails. – Andrew Fielden Nov 24 '13 at 13:07