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