Home > Android, Android how to > Email sending in Android

Email sending in Android


We moved. Please visit this link for this post.

 

 

 

Today I am going to show code snippet and example about how to send email from code.

In example I will open my email client with predefined data in fields: To, Subject, Message.

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

String[] recipients = new String[]{"my@email.com", "",};

emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);

emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test");

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is email's message");

emailIntent.setType("text/plain");

startActivity(Intent.createChooser(emailIntent, "Send mail..."));

finish();

What about reciepients?

I found that if in array only one address(String) then my email client doesn’t put it in filed To. I always put empty string after email. It is true, all our applications works in this way :)

As you can see it is not hard to send email from code.

I will post whole example and after I will explain how to test it.

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;

/**

* Class which shows how to send email

*

* @author FaYna Soft Labs

*/

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.click);

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[]{"my@email.com", "",};

emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);

emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test");

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is email's message");

emailIntent.setType("text/plain");

startActivity(Intent.createChooser(emailIntent, "Send mail..."));

finish();

}

});

}

}

Okay. Copy this example to the IDE.

Next. I tested on the emulator and I use on my phone email client called k9mail. I want to say big thanks to the developers who are behind this project. Project is very cool (personally for me).

From their site you can download last version of email client.

After downloading need to install it. How to install applications from command line is here.

After installing need to configure your email account. It is very easy. I will not describe it here.

After that I recommend to reinstall/redeploy our example.

When you will see an activity click “Send email” button.

You should see this

email

Click K-9 (it is our installed email client) and you will go to the “New email view“. In our example, To, Subject and Message, these fields are with predefined values.

From this moment you can send, discard, edit,…  your email.

That’s all.
As you saw sending email is easy operation.

About these ads
Categories: Android, Android how to
  1. ganesh
    March 2, 2010 at 5:24 am | #1

    Hi friend,
    Thanks for the snippet ,it works fine ,but is that possible to skip the two stage before sending mail.I mean when “send mail” button is clicked the app needs to send the email and restores to current activity ,skipping the screen shown above and another screen asking to enter text and subject .is that possible
    ganesh

    • March 2, 2010 at 10:43 am | #2

      Hello ganesh.
      You can skip window which ask you what program do you want to use by default for sending email, but you will see email client’s window every time.
      For skipping chooser window write startActivity(emailIntent). It should ask you to choose default application.

  2. Ashish
    March 8, 2010 at 3:22 pm | #3

    How u set the image to the alertDialog?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: