In this tutorial we will see how to send mail from one mobile to another mobile. Android supports email service. The Gmail/Email application on Android enables you to configure an email account using POP3 or IMAP, You can also send  email messages programmatically from within your android application. The following Try It Out show you how...


TRY IT OUT Sending E‑ mail Programmatically


1. Using Eclipse, create a new Android project and name it EmailSendExample.

2. Add the following statements to the main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <Button android:id="@+id/clickBtn" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:layout_gravity="center" />
</LinearLayout>

3. Add the following statements in bold to the Main.java file:


package com.thedevelopersinfo.tutorial.android.emailsendexample;
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;
public class Main extends Activity {
    private Button clickBtn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        clickBtn = (Button) findViewById(R.id.clickBtn);
        clickBtn.setText("Send email");
        clickBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                String[] recipients = new String[]{"server.mumbai@gmail.com"};
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test");
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Hello Friend");
                emailIntent.setType("text/plain");
                startActivity(Intent.createChooser(emailIntent, "Send mail..."));
                finish();
            }
        });
    }
}
4.   Press F11 to test the application on a real Android device. Click the Send Email button and you should see the Email application launched in your device.



How It Works


In this tutorial, you are launching the built-in Email application to send an e‑mail message. To do so, you use an Intent object and set the various parameters using the setData(), putExtra(), and setType() methods:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
String[] recipients = new String[]{"server.mumbai@gmail.com"};
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test");
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Hello Friend");
                emailIntent.setType("text/plain");           
startActivity(Intent.createChooser(emailIntent, "Send mail...")); 
finish();
Download Code

Categories: