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?
Asked
Active
Viewed 8.5k times
18
-
1I think this would answer your question: http://stackoverflow.com/questions/2197741/how-can-i-send-emails-from-my-android-application – ya man Feb 16 '15 at 17:13
3 Answers
30
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 = {"someone@gmail.com"};
String[] CC = {"xyz@gmail.com"};
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();
}
}

Psypher
- 10,717
- 12
- 59
- 83
-
I got an error: "The logging tag can be at most 23 characters, was 25 (Finished sending email...) ". – Sapphire_Brick Aug 25 '20 at 18:59
-
remove this to resolve the error: Log.i("Finished sending email...", ""); – Abdulrahman Abdelkader Jun 10 '21 at 11:17
6
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=" + "destination@mail.com");
mailIntent.setData(data);
startActivity(Intent.createChooser(mailIntent, "Send mail..."));

Eraldo
- 169
- 2
- 4
0
https://developer.android.com/guide/components/intents-common#ComposeEmail
Any messaging app:
public void composeEmail(String[] addresses, String subject, Uri attachment) { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("*/*"); intent.putExtra(Intent.EXTRA_EMAIL, addresses); intent.putExtra(Intent.EXTRA_SUBJECT, subject); intent.putExtra(Intent.EXTRA_STREAM, attachment); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); }
}
Only email apps
public void composeEmail(String[] addresses) { Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setData(Uri.parse("mailto:")); // only email apps should handle this intent.putExtra(Intent.EXTRA_EMAIL, addresses); intent.putExtra(Intent.EXTRA_SUBJECT, ""); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); }
}

fullmoon
- 8,030
- 5
- 43
- 58
-
I think it's the otherway aroud, the 2nd code contains the data as mailto which means it should be sent as an email right? – bahaeddin sagar Dec 23 '19 at 14:01
-
-
@Eddie, hi, there is an additional step now, please see my other answer here: https://stackoverflow.com/questions/62535856/intent-resolveactivity-returns-null-in-api-30/65166064#65166064 – fullmoon Apr 22 '22 at 10:49