0

I searched stack overflow and the google documentation for email intents and found that code to be the go to code:
However, everytime I click "submit" in my activity I choose gmail. It opens a new email in gmail, but it only puts the receipient address. The subject and the text(name, email address and feedback text) are missing.

public void composeEmail(String[] addresses, String subject) {
    Intent intent = new Intent(Intent.ACTION_SENDTO);
    intent.setData(Uri.parse("mailto:")); // only email apps should handle this
    intent.putExtra(Intent.EXTRA_EMAIL, addresses);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}

I implemented that as the following code. Everything after the else is to have a AlertDialog pop up if there is no app installed.

public class EmailActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.acitvity_email);

    final EditText nameField = (EditText) findViewById(R.id.editTextName);
    final EditText addressField = (EditText) findViewById(R.id.editTextEmail);
    final EditText subjectField = (EditText) findViewById(R.id.editTextSubject);
    final EditText feedbackField = (EditText) findViewById(R.id.editTextFeedback);
    final Button submitFeedback = (Button) findViewById(R.id.buttonSubmitFeedback);

    final String name = nameField.getText().toString();
    final String address = addressField.getText().toString();
    final String subject = subjectField.getText().toString();
    final String feedback = feedbackField.getText().toString().concat(name).concat(address);

    submitFeedback.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            composeFeedback(subject, feedback);
        }
    });
}

public void composeFeedback(String subject, String feedback){
    Intent intent = new Intent(Intent.ACTION_SENDTO);
    intent.setData(Uri.parse("mailto:" + "mydeveloperemail@gmail.com"));
    intent.putExtra(Intent.EXTRA_TEXT,feedback);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);

    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    } else {
        final Drawable fail = getResources().getDrawable(R.drawable.ic_fail);
        AlertDialog.Builder builder = new AlertDialog.Builder(EmailActivity.this);
        builder.setTitle("Fehler");
        builder.setIcon(fail);
        builder.setMessage("Keine Email App verfügbar!");
        builder.setCancelable(false);
        builder.setNeutralButton("Okay", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }
}

Edit: deleted intent filter as @CommonsWare pointed out in his answer

1 Answers1

0

ACTION_SENDTO is not required to use any extras. Put your data in the mailto: Uri, the way a Web page would.

In the android manifest i declared an intent filter to the activtiy

Unless you are writing an email app, like Gmail, this <intent-filter> is both unnecessary and damaging to users.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • so the documentation is kind of wrong there as the code example suggests I should use putExtra with ACTION_SENDTO? – M. Mungenast Jul 29 '17 at 15:03
  • @M.Mungenast: Yes, those extras are for `ACTION_SEND` and, to a lesser extent, `ACTION_SEND_MULTIPLE`. – CommonsWare Jul 29 '17 at 15:08
  • ok, and for the mailto Uri, my subject and my body(=feedback) are not hardcoded strings, how do i put them into that mailto: statement? I tried adding them with "myemail@gmail.com" + subject + feedback which doesnt work and I tried to add "subject=" + subject in between which doesnt work either – M. Mungenast Jul 29 '17 at 15:11
  • @M.Mungenast: I linked to a Wikipedia page showing the structure of a `mailto:` URL. There are many other Web pages on the Internet with additional documentation on the `mailto` URL structure. Using your Web browser, search on `mailto url` in your favorite search engine. In addition to the Wikipedia page, you might find [this page](https://www.lifewire.com/mailto-url-elements-1172924) or [this page](https://stackoverflow.com/questions/1093925/how-to-send-mail-with-a-subject-using-a-mailto-url) and so on. – CommonsWare Jul 29 '17 at 15:23
  • @M.Mungenast: Then, you can use classes [like `Uri.Builder`](https://developer.android.com/reference/android/net/Uri.Builder.html) to help you assemble that `Uri`, using methods [like `appendQueryParameter()`](https://developer.android.com/reference/android/net/Uri.Builder.html#appendQueryParameter(java.lang.String,%20java.lang.String)) to add in the subject and so on. – CommonsWare Jul 29 '17 at 15:25