Android通过字符串发送带附件的邮件

5

我有一个 HTML 字符串,想将其作为文件附加到邮件中。我可以将此字符串保存到文件并附加,但我想在不保存到文件的情况下完成此操作。我认为这应该是可能的,但我不知道如何做。以下是我的代码:

String html = "<html><body><b><bold</b><u>underline</u></body></html>";
Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(html));

// this is where I want to create attachment
intent.putExtra(Intent.EXTRA_STREAM, Html.fromHtml(html));

startActivity(Intent.createChooser(intent, "Send Email"));

如何将字符串作为文件附加到电子邮件中?


1
附件不是内联内容。根据定义,它们是文件。 - Phantômaxx
3个回答

6

这段代码可以避免您添加一个用于读取外部SD卡的权限清单。它会在您的应用程序私有目录中的文件目录中创建一个临时文件,然后使用字符串内容创建文件并授予读取权限,以便可以访问该文件。

String phoneDesc = "content string to send as attachment";

FileOutputStream fos = null;
try {
        fos = openFileOutput("tempFile", Context.MODE_WORLD_READABLE);
        fos.write(phoneDesc.getBytes(),0,phoneDesc.getBytes().length);
        fos.flush();
        fos.close();
} catch (IOException ioe) {
    ioe.printStackTrace();
}
finally {
    if (fos != null)try {fos.close();} catch (IOException ie) {ie.printStackTrace();}
}
File tempFBDataFile  = new File(getFilesDir(),"tempFile");
Intent emailClient = new Intent(Intent.ACTION_SENDTO, Uri.parse("someone@somewhere.com"));
emailClient.putExtra(Intent.EXTRA_SUBJECT, "Sample Subject";
emailClient.putExtra(Intent.EXTRA_TEXT, "Sample mail body content");
emailClient.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(tempFBDataFile));//attachment
Intent emailChooser = Intent.createChooser(emailClient, "select email client");
startActivity(emailChooser);

每当您不再需要该文件时,都应调用此函数。
File tempData = new File(getFilesDir(),"tempFile");
if (tempData.exists()) {
    tempData.delete();
}

3
它说"Context.MODE_WORLD_READABLE"由于安全问题已被弃用。 - OneWorld

-1
String pathname= Environment.getExternalStorageDirectory().getAbsolutePath();
String filename="/MyFiles/mysdfile.txt";
File file=new File(pathname, filename);
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_SUBJECT, "Title");
i.putExtra(Intent.EXTRA_TEXT, "Content");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
i.setType("text/plain");
startActivity(Intent.createChooser(i, "Your email id"));

-1
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("png/image");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {
                    "mail--id" });
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
    Uri uri = Uri.fromFile(new File(Environment
                    .getExternalStorageDirectory(), "/saved_images/MyImage.png"));
    emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
    emailIntent.setType("text/plain");
    startActivity(emailIntent);

不要忘记在清单文件中添加以下权限。

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" /

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接