Android send e-mail message

In this Android tip, I am going to show how to code in Android to send an e-mail message to a recipient. When you create an application that contains the sending e-mail message functionality, this tip might be useful to you.

Android provides a simple way to send the e-mail message programmatically. In your code, first you need to create an Intent object with the Intent.ACTION_SEND value passed to the Intent class's constructor.

//create an Intent object to send e-mail message
Intent intent=new Intent(Intent.ACTION_SEND);

Then you can specify the mine type of the e-mail data with the setType method as shown below. This method accepts a string that represents the mine type of the e-mail data. Here are the common mine types used in Android:
image/*
video/*
audio/*
text/plain
text/html

For example, to send an e-mail data of the image type, you can specify the mine type as below:
//specify the mine type of the e-mail message
intent.setType("image/*");

If you want to send an e-mail data of any mine type or unknown type, the mine type string will be */*.

After you specified the mine type of the e-mail data, you will set recipients' e-mail addresses, subject, message, and file attachment of the e-mail by using the putExtra method. This method accepts pairs of keys and values.


//put the recipient e-mail addresses in the intent object
intent.putExtra(Intent.EXTRA_EMAIL,new String[]{"yuk.dara@gmail.com","sovandarachoum@yahoo.com"});
//put the subject in the intent object
intent.putExtra(Intent.EXTRA_SUBJECT,"Hello");
//put the message in the intent object
intent.putExtra(Intent.EXTRA_TEXT,"This is an e-mail sent from Android mobile app.");
//create a Uri object to for the attached file path
String att_path=Environment.getExternalStorageDirectory()+"/myphoto.jpg";
Uri urlpath=Uri.parse("file://"+att_path);
//put attached file in the intent object
intent.putExtra(Intent.EXTRA_STREAM,urlpath);


Before starting sending the e-mail message, it is a good idea to know whether there are some e-mail client applications to help you send the e-mail message. You do this task by using the queryIntentActivities method of the PackageManager class.

//query available e-mail clients and start sending e-mail message
PackageManager pm=getPackageManager();
List<ResolveInfo> vapps=pm.queryIntentActivities(intent, 0);
if(vapps.size()>0){
startActivity(Intent.createChooser(intent,"Send mail"));
}



Android-send-e-mail-message

Posted by: Dara | post date: 06-14-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.