How to Make a Button Submit Multiple Textviews to my Email?

Question

I looked it up, and this is the most common way to send an email...

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT   , "body of email");
try {
    startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(MyActivity.this, "There are no email clients
installed.",     
Toast.LENGTH_SHORT).show();
}

I am confused as to what this is actually sending, and how to make this happen when the user clicks a button. Do I put the recipient email (me) like this?

 i.putExtra(Intent.EXTRA_EMAIL  , "myemail@gmail.com");

Then the same format for subject and message/body of the email?

Is this how I would put the user's input into the body of the email? (The user inputs multiple editText boxes)

 editText userTitle = (editText)findViewById(R.id.idOfTheEditTextBox);
 editText userDescription = (editText)findViewById(R.id.idOfTheEditTextBox);

Then input it like this?

 i.putExtra(Intent.EXTRA_TEXT   , "userTitle", "userDescription");

Finally, what does all that toast and no email clients installed mean? I am new to android app development, and am making an app on Android Studio! All help is very much appreciated!! Thanks!


Show source
| java   | android   | xml   | android-studio   | android-intent   2017-01-02 07:01 2 Answers

Answers to How to Make a Button Submit Multiple Textviews to my Email? ( 2 )

  1. 2017-01-02 07:01

    Firstly "Toast"

    Toast code is written to give alert to user, notifying that no email client is available in users phone (ex. gmail, etc)

    Now the main portion,

    i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
    

    Yes this above line add the recipents email address.

    So to achive this kind of thing, you need to do following things,

    1. Create layout with one Edittext (where user can put recepeints email address which you can pass to Intent). And one button on click of which you will launch Intent.
    2. Now then the button is clicked write this code:-

    public class MainActivity extends AppCompatActivity {
      EditText etRecipentId, etSubject, etBody;
      Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etRecipentId = (EditText) findViewById(R.id.email_id);
        etSubject = (EditText) findViewById(R.id.et_subject);
        etBody = (EditText) findViewById(R.id.et_body);
      }
    
      b1.setOnClickListener(new OnClickListener() {
        public void onClick() {
          Intent i = new Intent(Intent.ACTION_SEND);
          i.setType("message/rfc822");
          i.putExtra(Intent.EXTRA_EMAIL, new String[] {
            etRecipentId.getText().toString();
          });
          i.putExtra(Intent.EXTRA_SUBJECT, etSubject.getText().toString(););
          i.putExtra(Intent.EXTRA_TEXT, etBody.getText().toString(););
          try {
            startActivity(Intent.createChooser(i, "Send mail..."));
          } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(MyActivity.this, "There are no email clients
        installed.",
              Toast.LENGTH_SHORT).show();
          }
        }
      });

  2. 2017-01-02 07:01

    instead of i.putExtra(Intent.EXTRA_TEXT , "userTitle", "userDescription"); use i.putExtra(Intent.EXTRA_TEXT , userTitle+" "+userDescription);

    And as you are getting exception ,check that is there any email client like gmail,etc is installed in your phone or not.

Leave a reply to - How to Make a Button Submit Multiple Textviews to my Email?

◀ Go back