Monday 5 March 2012

Send Email with Intent in Android

Providing email support in android is very easy. There are few steps that we must consider in order to create email. Later in this post we will see how to attach multiple formats to mail.

Assuming that the project is being created, you can select 1.6 target device or greater.

In onCreate method we will create and Intent object which will take one parameter as an Action. We will refer to constant of Intent class which is: Intent.ACTION_SEND 
This tells the Intent that we are trying to send some text.

Intent sendEmail= new Intent(Intent.ACTION_SEND);
sendEmail.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"example@gmail.com"}); //This line is not necessary, it will just fill the email of the receiver.
sendEmail.putExtra(Intent.EXTRA_SUBJECT, "Images"); //This line is also not  necessary, the EXTRA_SUBJECT will add the subject line.
startActivity(sendEmail); //This will send Email directly to the receiver's address 
OR
startActivity(Intent.createChooser(sendEmail, "Email:")); // This line will ask the user to select the type of email network to send. Like through gmail, facebook etc



To attach as PDF:

Intent sendEmail= new Intent(Intent.ACTION_SEND);
sendEmail.setType("application/pdf");
sendEmail.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/tutorial/AndroidTutorial.pdf")); 
startActivity(Intent.createChooser(sendEmail, "Email:"));


To attach as JPG:

Intent sendEmail= new Intent(Intent.ACTION_SEND);
sendEmail.setType("jpeg/image");
sendEmail.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/tutorial/Image.jpg")); 
startActivity(Intent.createChooser(sendEmail, "Email:"));


To attach as 3GP video format:

Intent sendEmail= new Intent(Intent.ACTION_SEND);
sendEmail.setType("video/3gp");
sendEmail.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/tutorial/video.3gp")); 
startActivity(Intent.createChooser(sendEmail, "Email:"));

Now this is it!
We have successfully implemented Email functionality with the help of Intent.




No comments:

Post a Comment