Automatic send mail on button click in android

To send an email automatically when a button is clicked in Android, you can use the Intent class to launch the default email app on the device. Here's an example code snippet that demonstrates how to do this:

import android.content.Intent;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button sendEmailButton;

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

        sendEmailButton = findViewById(R.id.send_email_button);

        sendEmailButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendEmail();
            }
        });
    }

    private void sendEmail() {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("message/rfc822");
        intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
        intent.putExtra(Intent.EXTRA_SUBJECT, "Subject of the email");
        intent.putExtra(Intent.EXTRA_TEXT, "Body of the email");

        try {
            startActivity(Intent.createChooser(intent, "Send email"));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }
    }
}

In this example, we create an Intent with the ACTION_SEND action and set the type to "message/rfc822" to indicate that we want to send an email. We then set the EXTRA_EMAIL extra to specify the recipient's email address, EXTRA_SUBJECT to set the subject of the email, and EXTRA_TEXT to set the body of the email.

When the button is clicked, the sendEmail() method is called, which creates the Intent and starts the email app using startActivity().

Note that this code will launch the default email app on the device, and the user will be prompted to select an email account to send the email from. If the user has multiple email accounts set up on their device, they will be able to choose which account to use.

Also, keep in mind that this code will not send the email automatically, but rather launch the email app with the email composed and ready to be sent. The user will still need to click the "Send" button to actually send the email.

If you want to send the email automatically without prompting the user, you can use the JavaMail library, which is a popular library for sending emails in Android. However, this requires more complex setup and configuration.