How to send a mail in android studio

To send an email from an Android app using Android Studio, you can use the Intent class to launch the default email client on the device. Here's a step-by-step guide:

Step 1: Create a new Android project

Open Android Studio and create a new project. Choose "Empty Activity" as the project template and name your project (e.g., "EmailSender").

Step 2: Add the necessary permissions

In the AndroidManifest.xml file, add the following permission:

<uses-permission android:name="android.permission.SEND_SMS" />

This permission is required to send emails.

Step 3: Create a button to send the email

In the activity_main.xml file, add a button to send the email:

<Button
    android:id="@+id/send_email_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Send Email" />

Step 4: Write the code to send the email

In the MainActivity.java file, add the following code to send the email:

import android.content.Intent;
import android.os.Bundle;
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) {
                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 code, we create an Intent with the ACTION_SEND action and set the type to message/rfc822, which is the MIME type for email. We then set the recipient's email address, subject, and body using the EXTRA_EMAIL, EXTRA_SUBJECT, and EXTRA_TEXT extras.

Step 5: Run the app

Run the app on an emulator or a physical device. When you click the "Send Email" button, the default email client on the device will be launched with the email composed.

Note: This code will launch the default email client on the device, which may not be the Gmail app. If you want to use the Gmail app specifically, you can use the Intent with the ACTION_SENDTO action and the Gmail app's URI scheme (mailto:[email protected]).