Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_EMAIL, "[email protected]");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "I'm email body.");
startActivity(Intent.createChooser(intent, "Send Email"));
Der obige Code öffnet ein Dialogfeld mit folgenden Apps: - Bluetooth, Google Text & Tabellen, Yahoo Mail, Gmail, Orkut, Skype usw.
Eigentlich möchte ich diese Listenoptionen filtern. Ich möchte nur E-Mail-bezogene Apps anzeigen, z. Google Mail, Yahoo Mail. Wie es geht?
Ich habe ein solches Beispiel in der Anwendung "Android Market" gesehen.
Das Dialogfeld zeigt nur E-Mail-Apps an, z. Google Mail, Yahoo Mail usw. Es werden keine Bluetooth, Orkut usw. angezeigt. Welcher Code erzeugt einen solchen Dialog?
wenn Sie wie unten beschrieben Ihren Intent.setType ändern, erhalten Sie
intent.setType("text/plain");
Verwenden Sie Android.content.Intent.ACTION_SENDTO
, um nur die Liste der E-Mail-Clients abzurufen, ohne Facebook oder andere Apps. Nur die E-Mail-Clients . Bsp .:
new Intent(Intent.ACTION_SENDTO);
Ich würde nicht vorschlagen, dass Sie direkt zur E-Mail-App gelangen. Lassen Sie den Benutzer seine bevorzugte E-Mail-App auswählen. Zwinge ihn nicht.
Wenn Sie ACTION_SENDTO verwenden, funktioniert putExtra nicht, um der Absicht Betreff und Text hinzuzufügen. Verwenden Sie Uri, um den Betreff und den Haupttext hinzuzufügen.
EDIT: Wir können message/rfc822
anstelle von "text/plain"
als MIME-Typ verwenden. Dies bedeutet jedoch nicht "nur E-Mail-Clients anbieten", sondern "alles anbieten, was Message/rfc822-Daten unterstützt". Dies könnte leicht eine Anwendung beinhalten, die keine E-Mail-Clients ist.
message/rfc822
unterstützt MIME-Typen von .mhtml, .mht, .mime
Die akzeptierte Antwort funktioniert am 4.1.2 nicht. Dies sollte auf allen Plattformen funktionieren:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","[email protected]", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
startActivity(Intent.createChooser(emailIntent, "Send email..."));
Hoffe das hilft.
Update: Laut marcwjj scheint es, dass unter 4.3 das String-Array anstelle eines Strings für die E-Mail-Adresse übergeben werden muss, damit es funktioniert. Möglicherweise müssen Sie eine weitere Zeile hinzufügen:
intent.putExtra(Intent.EXTRA_EMAIL, addresses); // String[] addresses
Es gibt drei Hauptansätze:
String email = /* Your email address here */
String subject = /* Your subject here */
String body = /* Your body here */
String chooserTitle = /* Your chooser title here */
1. Benutzerdefinierte Uri
:
Uri uri = Uri.parse("mailto:" + email)
.buildUpon()
.appendQueryParameter("subject", subject)
.appendQueryParameter("body", body)
.build();
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(Intent.createChooser(emailIntent, chooserTitle));
2. Verwenden von Intent
-Extras:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + email));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
//emailIntent.putExtra(Intent.EXTRA_HTML_TEXT, body); //If you are using HTML in your body text
startActivity(Intent.createChooser(emailIntent, "Chooser Title"));
3. Unterstützungsbibliothek ShareCompat
:
Activity activity = /* Your activity here */
ShareCompat.IntentBuilder.from(activity)
.setType("message/rfc822")
.addEmailTo(email)
.setSubject(subject)
.setText(body)
//.setHtmlText(body) //If you are using HTML in your body text
.setChooserTitle(chooserTitle)
.startChooser();
Dies ist aus dem offiziellen Dokument von Android zitiert, ich habe es auf Android 4.4 getestet und funktioniert einwandfrei. Weitere Beispiele finden Sie unter https://developer.Android.com/guide/components/intents-common.html#Email
public void composeEmail(String[] addresses, String subject) {
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, subject);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Eine späte Antwort, obwohl ich eine Lösung gefunden habe, die anderen helfen könnte:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:[email protected]"));
startActivity(Intent.createChooser(emailIntent, "Send feedback"));
Dies war meine Ausgabe (nur Google Mail + Posteingang vorgeschlagen):
Ich habe diese Lösung von der Android Developers Site erhalten.
Versuchen:
intent.setType("message/rfc822");
Das funktioniert für mich:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL , new String[] { "[email protected]" });
intent.putExtra(Intent.EXTRA_SUBJECT, "My subject");
startActivity(Intent.createChooser(intent, "Email via..."));
verwenden Sie also die Aktion ACTION_SENDTO
anstelle der Aktion ACTION_SEND
. Ich habe es auf ein paar Android 4.4-Geräten ausprobiert, und es beschränkt das Auswahl-Popup auf die Anzeige von E-Mail-Anwendungen (E-Mail, Gmail, Yahoo Mail usw.) und fügt die E-Mail-Adresse und den Betreff korrekt in die E-Mail ein.
Gehen Sie dazu folgendermaßen vor: Android Developer Docs Fügen Sie der App diese Codezeilen hinzu:
Intent intent = new Intent(Intent.ACTION_SEND);//common intent
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
Wenn Sie den Hauptteil und den Betreff hinzufügen möchten, fügen Sie dies hinzu
intent.putExtra(Intent.EXTRA_SUBJECT, "Your Subject Here");
intent.putExtra(Intent.EXTRA_TEXT, "E-mail body" );
Wenn Sie nur die E-Mail-Clients verwenden möchten, sollten Sie Android.content.Intent.EXTRA_EMAIL
mit einem Array verwenden. Hier ist ein Beispiel:
final Intent result = new Intent(Android.content.Intent.ACTION_SEND);
result.setType("plain/text");
result.putExtra(Android.content.Intent.EXTRA_EMAIL, new String[] { recipient });
result.putExtra(Android.content.Intent.EXTRA_SUBJECT, subject);
result.putExtra(Android.content.Intent.EXTRA_TEXT, body);
Endlich den besten Weg zu finden
String to = "[email protected]";
String subject= "Hi I am subject";
String body="Hi I am test body";
String mailTo = "mailto:" + to +
"?&subject=" + Uri.encode(subject) +
"&body=" + Uri.encode(body);
Intent emailIntent = new Intent(Intent.ACTION_VIEW);
emailIntent.setData(Uri.parse(mailTo));
startActivity(emailIntent);
Der folgende Code funktioniert gut für mich.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
Intent mailer = Intent.createChooser(intent, null);
startActivity(mailer);
Dies war der einzige Weg, den ich zu dieser Zeit gefunden hatte, damit es mit allen Charakteren funktionierte.
die Antwort von Doreamon ist jetzt der richtige Weg, da sie mit allen Zeichen in neuen Versionen von Google Mail funktioniert.
Alte Antwort:
Hier ist mein. Es scheint auf allen Android-Versionen zu funktionieren, mit Unterstützung für Betreff und Nachrichtentext und voller Unterstützung für utf-8-Zeichen:
public static void email(Context context, String to, String subject, String body) {
StringBuilder builder = new StringBuilder("mailto:" + Uri.encode(to));
if (subject != null) {
builder.append("?subject=" + Uri.encode(Uri.encode(subject)));
if (body != null) {
builder.append("&body=" + Uri.encode(Uri.encode(body)));
}
}
String uri = builder.toString();
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
context.startActivity(intent);
}
Keine dieser Lösungen funktionierte für mich. Hier ist eine Minimallösung, die für Lollipop funktioniert. Auf meinem Gerät werden nur Gmail und die nativen E-Mail-Apps in der Ergebnisliste angezeigt.
Intent emailIntent = new Intent(Intent.ACTION_SENDTO,
Uri.parse("mailto:" + Uri.encode(address)));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(emailIntent, "Send email via..."));
Das funktioniert für mich perfekt:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("mailto:" + address));
startActivity(Intent.createChooser(intent, "E-mail"));
Der folgende Code hat für mich gearbeitet !!
import Android.support.v4.app.ShareCompat;
.
.
.
.
final Intent intent = ShareCompat.IntentBuilder
.from(activity)
.setType("application/txt")
.setSubject(subject)
.setText("Hii")
.setChooserTitle("Select One")
.createChooserIntent()
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(intent);
Funktioniert auf allen Android-Versionen:
String[] TO = {"[email protected]"};
Uri uri = Uri.parse("mailto:[email protected]")
.buildUpon()
.appendQueryParameter("subject", "subject")
.appendQueryParameter("body", "body")
.build();
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, uri);
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Wenn Sie sicherstellen möchten, dass Ihre Absicht nur von einer E-Mail-App (und nicht von anderen Textnachrichten oder sozialen Apps) verarbeitet wird, verwenden Sie die Aktion ACTION_SENDTO
und fügen Sie das Datenschema "mailto:" hinzu. Zum Beispiel:
public void composeEmail(String[] addresses, String subject) {
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, subject);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Ich fand das in https://developer.Android.com/guide/components/intents-common.html#Email
Die meisten dieser Antworten funktionieren nur für einen einfachen Fall, wenn Sie attachment nicht senden. In meinem Fall muss ich manchmal Anlagen (ACTION_SEND) oder zwei Anlagen (ACTION_SEND_MULTIPLE) senden.
Also nahm ich die besten Ansätze aus diesem Thread und kombinierte sie. Es verwendet ShareCompat.IntentBuilder
der Unterstützungsbibliothek, aber ich zeige nur Apps, die ACTION_SENDTO mit "mailto:" uri entsprechen. Auf diese Weise bekomme ich nur eine Liste von E-Mail-Apps mit Unterstützung für Anhänge:
fun Activity.sendEmail(recipients: List<String>, subject: String, file: Uri, text: String? = null, secondFile: Uri? = null) {
val originalIntent = createEmailShareIntent(recipients, subject, file, text, secondFile)
val emailFilterIntent = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"))
val originalIntentResults = packageManager.queryIntentActivities(originalIntent, 0)
val emailFilterIntentResults = packageManager.queryIntentActivities(emailFilterIntent, 0)
val targetedIntents = originalIntentResults
.filter { originalResult -> emailFilterIntentResults.any { originalResult.activityInfo.packageName == it.activityInfo.packageName } }
.map {
createEmailShareIntent(recipients, subject, file, text, secondFile).apply { `package` = it.activityInfo.packageName }
}
.toMutableList()
val finalIntent = Intent.createChooser(targetedIntents.removeAt(0), R.string.choose_email_app.toText())
finalIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedIntents.toTypedArray())
startActivity(finalIntent)
}
private fun Activity.createEmailShareIntent(recipients: List<String>, subject: String, file: Uri, text: String? = null, secondFile: Uri? = null): Intent {
val builder = ShareCompat.IntentBuilder.from(this)
.setType("message/rfc822")
.setEmailTo(recipients.toTypedArray())
.setStream(file)
.setSubject(subject)
if (secondFile != null) {
builder.addStream(secondFile)
}
if (text != null) {
builder.setText(text)
}
return builder.intent
}
Wenn Sie Google Mail als Ziel verwenden möchten, können Sie Folgendes tun. Beachten Sie, dass die Absicht "ACTION_SENDTO" und nicht "ACTION_SEND" ist und die Felder für zusätzliche Absichten für Google Mail nicht erforderlich sind.
String uriText =
"mailto:[email protected]" +
"?subject=" + Uri.encode("your subject line here") +
"&body=" + Uri.encode("message body here");
Uri uri = Uri.parse(uriText);
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(uri);
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(Intent.createChooser(sendIntent, "Send message"));
}
Dieser Code funktioniert auf meinem Gerät
Intent mIntent = new Intent(Intent.ACTION_SENDTO);
mIntent.setData(Uri.parse("mailto:"));
mIntent.putExtra(Intent.EXTRA_EMAIL , new String[] {"[email protected]"});
mIntent.putExtra(Intent.EXTRA_SUBJECT, "");
startActivity(Intent.createChooser(mIntent, "Send Email Using..."));
Ich aktualisiere Adils Antwort in Kotlin,
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto:") // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, Array(1) { "[email protected]" })
intent.putExtra(Intent.EXTRA_SUBJECT, "subject")
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
} else {
showSnackBar(getString(R.string.no_apps_found_to_send_mail), this)
}
in Kotlin wenn jemand sucht
val emailArrray:Array<String> = arrayOf("[email protected]")
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto:") // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, emailArrray)
intent.putExtra(Intent.EXTRA_SUBJECT, "Inquire about travel agent")
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
Das ist was ich benutze und es funktioniert für mich:
//variables
String subject = "Whatever subject you want";
String body = "Whatever text you want to put in the body";
String intentType = "text/html";
String mailToParse = "mailto:";
//start Intent
Intent variableName = new Intent(Intent.ACTION_SENDTO);
variableName.setType(intentType);
variableName.setData(Uri.parse(mailToParse));
variableName.putExtra(Intent.EXTRA_SUBJECT, subject);
variableName.putExtra(Intent.EXTRA_TEXT, body);
startActivity(variableName);
Dadurch kann der Benutzer auch seine bevorzugte E-Mail-App auswählen. Das Einzige, was Sie nicht tun dürfen, ist das Einstellen der E-Mail-Adresse des Empfängers.
Vielleicht sollten Sie folgendes versuchen: intent.setType("plain/text");
Ich habe es gefunden hier . Ich habe es in meiner App verwendet und es werden nur E-Mail- und Google Mail-Optionen angezeigt.
Benutze das:
boolean success = EmailIntentBuilder.from(activity)
.to("[email protected]")
.cc("[email protected]")
.subject("Error report")
.body(buildErrorReport())
.start();
verwenden Sie Build Gradle:
compile 'de.cketti.mailto:email-intent-builder:1.0.0'
benutze Anko - kotlin
context.email(email, subject, body)
Von Android-Entwicklerdokumente :
public void composeEmail(String[] addresses, String subject) {
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, subject);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto", email, null));
if (emailIntent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(Intent.createChooser(emailIntent, "Send Email..."));
} else {
Toast.makeText(context, "No apps can perform this action.", Toast.LENGTH_SHORT).show();
}
Verfassen Sie eine E-Mail im Telefon-E-Mail-Client:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" });
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail body");
startActivity(Intent.createChooser(intent, ""));
Die Verwendung von intent.setType("message/rfc822");
funktioniert, zeigt jedoch zusätzliche Apps an, die nicht notwendigerweise E-Mails verarbeiten (z. B. GDrive). Die Verwendung von Intent.ACTION_SENDTO
mit setType("text/plain")
ist am besten. Sie müssen jedoch setData(Uri.parse("mailto:"))
hinzufügen, um die besten Ergebnisse zu erzielen (nur E-Mail-Apps). Der vollständige Code lautet wie folgt:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("text/plain");
intent.setData(Uri.parse("mailto:[email protected]"));
intent.putExtra(Intent.EXTRA_SUBJECT, "Email from My app");
intent.putExtra(Intent.EXTRA_TEXT, "Place your email message here ...");
startActivity(Intent.createChooser(intent, "Send Email"));