How can I make email sent from android java?

55
September 11, 2019, at 11:10 AM

I have an POS app and I want to send my e-receipt to customers via email.

I have checked some resources and what I have found is how to send emails with text messages.

How can I make my email to look something like this?

Thank you for any answers that you can give.

Answer 1

I think you can send email via Intent action

     Intent it = new Intent(Intent.ACTION_SEND);
     it.putExtra(Intent.EXTRA_EMAIL, new String[]{"support@tutlane.com"});
     it.putExtra(Intent.EXTRA_SUBJECT, "Welcome to Tutlane");
     it.putExtra(Intent.EXTRA_TEXT, "Hi Guest, Welcome to Tutlane Tutorial Site");
     it.setType("message/rfc822");
  • https://www.tutlane.com/tutorial/android/android-send-email-with-examples
Answer 2

You can pass Spanned text in your extra

final Intent shareIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "The Subject");
shareIntent.putExtra(
Intent.EXTRA_TEXT,
Html.fromHtml(new StringBuilder()
    .append("<p><b>Some Content</b></p>")
    .append("<small><p>More content</p></small>")
    .toString())
);
Answer 3

You can try the following way.

      String[] recipients = {"abc@gmail.com"};
      Intent email = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
      // prompts email clients only
      email.setType("message/rfc822");
      email.putExtra(Intent.EXTRA_EMAIL, recipients);
      email.putExtra(Intent.EXTRA_SUBJECT, "email subject");
      email.putExtra(Intent.EXTRA_TEXT, "Hello, This is a test message");
      try {
        // the user can choose the email client
         startActivity(Intent.createChooser(email, "Choose an email client from..."));
      } catch (android.content.ActivityNotFoundException ex) {
         Toast.makeText(MainActivity.this, "No email client installed.",
                 Toast.LENGTH_LONG).show();
      }
   }

Android provides some further fields, these fields have to be attached to the Intent as extra data:

  • EXTRA_BCC: email addresses for blind carbon copy
  • EXTRA_CC: email addresses for carbon copy
  • EXTRA_HTML_TEXT: supply an alternative to EXTRA_TEXT as HTML formatted text
  • EXTRA_STREAM: URI holding a stream of data supplying the data that are sent
  • EXTRA_TITLE: the title that is shown when the user has to choose an email client

For a complete example, see this tutorial.

Hope it helps you, Happy Coding.

READ ALSO