Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to code using android studio to send an email

Tags:

android

I'm planning to develop an android mobile application using android studio, where an user give email address and secret code. Then that secret code should be send to mentioned email address. Can any body share any code snippet in order to do this?

like image 590
Siluni Upeksha Avatar asked Feb 16 '15 17:02

Siluni Upeksha


People also ask

How do I create an Android email app?

Follow these steps to create a native Email Android application using Android Studio. I have attached the source code too with this article. Open Android Studio and start a new Android Studio project. Choose your application name and choose where your project is stored at the location.

Can you write code in Android Studio?

Android Studio includes tools for every stage of development, but what's most important is simply writing your app: writing the code, building layouts, creating images, and being productive along the way. That's what this section is all about: the tools that help you write your app and write it quickly.


2 Answers

If you want to send email in background refer here

If user is waiting on screen use below method:

protected void sendEmail() {
      Log.i("Send email", "");

      String[] TO = {"[email protected]"};
      String[] CC = {"[email protected]"};
      Intent emailIntent = new Intent(Intent.ACTION_SEND);
      emailIntent.setData(Uri.parse("mailto:"));
      emailIntent.setType("text/plain");


      emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
      emailIntent.putExtra(Intent.EXTRA_CC, CC);
      emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
      emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");

      try {
         startActivity(Intent.createChooser(emailIntent, "Send mail..."));
         finish();
         Log.i("Finished sending email...", "");
      } catch (android.content.ActivityNotFoundException ex) {
         Toast.makeText(MainActivity.this, 
         "There is no email client installed.", Toast.LENGTH_SHORT).show();
      }
   }
like image 148
Psypher Avatar answered Sep 28 '22 17:09

Psypher


If you use Intent.ACTION_SEND android show all communicatons app. If you want show only email client you can use the following code.

 Intent mailIntent = new Intent(Intent.ACTION_VIEW);
 Uri data = Uri.parse("mailto:?subject=" + "subject text"+ "&body=" + "body text " + "&to=" + "[email protected]");
 mailIntent.setData(data);
 startActivity(Intent.createChooser(mailIntent, "Send mail..."));
like image 32
Eraldo Avatar answered Sep 28 '22 15:09

Eraldo