0% found this document useful (0 votes)
11 views3 pages

Exp 30

The document outlines a program to send emails using an Android application. It includes the XML layout for the main activity and the Java code for handling user input and sending the email. The program checks for empty fields and handles exceptions if no email clients are installed on the device.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Exp 30

The document outlines a program to send emails using an Android application. It includes the XML layout for the main activity and the Java code for handling user input and sending the email. The program checks for empty fields and handles exceptions if no email clients are installed on the device.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment 30

1. Write a program to send email.


Activity_main.xml Import
<?xml version="1.0" encoding="utf-8"?> androidx.appcompat.app.AppCompatAc
<LinearLayout tivity;
xmlns:android="http://schemas.android.com import android.content.Intent;
/apk/res/android" import android.net.Uri;
import android.os.Bundle;
xmlns:app="http://schemas.android.com/ap
k/res-auto"
import android.view.View;
import android.widget.Button;
xmlns:tools="http://schemas.android.com/to import android.widget.EditText;
ols" import android.widget.Toast;
android:orientation="vertical" public class MainActivity extends
android:padding="16dp" AppCompatActivity {
android:gravity="center" private EditText etTo, etSubject,
android:layout_width="match_parent" etMessage;
android:layout_height="match_parent"> private Button btnSend;
<EditText @Override
android:id="@+id/etSubject"
protected void onCreate(Bundle
android:layout_width="match_parent"
android:layout_height="wrap_content"
savedInstanceState) {
android:layout_marginTop="8dp"
android:hint="Subject" super.onCreate(savedInstanceState);
android:minHeight="48dp"
android:padding="10dp" /> setContentView(R.layout.activity_main);
<EditText etTo = findViewById(R.id.etTo);
android:id="@+id/etMessage" etSubject =
android:hint="Message" findViewById(R.id.etSubject);
android:layout_width="match_parent" etMessage =
android:layout_height="wrap_content" findViewById(R.id.etMessage);
android:minLines="5" btnSend =
android:padding="10dp" findViewById(R.id.btnSend);
android:layout_marginTop="8dp" />
btnSend.setOnClickListener(v ->
<Button
android:id="@+id/btnSend"
sendEmail());
android:layout_width="wrap_content" }
android:layout_height="wrap_content" private void sendEmail() {
android:layout_marginTop="16dp" /> String to =
</LinearLayout> etTo.getText().toString().trim();
String subject =
MainActivity.java etSubject.getText().toString().trim();
String message =
package com.example.exp_23; etMessage.getText().toString().trim();
if (to.isEmpty() || subject.isEmpty()
|| message.isEmpty()) {
Toast.makeText(this, "Please fill
all fields",
Toast.LENGTH_SHORT).show();
return;
}
Intent emailIntent = new
Intent(Intent.ACTION_SENDTO);

emailIntent.setData(Uri.parse("mailto:" +
to));

emailIntent.putExtra(Intent.EXTRA_SUB
JECT, subject);

emailIntent.putExtra(Intent.EXTRA_TEX
T, message);
try {

startActivity(Intent.createChooser(emailI
ntent, "Send email using"));
} catch (Exception e) {
Toast.makeText(this, "No email
clients installed",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
Output

co2022.muskan.kateja@ves.

You might also like