0

I want to attach a text file and email it automatically in background to a predefined email address when button is pressed.

I am creating a text file like this:

   try {
    FileOutputStream fos = openFileOutput("abc.txt",MODE_PRIVATE);
    String s = "Data to be written ok fine cool";fos.write(s.getBytes());
   } catch (Exception e) {
e.printStackTrace();

}

How will I setup the sending of email automatically?

Samrat Mazumdar
  • 2,641
  • 4
  • 19
  • 28

4 Answers4

2

See this link on how to send e-mails without an intent. As mentioned in the tutorial, you will need the javamail-android libraries.

Note the addAttachment method in the Mail class.

Community
  • 1
  • 1
cklab
  • 3,761
  • 20
  • 29
  • Those methods are missing from the code sample for some reason. This was true when I used the code as well. Simply add the methods to the class and all they will need to do is set the class variable that is mentioned in the name of the method. `setFrom(String)` would set the `_form` variable, etc.. – cklab Jun 21 '12 at 17:48
  • I want to attach the "abc.txt" file I created above in the mail, but don't know what filelocation will I give – Samrat Mazumdar Jun 21 '12 at 17:55
  • You are using `openFileOutput`. Take a look at http://stackoverflow.com/questions/4926027/what-file-system-path-is-used-by-androids-context-openfileoutput – cklab Jun 21 '12 at 18:12
  • Don't know what I am doing wrong, no errors and nothing but email is not getting send http://smrtm.in/rCK1 – Samrat Mazumdar Jun 21 '12 at 18:25
0

You could spawn a new thread that does the email sending for you... Or perhaps a more elegant solution would be to spawn a second thread at the start of your application, and your main thread notifies the second thread when there is a new message to send.

steelshark
  • 644
  • 4
  • 10
0

That's depend on how would u like to send email. This link show how to send email WITHOUT using default mail client. And this to let user choose

private void sendEmail() {
        final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.setType("plain/text");
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, String[]{"recipient@example.com"} );
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "content");
        this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
    }
Community
  • 1
  • 1
Hein
  • 2,675
  • 22
  • 32
0

To send the mail in background you can use AsyncTask and to make it automatic(without using any Intent) use JavaMail api.

Community
  • 1
  • 1
Eight
  • 4,194
  • 5
  • 30
  • 51