3

I'm having an issue with my application. The purpose of the app is to collect data and submit the data form using javamail to a specified email address. when the user clicks the button, they should receive a prompt to select an email client, however I am not getting this prompt when I test it. Can someone tell me what I am missing?

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class SubmitForm extends Activity implements View.OnClickListener {
private Intent emailIntent;
EditText personsFirstName, personsLastName, personsEmail, personsPhone, comments, vehicleModel;
Spinner vehicleYear, vehicleMake, serviceTime, serviceNeeded;
TextView serviceDate;
String fname, lname, emailAdd, phoneNumber, vehicleYears, vehicleModeltrim, vehicleManu, serviceAppointment, serviceAppointmentTime, serviceTypeNeeded, extraComments;
Button sendEmail;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.service);
    initializeVars();
    sendEmail.setOnClickListener(this);
}
private void initializeVars() {
    // TODO Auto-generated method stub
    personsFirstName = (EditText) findViewById(R.id.firstName);
    personsLastName = (EditText) findViewById(R.id.lastName);
    personsEmail = (EditText) findViewById(R.id.Email);
    personsPhone = (EditText) findViewById(R.id.PhoneNumber);
    vehicleYear = (Spinner) findViewById(R.id.sYear);
    vehicleMake = (Spinner) findViewById(R.id.sMake);
    vehicleModel = (EditText) findViewById(R.id.Model);
    serviceDate = (TextView) findViewById(R.id.Model);
    serviceTime = (Spinner) findViewById(R.id.sTime);
    serviceNeeded = (Spinner) findViewById(R.id.sNeeded);
    comments = (EditText) findViewById(R.id.eComments);
    sendEmail = (Button) findViewById(R.id.bSubmit);
}
public void onClick(View v) {
    // TODO Auto-generated method stub
    convertToString();
    String emailaddress[] = { emailAdd };
    String message = "Please Review the following"
            + '\n' + " "
            + '\n' + "First Name: " + fname
            + '\n' + "Last Name: " + lname
            + '\n' + "Email :" + emailAdd
            + '\n' + "Phone Number: " + phoneNumber
            + '\n' + "Vechile Year: " + vehicleYears
            + '\n' + "Vehicle Make: " + vehicleManu
            + '\n' + "Vehicle Model: " + vehicleModeltrim
            + '\n' + "Requested Service Date: " + serviceAppointment
            + '\n' + "Requested Service Time: " + serviceAppointmentTime
            + '\n' + "Service Needed: " + serviceTypeNeeded
            + '\n' + "Comments: " + extraComments;

    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("plain/text");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {"test@someemail.com"});

    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress);
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Schedule Service Request");

    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
    startActivity(Intent.createChooser(emailIntent, "Please select Email Client"));
}
private void convertToString() {
    // TODO Auto-generated method stub

    fname = personsFirstName.getText().toString();
    lname = personsLastName.getText().toString();
    emailAdd = personsEmail.getText().toString();
    phoneNumber = personsPhone.getText().toString();

    //vehicleYears = vehicleYear.getText().toString();
    vehicleYears = (String) vehicleYear.getAdapter().getItem(RESULT_OK);


    //vehicleManu = vehicleMake.getText().toString();
    vehicleManu = (String) vehicleMake.getAdapter().getItem(RESULT_OK);

    vehicleModeltrim = vehicleModel.getText().toString();
    serviceAppointment = serviceDate.getText().toString();
    serviceAppointmentTime = (String) serviceTime.getAdapter().getItem(RESULT_OK);


    //serviceTypeNeeded = serviceNeeded.getText().toString();
    serviceTypeNeeded = (String) serviceNeeded.getAdapter().getItem(RESULT_OK);

    extraComments = comments.getText().toString();
}
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}
}
MADPADGE
  • 113
  • 2
  • 12
  • hi this is imran,what about these two lines:`emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {"test@someemail.com"}); emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress);` – ρяσѕρєя K Mar 13 '12 at 18:25
  • or replace this ` emailIntent.setType("plain/text")` with `emailIntent.setType("text/plain")` – ρяσѕρєя K Mar 13 '12 at 18:28
  • refer this link this may help you http://www.vidyut.com/sunit/android/android_sendmail.html – Aerrow Mar 13 '12 at 18:28
  • Check this **Kotlin** answer: [If you need to show only email apps and then you want to open only inbox (not open new email writing), you just need to use below function.](https://stackoverflow.com/a/70441024/8471798) – canerkaseler Dec 21 '21 at 20:44

2 Answers2

13

if u want only to choose among email client install on device you can make use of ACTION_SENTTO option

String mailTo="email@gmail.com";
Intent email_intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",mailTo, null)); 
email_intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject text here"); 
email_intent.putExtra(android.content.Intent.EXTRA_TEXT,"Body text here"); 

startActivity(Intent.createChooser(email_intent, "Send email..."));

try this email intent on real device...emulator might not give you the result..

good luck..

Kri
  • 1,846
  • 2
  • 13
  • 17
  • Thanks, I tried this on my device, and it still wont display the email chooser; very strange. Should this intent be triggered in the manifest? – MADPADGE Mar 14 '12 at 14:36
  • one suggestion make sure that u have more then one email client installed on your real device... – Kri Mar 15 '12 at 18:39
  • 1
    u can also try to setType(message/rfc822); might be it will show only mail client installed on your device... – Kri Mar 15 '12 at 19:02
  • Thanks, I figured out what the issue was. "getAdapter" was stopping it. Once I commented it out, and removed the variables, it worked perfectly. Thanks for your help – MADPADGE Mar 21 '12 at 22:06
0

just for the record:

Some e-mail apps, such as Gmail, expect a String[] for extras like EXTRA_EMAIL and EXTRA_CC, use putExtra(String, String[]) to add these to your intent.

Android developer page

val sendIntent: Intent = Intent().apply {
            action = Intent.ACTION_SEND
            putExtra(Intent.EXTRA_EMAIL, arrayOf("a@bc.de"))
            putExtra(Intent.EXTRA_SUBJECT, "subject")
            putExtra(Intent.EXTRA_TEXT, "content")
            type = "text/plain"
}

startActivity(Intent.createChooser(sendIntent, "Send mail"))
longi
  • 11,104
  • 10
  • 55
  • 89