Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 7

Mobile App Development

Lecture 7
Send an Email in Android:
 Email is messages distributed by electronic means from one system user to one or
more recipients via a network.
 Email functionality with intent, Intent is carrying data from one component to another
component with-in the application or outside the application.
 We can do this with the help of Intent with action as ACTION_SEND with extra
fields:
 email id to which you want to send mail,
 the subject of the email and
 body of the email.
 Basically Intent is a simple message object that is used to communicate
between android components such as activities, use to send the email. This application
basically contains one activity with EditText to take input of email address, subject, and
body of the email from the user and button to send that email.
Send an Email in Android:
 Syntax
 Intent emailIntent = new Intent(Intent.ACTION_SEND);
 emailIntent.setData(Uri.parse("mailto:"));
 emailIntent.setType("text/plain");
 emailIntent.putExtra(Intent.EXTRA_EMAIL,newString[]{"Recipient"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT,"subject");
emailIntent.putExtra(Intent.EXTRA_TEXT , "Message Body");
Send an Email in Android :
 Step 1: Create a new Project
 Step 2: Make design in activity.xml file

This application basically contains one activity with EditText to take input of email
address, subject, and body of the email from the user and button to send that email.
 Step 3: Code in activity,java file

public class MainActivity extends Activity {


EditText editTextTo,editTextSubject,editTextMessage;
Button send;
public void onCreate( ) {
setContentView(R.layout.activity_main);
editTextTo=(EditText)findViewById(R.id.editText1);
editTextSubject=(EditText)findViewById(R.id.editText2);
editTextMessage=(EditText)findViewById(R.id.editText3);

send=(Button)findViewById(R.id.button1);
Send an Email in Android :
send.setOnClickListener(new OnClickListener(){
public void onClick( )
{
String emailsend = sendto.getText().toString();
String emailsubject = subject.getText().toString();
String emailbody = body.getText().toString();
Intent intent = new Intent(Intent.ACTION_SEND);

// add three fields to intent using putExtra function


intent.putExtra(Intent.EXTRA_EMAIL, new String[]{emailsend});
intent.putExtra(Intent.EXTRA_SUBJECT, emailsubject);
intent.putExtra(Intent.EXTRA_TEXT, emailbody);
Intent intent = new Intent(Intent.ACTION_SEND);

// set type of intent


intent.setType("message/rfc822");
// startActivity with intent with chooser as Email client using createChooser function
startActivity(Intent.createChooser(intent, "Choose an Email client :")); }}
Send an Email in Android :
Thank You

You might also like