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

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.
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
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.
How u set the image to the alertDialog?
Hello Ashish.
If you are creating alert dialog via AlertDialog.Builder then use setIcon() method.