Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android sending email with attachments doesn't always work

I am sending an email with attachments using a ContentProvider.

  1. Firstly, I write the files into the cache dir.
  2. Then I create the email with a url for each file to be found in by the content provider
  3. Then I start a new activity with the ACTION_SEND_MULTIPLE intent.
  4. I choose gmail and then hit the send button.

This sometimes works, it seems to work the first time in a while, but not working after subsequent tries... but it is not always like that.

When it doesn't work the email is stuck in sending in gmail. This happens on 2.3.3 and 4.0.1, opening the mail in the gmail client and hitting send button so often causes the message to be delivered almost instantly, but not every time.

Opening the Intent with Google Drive has the same behaviour as gmail.

Opening the Intent with the built in exchange mail client always works so far.

Here is the code for sending the email:

            Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
            sendIntent.putExtra(Intent.EXTRA_EMAIL, exportParams.emailAddresses);
            sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Leader Activity Report");
            sendIntent.putExtra(Intent.EXTRA_TEXT, "Leader Activity Report, see attached file.");
            Uri fileUri = CachedFileProvider.createFileUri(result.fileName);
            if (L.dbg())
                L.dbg("Using uri:" + fileUri.toString());
            ArrayList<Uri> uris = new ArrayList<Uri>();
            uris.add(fileUri);
            Uri fileUri2 = CachedFileProvider.createFileUri(result.fileNameDayByDay);
            uris.add(fileUri2);
            if (L.dbg())
                L.dbg("Using uri2:" + fileUri2.toString());
            sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
            sendIntent.setType("text/plain");
            parent.startActivity(sendIntent);

Here is the Content Provider

public class CachedFileProvider extends ContentProvider {

private static final String CLASS_NAME = "CachedFileProvider";
public static final String AUTHORITY = "com.josh.lll.file.provider";

private UriMatcher uriMatcher;

@Override
public boolean onCreate() {
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI(AUTHORITY, "*", 1);
    return true;
}


@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
        throws FileNotFoundException {
    try {
        String LOG_TAG = CLASS_NAME + " - openFile";
        Log.v(LOG_TAG,
                "Called with uri: '" + uri + "' - " + uri.getLastPathSegment());
        switch (uriMatcher.match(uri)) {
        case 1:
            String fileLocation = getContext().getCacheDir() + File.separator
                    + uri.getLastPathSegment();
            Log.i(CLASS_NAME,"Returning file :"+fileLocation);
            ParcelFileDescriptor pfd = ParcelFileDescriptor.open(new File(
                    fileLocation), ParcelFileDescriptor.MODE_READ_ONLY);
            return pfd;
        default:
            Log.v(LOG_TAG, "Unsupported uri: '" + uri + "'.");
            throw new FileNotFoundException("Unsupported uri: "
                    + uri.toString());
        }
    } catch (FileNotFoundException t) {
        Bug.major(this, t, "Could not return file descriptor");
        throw t;
    } catch (RuntimeException t) {
        Bug.major(this, t, "Could not return file descriptor");
        throw t;
    } catch (Error t) {
        Bug.major(this, t, "Could not return file descriptor");
        throw t;
    }
}

public static String createFullyQualifiedFileName(Context c, String fileNamePart) {
    File cacheDir = c.getCacheDir();
    Log.i(CLASS_NAME,"Using cache dir:"+cacheDir);
    return cacheDir + File.separator + fileNamePart;
}

public static Uri createFileUri(String fileNamePart) {
    return Uri.parse("content://" + AUTHORITY + "/"+ fileNamePart);
}

public int update(Uri uri, ContentValues contentvalues, String s,
        String[] as) {
    return 0;
}

@Override
public int delete(Uri uri, String s, String[] as) {
    return 0;
}

@Override
public Uri insert(Uri uri, ContentValues contentvalues) {
    return null;
}

@Override
public String getType(Uri uri) {
    return null;
}

@Override
public Cursor query(Uri uri, String[] projection, String s, String[] as1,
        String s1) {
    return null;
}

}

For both successful and 'stalled' email sends the following log message is printed by gmail:

04-03 22:17:35.027: I/Gmail(13206): >>>>> Attachment uri: content://com.josh.lll.file.provider/report_20100401_20130402_LeadetJosh_3_1364980653516.csv
04-03 22:17:35.035: I/Gmail(13206): >>>>>           type: text/plain
04-03 22:17:35.035: I/Gmail(13206): >>>>>           size: 0
04-03 22:17:35.054: I/Gmail(13206): >>>>> Attachment uri:  content://com.josh.lll.file.provider/backup_20100401_20130402_LeadetJosh_3_1364980653516_day_by_day.lll
04-03 22:17:35.054: I/Gmail(13206): >>>>>           type: text/plain
04-03 22:17:35.054: I/Gmail(13206): >>>>>           size: 0
like image 508
placebojim Avatar asked Apr 03 '13 08:04

placebojim


People also ask

Why are my emails with attachments not going through?

The most common reason that an attachment won't send is that it is too big. These limits are set by whoever you use for email, whether it's an email account through your ISP or through an online provider like Yahoo or GMail. You should check with your email service provider to see what the limits are for attachments.

Why is my Gmail not sending attachments Android?

Clear Browser Cache and Cookies Some temporary files stored in the Cache may be keeping your email from getting sent. Simply go to Settings>Privacy and Security and clear the cache and cookies. This is one of the best ways to fix the 'Can't Send Emails With Attachments From Gmail Account' issue.

Why is my Gmail not sending emails with attachments?

Disable the Web Browser Proxy. If you've set up a web browser proxy, this might be causing the issue with Gmail can't send emails with attachments. So you should try disabling the proxy server.

How do I change the attachment settings in Gmail?

Click on G-Suite. Scroll down to find Gmail and click on it. Scroll down to Advanced settings and click on it. Scroll down to Attachment compliance and hover on it and to see configure option and click on it.


1 Answers

it happens for files stored in system folders like /data/app or /system.

Workaround for this is: Copy those files into sdcard location and attach/use them from there.

like image 188
user3361318 Avatar answered Sep 27 '22 19:09

user3361318