How to send email from and ANDROID Application programatically?
Hello all……..
In today’s post I will show you send mail from an android application progrmatically..
Let’s go to the code fast……
This is the code in the mail java file….
package com.coderzheaven;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class sendMailDemo extends Activity {
Button send;
EditText address, subject, emailbody;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
address = (EditText) findViewById(R.id.address);
subject = (EditText) findViewById(R.id.subject);
emailbody = (EditText) findViewById(R.id.body);
send = (Button) findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendEmail();
}
});
}
public void sendEmail(){
if(!address.getText().toString().trim().equalsIgnoreCase("")){
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, emailbody.getText());
sendMailDemo.this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
else{
Toast.makeText(getApplicationContext(), "Please enter an email address..", Toast.LENGTH_LONG).show();
}
}
}
Now the layout file (main.xml)
<?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.com/apk/res/android" android:orientation="vertical" android:background="@drawable/android" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/emailaddress" android:text="Email Address" android:textStyle="bold"> </TextView> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="250dip" android:hint="email address" android:id="@+id/address"> </EditText> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Subject" android:textStyle="bold"> </TextView> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="250dip" android:hint="Subject" android:id="@+id/subject"> </EditText> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Your Message" android:textStyle="bold"> </TextView> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:lines="5" android:hint="Your message here!!" android:id="@+id/body"> </EditText> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/send" android:text="Send Email" android:width="150dip"> </Button> </LinearLayout>
The AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.coderzheaven"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="Send Mail Demo">
<activity android:name=".sendMailDemo"
android:label="Send Mail Demo">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Note: However if you test this in your emulator, it will not work. Install it in your device to test it.
Link to this post!