We can easily send email in android via intent. You need to write few lines of code only as given below
Intent email = new Intent(Intent.ACTION_SEND); email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to}); email.putExtra(Intent.EXTRA_SUBJECT, subject); email.putExtra(Intent.EXTRA_TEXT, message); //need this to prompts email client only email.setType("message/rfc822"); startActivity(Intent.createChooser(email, "Choose an Email client :"));
Example of phone call in android
activity_main.xml
Drag the 2 EditTexts, 1 MultiLine EditText, 3 TextViews and 1 Button from the pallete, now the activity_main.xml file will like this:
File: activity_main.xml
Activity class
Let’s write the code to send email via intent.
File: MainActivity.java
package com.example.sendemail; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { EditText editTextTo,editTextSubject,editTextMessage; Button send; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 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.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { String to=editTextTo.getText().toString(); String subject=editTextSubject.getText().toString(); String message=editTextMessage.getText().toString(); Intent email = new Intent(Intent.ACTION_SEND); email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to}); email.putExtra(Intent.EXTRA_SUBJECT, subject); email.putExtra(Intent.EXTRA_TEXT, message); //need this to prompts email client only email.setType("message/rfc822"); startActivity(Intent.createChooser(email, "Choose an Email client :")); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
Leave A Comment