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

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.
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
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.
No related posts.

What happens if the user is not using K-9 mail? Will their default mail program be used? Do all Android devices have a mail client marked as “default”?
Can you post the screen shot of the mail client when opened after receiving the data?
Hi.
By default Android has default email client.
Do you mean screen shot after selecting email client?
Hi,
I can’t get this work. The the body of the email passed through correctly,but not the TO:data. Am I missing a manifest permission?
Code that I’m using is copy and pasted.
Hi Chris. I think I found your problem. I updated post with important note.
If it will not work for you,notify me.
Oleg,
I see the Important Note about having k9mail,but I don’t think that’s the problem. When the ‘Send Mail…’dialog comes up,I selected Gmail. I then see a new message with the custom Message Body message and custom Subject. But the “To:”field is blank. The recipient data wasn’t passed through.
I thought it might have to do with <intent filters…or something else in the Manifest. But I'm stuck.
Chris is is very strange problem.
Unfortunately I don’t use other <intent filters…only default.
Did you run code from this post? I mean there is a zip with this example.
I added new screen shots. Please check if your default email client looks like this one.
I will be waiting your answer.
After launching the screen,i can see all the fields like subject,extra attachment etc…My problem is that I don’t want these fields to be changed(to be more specific,I don’t want the values in “to”,“subject”etc to be changed) after the email editor is launched. Can you please help me. Thanks in advance
This does work great,I also find if I leave the email blank (while still having two blank strings in my email address array) I allow user to choose address at will,vice having an odd address in place.
Anyway –I’ve scoured the net and need help follow-on to this. Again the dreaded Activity sequence kills me. How can I return back to my application after the email is sent? Right now,the email is sent and in good android fashion,the app moves to background. I would like it to come back to the screen the user was on.
Thanks!
Never mind –duh –feel stupid =- lol.
Just remove finish();
Oi!
[...] intent http://thedevelopersinfo.com/2009/10/22/email-sending-in-android [...]