4

I try to make a newline in email body, it don't work either "\n" or System.getProperty("line.separator"), how can I do? Thanks!

        Intent emailIntent=new Intent(Intent.ACTION_SEND);         

        String subject = "Your sms sent by email";
        String body = "aa"+"\n"+"bb"+System.getProperty("line.separator")+"cc" ;

        String[] extra = new String[]{"aa@gmail.com"};
        emailIntent.putExtra(Intent.EXTRA_EMAIL, extra);

        emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
        emailIntent.putExtra(Intent.EXTRA_TEXT, body);
        emailIntent.setType("message/rfc822");

        startActivity(emailIntent);
  • @PankajKumar the question you suggest as duplicate offers solution already considered by Paul. – njzk2 May 28 '13 at 08:09
  • You need to send your email using email Intent then you be able to insert new lines using html context: Check out this [answer](http://stackoverflow.com/questions/2007540/how-to-send-html-email) – Ilya Gazman May 28 '13 at 08:08

2 Answers2

5

If your e-mail is in HTML, try <br/>

String body = "aa"+"<br/>"+"bb"+"<br/>"+"cc";

or

String body = "aa<br/>bb<br/>cc";
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
  • which it is not, since setType("message/rfc822"). – njzk2 May 28 '13 at 08:08
  • This RFC specifies how to format the e-mail but does it force the content to be in plain text rather than HTML? (Open question, I absolutely don't know). IMHO, the body of the e-mail (plain text/HTML) is independent from the email format itself. – Arnaud Denoyelle May 28 '13 at 08:13
3

This is my solution to send an email :

public static void Send(Activity activity, String subject, String message, String to, String cc, String bcc, File attachedFile, String typeAttached) {
    Intent email = new Intent(Intent.ACTION_SEND);
    email.setType("message/rfc822");
    email.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
    email.putExtra(Intent.EXTRA_SUBJECT, subject);
    email.putExtra(Intent.EXTRA_TEXT, message);

    if(attachedFile!=null && attachedFile.exists() && !typeAttached.isEmpty()) {
        email.setType(typeAttached);
        email.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachedFile));
    }

    if(!cc.isEmpty())
        email.putExtra(Intent.EXTRA_CC, new String[]{cc}); 
    if(!bcc.isEmpty())
        email.putExtra(Intent.EXTRA_BCC, new String[]{bcc}); 

    try {
        activity.startActivity(Intent.createChooser(email, "Please select your mail client:"));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(activity, "There is no email client installed", Toast.LENGTH_SHORT).show();
    }
}

And here is a sample message:

String message = "Hello,<br/><br/>this is a test message!";

I hope I have helped you!

lopez.mikhael
  • 9,943
  • 19
  • 67
  • 110