Send Email Example: Mainactivity - Java
Send Email Example: Mainactivity - Java
Send Email Example: Mainactivity - Java
MainActivity.java:
package com.example.email3; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } // Send a simple plain text email (no contents supplied) public void sendSimpleEmail(View button) { Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); emailIntent.setType("plain/text"); startActivity(emailIntent); } public void sendPlainTextEmail(View button) { Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); String aEmailList[] = { getResources() .getString(R.string.email_address) }; String aEmailCCList[] = { getResources().getString( R.string.email_address_cc) }; String aEmailBCCList[] = { getResources().getString( R.string.email_address_bcc) }; emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList); emailIntent.putExtra(android.content.Intent.EXTRA_CC, aEmailCCList); emailIntent.putExtra(android.content.Intent.EXTRA_BCC, aEmailBCCList); emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getResources().getString(R.string.email_subject)); emailIntent.setType("plain/text"); emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, getResources() .getString(R.string.email_message)); startActivity(emailIntent); } public void chooseEmail(View button) { Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); emailIntent.setType("plain/text"); startActivity(Intent.createChooser(emailIntent, getResources().getString(R.string.chooser))); } }
Values strings.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Click on the buttons to send an email in different ways</string> <string name="app_name">Message Launcher</string> <string name="buttontext_simpleemail">Send plain text</string> <string name="chooser">Send message using:</string> <string name="chooser_pic">Send picture using:</string> <string name="buttontext_simpleemail_chooser">Send a message (Choose app to send)</string> <string name="buttontext_email">Send plaintext message (with supplied content)</string> <string name="buttontext_msg_pic">Send picture msg (with supplied content)</string> <string name="email_message">This is my message body.\n\nCheers,\nMe</string> <string name="email_subject">This is my subject</string> <string name="email_address">test@fakehost.com</string> <string name="email_address_cc">test_cc@fakehost.com</string> <string name="email_address_bcc">test_bcc@fakehost.com</string> <string name="title_activity_send_message">SendMessageActivity</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> </resources>
Layout activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ScrollView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scrollbars="vertical"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:gravity="center"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:gravity="center_horizontal" android:layout_gravity="top" android:textSize="10pt" /> <Button android:id="@+id/Button_Simple" android:layout_height="wrap_content" android:text="@string/buttontext_simpleemail" android:layout_width="fill_parent" android:onClick="sendSimpleEmail"></Button> <Button android:id="@+id/Button_Email" android:layout_height="wrap_content" android:text="@string/buttontext_email" android:layout_width="fill_parent" android:onClick="sendPlainTextEmail"></Button> <Button android:id="@+id/Button_Chooser" android:layout_height="wrap_content" android:text="@string/buttontext_simpleemail_chooser" android:layout_width="fill_parent" android:onClick="chooseEmail"></Button> </LinearLayout> </ScrollView>