I am trying to use an intent to send an email from my application but the To field of the email will not populate. If I add code to fill in the subject or text, they work fine. Just the To field will not populate.
I have also tried changing the type to "text/plain" and "text/html" but I get the same problem. Can anyone help please?
public void Email(){
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("message/rfc822"); //set the email recipient
String recipient = getString(R.string.IntegralEmailAddress);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL , recipient);
//let the user choose what email client to use
startActivity(Intent.createChooser(emailIntent, "Send mail using...")); }
The email client I'm trying to use is Gmail
I think you are not passing recipient
as array of string
it should be like
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { "[email protected]" });
In Kotlin - Android
fun sendMail(
activity: Activity,
emailIds: Array<String>,
subject: String,
textMessage: String
) {
val emailIntent = Intent(Intent.ACTION_SEND)
emailIntent.type = "text/plain"
emailIntent.putExtra(Intent.EXTRA_EMAIL, emailIds)
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
emailIntent.putExtra(Intent.EXTRA_TEXT, textMessage)
emailIntent.setType("message/rfc822")
try {
activity.startActivity(
Intent.createChooser(
emailIntent,
"Send email using..."
)
)
} catch (ex: ActivityNotFoundException) {
Toast.makeText(
activity,
"No email clients installed.",
Toast.LENGTH_SHORT
).show()
}
}
Also you can use [
val emailIntent = Intent(Intent.ACTION_SENDTO)
] to invoke direct email client
//argument of function
val subject = "subject of you email"
val eMailMessageTxt = "Add Message here"
val eMailId1 = "[email protected]"
val eMailId2 = "[email protected]"
val eMailIds: Array<String> = arrayOf(eMailId1,eMailId2)
//Calling function
sendMail(this, eMailIds, subject, eMailMessageTxt)
I hope this code snippet will help to kotlin developers.
Use this
public void Email(){
// use this to declare your 'recipient' string and get your email recipient from your string xml file
Resources res = getResources();
String recipient = getString(R.string.IntegralEmailAddress);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("message/rfc822"); //set the email recipient
emailIntent.putExtra(Intent.EXTRA_EMAIL, recipient);
//let the user choose what email client to use
startActivity(Intent.createChooser(emailIntent, "Send mail using..."));
``}
This will work :)
This is what android documentation says about Intent.Extra_Email
-A string array of all "To" recipient email addresses.
So you should feed string properly
You can read more over here
http://developer.android.com/guide/components/intents-common.html#Email
and here http://developer.android.com/guide/topics/resources/string-resource.html Or use the ACTION_SENDTO action and include the "mailto:" data scheme. For example:
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With