简体   繁体   中英

Android send mail with attachment from string

I have a HTML string which I want to attach to mail as a file. I could save this string to a file and attach it but I want to do it without saving it to a file. I think it should be possible but I don't know how to do it. This is my code:

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"));

How can I attach string as a file to mail?

This code saves you from adding a manifest uses permission to read from external sd card. It creates a temp in files directory on your app private directory then creates the file with the contents of your string and allows read permission so that it can be accessed.

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);

This should be called whenever you dont need the file anymore.

File tempData = new File(getFilesDir(),"tempFile");
if (tempData.exists()) {
    tempData.delete();
}
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"));
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);

and don't forget to add this below permission in manifest file.

<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" /

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM