Sending Mails using Intents

Using ACTION_SEND in intent for mail..

The code written below is using ACTION_SEND in the intent to generate mail which can be used to send the email to the single or multiple  person. firstly we will generate the intent with ACTION_SEND or ACTION_SENDTO. ACTION_SENDTO has one drawback that we can not add the attachment/files to the intent that we are sending. after setting the ACTION we will set up the intent with the type of the data that it is holding, for now we will use "message/rfc822", documentation on the type you can get here (rfc822). Now we will move on to the setting the parameters that we are going to pass to our email using the function putExtras(). after setting up parameters for the mail we will create a chooser and launch our intent.

NOTE: The Android device on which application is going to run must have already setup their mailing application.

CODE:

 Intent emailIntent=new Intent(Intent.ACTION_SEND);
 
//add the multiple or single reciever
String[] reciever={"abc@xyz.com","pqr@xyz.com"};
//set the type
 emailIntent.setType("message/rfc822");

//set the parameters using extras
 emailIntent.putExtra(Intent.EXTRA_EMAIL,to);

// here Uri object to the file will be passed, for that use the FileProvider
emailIntent.putExtra(Intent.EXTRA_STREAM,"here pass the Uri to the file"); 

// setting subject for mail
emailIntent.putExtra(Intent.EXTRA_SUBJECT,"This is the Subject");

//writing text 
emailIntent.putExtra(Intent.EXTRA_TEXT,"this is the BOdy text..");

//launching the chooser to select the mailing application from the available apps.
startActivity(Intent.createChooser(emailIntent,"select mail..."));

ACTION_SENDTO is used to send the text body but we can not attach the file to it. targetsdkversion>=24

And if you want to add multiple file at once you can use ACTION_SEND_MULTIPLE can be used to create the intent.

Intent emailIntent=new Intent(ACTION_SEND_MULTIPLE); 
emailIntent.setType("message/rfd822");

//add the Uri to all the files that you want to share
ArrayList<Uri> myfiles = new ArrayList<Uri>();
 myfiles.add(Uri1);//add Uri object to the files
 myfiles.add(Uri2);
    ..
     ..
    ..
 myfiles.add(UriN);

//use the same code as above to generate the List of people with whom you
//want to share this file and put it on the emailIntent using //putExtra(Intent.EXTRA_EMAIL,to) function

emailIntent.putParceableArrayListExtra(Intent.EXTRA_STREAM,myfiles);



//launching the chooser to select the mailing application from the available apps.
startActivity(Intent.createChooser(emailIntent,"select mail..."));

Hoping this would be the answer you have been looking for. If any doubt feel free to ask it in the comment section. AND ANOTHER THING TO NOTE IS THAT IF U_YOU ARE ACCESSING FILE USING URI AND YOUR targetsdkversion>=24 then You must use file Provider in order to Avoid FileUriExposedExceptiom.

Comments

Popular posts from this blog

Contest Leaderboard: hackerrank problem (Level medium)

The Occupation: hackerrank problem

Placements: SQL Advance join (Hackerrank Problem)