如何通过电子邮件发送更大的数据

时间:2013-09-02 15:44:08

标签: java android android-intent

我正在使用This解决方案。

i.putExtra(Intent.EXTRA_TEXT , "body of email");,它按预期工作。如果"body of email"更改为长度为711098的字符串,则不会:电子邮件客户端选择器。

任何想法,解决方案?

2 个答案:

答案 0 :(得分:2)

操作中使用的Intent(例如startActivity())限制为~1MB。

  

如何克服它?

发送较短的电子邮件。

或者,使用EXTRA_STREAM将长文本作为附件发送。

或者,使用JavaMail发送电子邮件。

或者,通过将711098字节发送到您运行的代表您的应用发送电子邮件的Web服务来发送电子邮件。

答案 1 :(得分:0)

以下是建议CommonsWare

的实现

临时文件已保存到“下载”文件夹中,因此可供所有应用访问。别忘了删除它!

在开发时,USB电缆已插入您的设备,您正在观看logcat。现在拔出usb线,否则你将获得Permission denied异常!

data.txt它将在Gmail客户端界面上显示,但如果您忘记拔出电缆并让Android操作系统访问其下载文件夹,则不会发送。

public void sendEmail(String emailBody, String emailAddrressTo) {

        boolean bodyToLong = (emailBody != null && emailBody.length() > 300000);

        final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { emailAddrressTo });
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "data");
        if (!bodyToLong) {
            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailBody);
        } else {// data file to big:

            String tmpFileName = "data.txt";
            File dirDownloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            File fileOut = new File(dirDownloads, tmpFileName);

            FileOutputStream fos;
            try {
                fileOut.createNewFile();
                fos = new FileOutputStream(fileOut);

                FileDescriptor fd = fos.getFD();
                BufferedWriter bw = new BufferedWriter(new FileWriter(fd));
                bw.write(emailBody);

                fd.sync();
                bw.close();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                String msg = e.getMessage();
                if (msg.contains("(Permission denied)")) {
                    Toast.makeText(activity, "PULL THE USB CABLE OUT FROM PHONE!!! Out You have forgot to add android.permission.WRITE_EXTERNAL_STORAGE  permission to AndroidManifest.xml", Toast.LENGTH_SHORT).show();
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This message has to long data. Please see the attachment!");

            Uri uri = Uri.fromFile(fileOut);
            emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, uri);
        }
        emailIntent.setType("message/rfc822");

        Intent intentToStart = Intent.createChooser(emailIntent, "Send mail...");

        activity.startActivity(intentToStart);
    }