0

When my users want to email me, I want to add an option where they can send me logs from the app. I would like to save some text in a text file and then send that text file as an attachment to the email the user is going to send to me.

I have tried the following two approaches.

Approach #1:

Calling the two functions below as follows:

            copyFileToExternal("test_file_name" + ".xml");
            sendEmail("nilashis@gmail.com", "test_file_name");

Below are the functions:

private  File copyFileToExternal(String fileName) {
    File file = null;
    String newPath = Environment.getExternalStorageDirectory()+"/folderName/";
    try {
        File f = new File(newPath);
        f.mkdirs();
        FileInputStream fin = openFileInput(fileName);
        FileOutputStream fos = new FileOutputStream(newPath + fileName);
        //byte[] buffer = new byte[1024];
        byte[] buffer = "safdsdfsdfsdfsdfdsf".getBytes();
        int len1 = 0;
        while ((len1 = fin.read(buffer)) != -1) {
            fos.write(buffer, 0, len1);
        }
        fin.close();
        fos.close();
        file = new File(newPath + fileName);
        if (file.exists())
            return file;
    } catch (Exception e) {

    }
    return null;
}

//Method to email
private void sendEmail(String email, String fileName) {

    File file = new File(Environment.getExternalStorageState()+"/folderName/" + fileName+ ".xml");
    Uri path = Uri.fromFile(file);
    Intent intent = new Intent(android.content.Intent.ACTION_SEND);
    intent.setType("application/octet-stream");
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "This is the subject I want");
    String to[] = { email };
    intent.putExtra(Intent.EXTRA_EMAIL, to);
    intent.putExtra(Intent.EXTRA_TEXT, "here is the message I want");
    intent.putExtra(Intent.EXTRA_STREAM, path);
    startActivityForResult(Intent.createChooser(intent, "Send mail..."),
            1222);
}

Approach #2:

Does not work:

public void doSendFile() {
    String xmlFilename = "fileToSend.txt";
    FileOutputStream fos = null;
    try {
        fos = openFileOutput(xmlFilename, MODE_WORLD_READABLE);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        fos.write("this is test being written to ".getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Intent intent = new Intent(android.content.Intent.ACTION_SEND);
    intent.setType("text/plain");

//        Uri uri = Uri.fromFile(new File(xmlFilename));
    Uri uri = Uri.fromFile(new File("/mnt/sdcard/../.."+getFilesDir()+"/"+xmlFilename));
    intent.putExtra(android.content.Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(intent, "Send eMail..asdasd"));

}
user1406716
  • 9,092
  • 22
  • 91
  • 149

3 Answers3

0

I think you can get something from here. Sending Email in Android using JavaMail API without using the default/built-in app
http://techblogon.com/send-email-from-an-android-application-programmatically/

And add this in your manifest.

<uses-permission android:name="android.permission.INTERNET"/>
Community
  • 1
  • 1
Chaudhary Amar
  • 773
  • 7
  • 20
0

Below is the sample code:

private void share(String strEmail){
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("text/plain");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{strEmail});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Log");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "From My App");
    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///mnt/sdcard/files/log.txt"));
    startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}

where URI string must be the path to your log file.

Update: Other things you have to check 1. Internet Permission in your manifest file 2. If you were able to open mail and not seeing the attachment, then there may be problem with URI path.

Sumighosh Charuvil
  • 446
  • 1
  • 4
  • 14
0

Here is a sample code. uris is the path of the file that you want to share in the mail.

Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    shareIntent.setType("application/pdf");
    shareIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "sender_mail_id" });
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");                               
    shareIntent.putExtra(Intent.EXTRA_TEXT, "Text to be displayed as the content");
    shareIntent.putExtra(Intent.EXTRA_STREAM, uris);
    startActivity(shareIntent);
gian1200
  • 3,567
  • 2
  • 27
  • 57
Anusha Harish
  • 376
  • 3
  • 14