Email sending in AndroidEmail sending in Android

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.
Code which need to use:

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 field 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;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

Send email dialog

Click K-9 Mail (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.
K9 mail client
From this moment you can send,discard,edit,… your email.
Also I want to show how will look default email client if you will prefer to use it
Default email

 

Download code from this post.

 

IMPORTANT NOTE:if you don’t use k9mail client you have to configure default email client. WHY? As you can see on the last image you need select an email client. If you don’t have configured client then system by default will run Mesagging application –and this case is another story.

 

That’s all.
As you can see send email is easy operation.
Don’t forget to grab our RSS feeds.

Share

No related posts.

10 comments to Email sending in Android

Leave a Reply

  

  

  

You can use these HTML tags

<a href=""title=""><abbr title=""><acronym title=""><b><blockquote cite=""><cite><code><del datetime=""><em><i><q cite=""><strike><strong>

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna,tincidunt vitae molestie nec,molestie at mi. Nulla nulla lorem,suscipit in posuere in,interdum non magna.