How to send the text in an edit text to my Email by pressing a button?

1

I have 5 'edit texts' and a button, how do I make the information I write in the EditText by pressing the 'Button' sent to my email?

public class MainActivity extends AppCompatActivity {

    EditText txtNombre,txtCip,txtDNI,txtCel,txtEmail;
    Button btnIniciar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtNombre=(EditText)findViewById(R.id.txtNombre);
        txtCip=(EditText)findViewById(R.id.txtCip);
        txtDNI=(EditText)findViewById(R.id.txtDNI);
        txtCel=(EditText)findViewById(R.id.txtCel);
        txtEmail=(EditText)findViewById(R.id.txtEmail);

        btnIniciar=(Button)findViewById(R.id.btnIniciar);

        btnIniciar.setOnClickListener(new View.OnClickListener() {
            @Override



        });


    }
}
    
asked by antonio garcia 04.07.2017 в 02:51
source

3 answers

0

First you need to define the method onClick() of the listener and inside you add the necessary code to send the text contained within the EditText (by the way you do not define which EditText ), as an example the EditText txtNombre :

 btnIniciar=(Button)findViewById(R.id.btnIniciar);

        btnIniciar.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
               enviarEmail(txtNombre.getText().toString());
            } 

        });

This would be a method to send email:

public void  enviarEmail(String mensaje){

  Intent emailIntent = new Intent(Intent.ACTION_SEND);

  emailIntent.setData(Uri.parse("mailto:"));
  emailIntent.setType("text/plain");
  emailIntent.putExtra(Intent.EXTRA_EMAIL, "[email protected]"); // * configurar email aquí!
  emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Tu Asunto...");
  emailIntent.putExtra(Intent.EXTRA_TEXT, mensaje); 

  try {
     startActivity(Intent.createChooser(emailIntent, "Enviar email."));        
     Log.i("EMAIL", "Enviando email...");
  }
  catch (android.content.ActivityNotFoundException e) {
     Toast.makeText(this, "NO existe ningún cliente de email instalado!.", Toast.LENGTH_SHORT).show();
  }

}
    
answered by 23.07.2017 / 05:56
source
0

You can use a method like this:

public void enviarEmail(View view)
{
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, "Asunto de prieba");
    intent.putExtra(Intent.EXTRA_TEXT, "Probando el envio");
    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
    startActivity(intent);
}

If I remember correctly, it will open a new window where you specify some things.

    
answered by 04.07.2017 в 03:45
0

If you do not want a new window to open and send the mail directly when you click on the button, you can use a library that does it directly. I usually use Javamail. I leave a link for your use

link

I hope you're worth

    
answered by 05.07.2017 в 17:49