How to send email from your application
Today we'll create an easy email sender application.
First of all we need to create a layout to set the address, the subject and email body box.
- <?xml version="1.0" encoding="utf-8"?><LinearLayout android:id="@
- +id/LinearLayout01" android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.c
om/apk/res/android" - android:orientation="vertical"><LinearLayout android:id="@+id/LinearLayout02"
; - android:layout_width="wrap_content" android:layout_height="wrap_content"
- android:orientation="horizontal"><EditText android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:width="170dip" android:id="@
- +id/emailaddress"></EditText><TextView android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/emailaddress"
- android:text="Email address"></TextView>
- </LinearLayout>
- <LinearLayout android:id="@+id/LinearLayout03"
; - android:layout_width="wrap_content" android:layout_height="wrap_content"
- android:orientation="horizontal"><EditText android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:width="170dip" android:id="@
- +id/emailsubject"></EditText><TextView android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/emailsubject"
- android:text="Subject"></TextView>
- </LinearLayout>
- <EditText android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:lines="5" android:width="300dip"
- android:id="@+id/emailtext"></EditText>
- <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
- android:id="@+id/emailsendbutton&quo
t; android:text="Send!" - android:width="150dip"></Button>
- </LinearLayout>
Ugly, but works...
Next we create a new class, called ....uhhhm...Email, then modify like this:
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListe
ner; - import android.widget.Button;
- import android.widget.EditText;
- public class Email extends Activity {
- Button send;
- EditText address, subject, emailtext;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.email);
- address=(EditText) findViewById(R.id.emailaddress);
- subject=(EditText) findViewById(R.id.emailsubject);
- emailtext=(EditText) findViewById(R.id.emailtext);
- send.setOnClickListener(new OnClickListener() {
- @Override
- // TODO Auto-generated method stub
- final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
- emailIntent.setType("plain/text");
- emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ address.getText().toString()});
- emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject.getText());
- emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext.getText());
- Email.this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
- }
- });
- }
- }
It will use the button's onclicklistener method to send the email. It does not work on emulator, but works on real devices.
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@StephanieNich10 (Stephanie Nichols)#android I laughed so hard at ochocinco 's avi. Hahahaha http://t.co/MPuhhi3m
25 weeks 4 days ago -
@CarlaAtkins8 (Carla Atkins)#android Omg! This is actually f'n interesting http://t.co/JodMOehr
25 weeks 4 days ago -
@MarianMcleod12 (Marian Mcleod)#android Precisely what song is? http://t.co/YmJXU0rB
25 weeks 4 days ago -
@JoBeach15 (Jo Beach)#android haha this made me laugh, i love ted:-) http://t.co/gtcWQ79C
25 weeks 4 days ago -
@aochart3 (青ちゃ)Start playing Paradise Island on Android http://t.co/DEID0Ao5 #Android #Androidgames #Gameinsight http://t.co/e1bifSeL
25 weeks 4 days ago
Poll
Useful resources
Android Development Projects
- Android bases applications development (only indian & very skilled developers apply) by sudhirjeet01
- Health Care App - want freelancer in Georgia by rshearer
- IOS+Android+HTML5+PHP Expert Needed Urgently by johnusa1
- NFC Keyboard Wedge by billinginfoes
- I need an iphone and android app + web services by senjy
- Mobile Remote control software for android only by leoagent786
- Add a "Print" button, with code, to our existing process (PDF explains) by sportingchance
- I need a phone app - Android and Iphone - a copy of an existing app by DooniseP
- Apple & Android app - Connecting our CRM and an external GPS device by kshyamnatraj
- Flash&Actionscript app for android by mobistar2013



