Mobile Application Development Practical 30
Question 1
Write a program to send email.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Android Email"
android:gravity="center"
android:textSize="20dp"
android:layout_marginTop="20dp"
android:textStyle="bold"/>
<EditText
android:id="@+id/sendto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginTop="50dp"
android:hint="Enter email address here" />
<EditText
android:id="@+id/sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter subject" />
<EditText
android:id="@+id/mail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter email text" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Send" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.practical30;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText sender,subject,text;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sender=findViewById(R.id.sendto);
subject=findViewById(R.id.sub);
text=findViewById(R.id.mail);
b=findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String sendto=sender.getText().toString();
String sub=subject.getText().toString();
String msg=text.getText().toString();
Intent eintent=new Intent(Intent.ACTION_SEND);
eintent.putExtra(Intent.EXTRA_EMAIL,new String[]{sendto});
eintent.putExtra(Intent.EXTRA_SUBJECT,sub);
eintent.putExtra(Intent.EXTRA_TEXT,msg);
eintent.setType("message/rfc822");
startActivity(Intent.createChooser(eintent,"Choose email application"));
}
});
}
}
Output

