0% found this document useful (0 votes)
63 views

Send Email Example: Mainactivity - Java

This document contains code for an Android app that demonstrates different ways to send emails from an app. It includes Java code for an activity with methods to send a simple email, an email with text content, and an email by opening the system chooser. It also includes XML layout and string resources used by the activity.

Uploaded by

Yun Kai Egon Liu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Send Email Example: Mainactivity - Java

This document contains code for an Android app that demonstrates different ways to send emails from an app. It includes Java code for an activity with methods to send a simple email, an email with text content, and an email by opening the system chooser. It also includes XML layout and string resources used by the activity.

Uploaded by

Yun Kai Egon Liu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

SEND EMAIL EXAMPLE

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">[email protected]</string> <string name="email_address_cc">[email protected]</string> <string name="email_address_bcc">[email protected]</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>

You might also like