Android code rate us submit form to mail id

Here is an example of how you can submit a form to an email address using Android:

Step 1: Create a form layout

Create a layout file (e.g. form.xml) with the following code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Name" />

    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email" />

    <EditText
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Message" />

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

</LinearLayout>

Step 2: Create a Java class to handle the form submission

Create a Java class (e.g. FormActivity.java) with the following code:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Properties;

public class FormActivity extends Activity {

    private EditText nameEditText;
    private EditText emailEditText;
    private EditText messageEditText;
    private Button sendButton;

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

        nameEditText = findViewById(R.id.name);
        emailEditText = findViewById(R.id.email);
        messageEditText = findViewById(R.id.message);
        sendButton = findViewById(R.id.send);

        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = nameEditText.getText().toString();
                String email = emailEditText.getText().toString();
                String message = messageEditText.getText().toString();

                if (validateForm(name, email, message)) {
                    sendEmail(name, email, message);
                } else {
                    Toast.makeText(FormActivity.this, "Please fill out all fields", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private boolean validateForm(String name, String email, String message) {
        if (name.isEmpty() || email.isEmpty() || message.isEmpty()) {
            return false;
        }
        return true;
    }

    private void sendEmail(String name, String email, String message) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("[email protected]", "your_password");
            }
        });

        try {
            Message message1 = new MimeMessage(session);
            message1.setFrom(new InternetAddress("[email protected]"));
            message1.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
            message1.setSubject("Form Submission");
            message1.setText("Name: " + name + "\nEmail: " + email + "\nMessage: " + message);

            Transport.send(message1);
            Toast.makeText(FormActivity.this, "Email sent successfully", Toast.LENGTH_SHORT).show();
        } catch (MessagingException e) {
            Toast.makeText(FormActivity.this, "Error sending email", Toast.LENGTH_SHORT).show();
        }
    }
}

Step 3: Add the activity to the AndroidManifest.xml file

Add the following code to the AndroidManifest.xml file:

<activity
    android:name=".FormActivity"
    android:exported="true" />

Step 4: Run the app

Run the app on an emulator or a physical device. When you click the "Send" button, the form data will be sent to the email address specified in the sendEmail method.

Note: Make sure to replace "[email protected]" and "your_password" with your actual Gmail email and password. Also, make sure to enable less secure apps in your Google account settings to allow the app to send emails.