0

I'm trying to let my app send a new password to ones email but it opens a page and gives the message "No apps can perform this action" with the title "Send email" (which I've have I know), why? The idea with a new password is what I want, but first I want to send anything to the typed email in the edittext to test it. I've tried to run it on a real device.

public class Glemtpassword extends AppCompatActivity implements View.OnClickListener{

Button nypassword;
EditText email;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_glemtpassword);
    nypassword = (Button) findViewById(R.id.nypassword);
    nypassword.setOnClickListener(this);
    email = (EditText) findViewById(R.id.email);
}

 @Override
public void onClick(View view) {
    if (view == nypassword){
        sendEmail();
    }
}

protected void sendEmail() {
    Log.i("Send email", "");
    Intent emailIntent = new Intent(Intent.ACTION_SEND);

    emailIntent.putExtra(Intent.EXTRA_EMAIL, String.valueOf(email));

    try {
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        finish();
        Log.i("Nyt password er sendt til din mail...", "");
    }
    catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(Glemtpassword.this, "Ingen email klient", Toast.LENGTH_SHORT).show();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.Forside) {
        Intent intent = new Intent(this, Forside.class);
        startActivity(intent);
    }
    else if (id == R.id.Logind){
        Intent intent = new Intent(this, LogInd.class);
        startActivity(intent);
    }
    else if (id==R.id.Opretbruger) {
        Intent intent = new Intent(this, OpretBruger.class);
        startActivity(intent);
    }
    return true;
 }
}

EDIT:

public class Glemtpassword extends AppCompatActivity implements View.OnClickListener{

Button nypassword;
EditText email;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_glemtpassword);
    nypassword = (Button) findViewById(R.id.nypassword);
    nypassword.setOnClickListener(this);
    email = (EditText) findViewById(R.id.email);
}

 @Override
public void onClick(View view) {
    if (view == nypassword){
        sendEmail();
    }
}

protected void sendEmail() {
    Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
            "mailto", email.getText().toString(), null));
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
    emailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
    startActivity(Intent.createChooser(emailIntent, "Send email..."));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.Forside) {
        Intent intent = new Intent(this, Forside.class);
        startActivity(intent);
    }
    else if (id == R.id.Logind){
        Intent intent = new Intent(this, LogInd.class);
        startActivity(intent);
    }
    else if (id==R.id.Opretbruger) {
        Intent intent = new Intent(this, OpretBruger.class);
        startActivity(intent);
    }
    return true;
 }
}
Hudhud
  • 61
  • 1
  • 10

2 Answers2

0

You did not set a MIME type, and you have not provided any content. Call putExtra() to set a value for EXTRA_TEXT on the Intent, with whatever you want to be appearing in the body of the email. Then, call setType() on the Intent to indicate the MIME type of the data that you are placing in EXTRA_TEXT.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Have edited my code and now it gives the opportunity to choose which app I wanna send from. My question is, can't it send directly from the app? I wanna send this message "123" – Hudhud Dec 04 '15 at 16:54
  • @Hudhud: If you use `ACTION_SEND` or `ACTION_SENDTO`, the user gets to choose their email client (as there could be several of them) and the user can elect not to send the email. It is the user's device and the user's email account, after all. You are welcome to send email directly using third-party libraries like JavaMail. However, you will need an email account, with its password. Users will not want to give you their email passwords, and using your own means that your email account will be taken over rapidly by somebody who grabs the password out of the app. – CommonsWare Dec 04 '15 at 17:05
  • Ok, then what's the easiest and securest method to send an email with the message "123" ? – Hudhud Dec 04 '15 at 17:14
  • @Hudhud: You have already implemented that, via `ACTION_SEND`/`ACTION_SENDTO`. – CommonsWare Dec 04 '15 at 17:17
  • Ok, but as I mentioned before currently it uses my own outlook account to send emails. So if I write 123@hotmail.com in the edittext it opens my Outlook and gives me the opportunity to send to 123@hotmail.com, but I want it to automatically send the message "123" from my account. – Hudhud Dec 04 '15 at 17:21
  • @Hudhud: Then don't send an email from the device. Either send it from some Web server that you control (triggering it via an HTTP request from the device), or use some other communications channel. – CommonsWare Dec 04 '15 at 17:27
-1

As per this answer

The solution should be:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
        "mailto","abc@gmail.com", null)); 
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body"); 
startActivity(Intent.createChooser(emailIntent, "Send email..."));

or as stated in the Android documentation you can use:

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);
Community
  • 1
  • 1
fedestylah
  • 169
  • 1
  • 10
  • Have uploaded the edited version of my code. Now it wants me to open an application, can't it just send directly? – Hudhud Dec 04 '15 at 16:35