Following section explains different parts of our Intent object required to send an email.
Intent Object - Action to send Email
You will use ACTION_SEND action to launch an email client installed on your Android device. Following is simple syntax to create an intent with ACTION_SEND action
Intent emailIntent = new Intent(Intent.ACTION_SEND);
Intent Object - Data/Type to send Email
To send an email you need to specify mailto: as URI using setData() method and data type will be totext/plain using setType() method as follows:
emailIntent.setData(Uri.parse("mailto:")); emailIntent.setType("text/plain");
Intent Object - Extra to send Email
Android has built-in support to add TO, SUBJECT, CC, TEXT etc. fields which can be attached to the intent before sending the intent to a target email client. You can use following extra fields in your email:
S.N. | Extra Data & Description |
---|---|
1 | EXTRA_BCC A String[] holding e-mail addresses that should be blind carbon copied. |
2 | EXTRA_CC A String[] holding e-mail addresses that should be carbon copied. |
3 | EXTRA_EMAIL A String[] holding e-mail addresses that should be delivered to. |
4 | EXTRA_HTML_TEXT A constant String that is associated with the Intent, used with ACTION_SEND to supply an alternative to EXTRA_TEXT as HTML formatted text. |
5 | EXTRA_SUBJECT A constant string holding the desired subject line of a message. |
6 | EXTRA_TEXT A constant CharSequence that is associated with the Intent, used with ACTION_SEND to supply the literal data to be sent. |
7 | EXTRA_TITLE A CharSequence dialog title to provide to the user when used with a ACTION_CHOOSER. |
Here is an example showing you how to assign extra data to your intent:
emailIntent.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient@example.com"}); emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject of email"); emailIntent.putExtra(Intent.EXTRA_TEXT , "body of email");
Example
MainActivity.java
- public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button = (Button) findViewById(R.id.button1);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent i = new Intent(Intent.ACTION_SEND);i.setType("message/rfc822");i.putExtra(Intent.EXTRA_EMAIL,new String[] { "" });i.putExtra(Intent.EXTRA_SUBJECT," \n\n www.google.com"); // your urli.putExtra(Intent.EXTRA_TEXT,"new test"); // your text in place of contentStrtry {MainActivity.this.startActivity(Intent.createChooser(i,"Send mail..."));} catch (android.content.ActivityNotFoundException ex) {ex.printStackTrace();}}});}}
main.xml
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity" ><Buttonandroid:id="@+id/button1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignLeft="@+id/textView1"android:layout_below="@+id/textView1"android:text="Send data to gmail" /></RelativeLayout>
No comments:
Post a Comment