JohnDoe
JohnDoe

Reputation: 97

Android: Using Intent to send email - only offering email applications doesnt work

i am trying to create an intent activity that sends an email. Using

public void emailSend(View view){
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setData(Uri.parse("mailto:"));
        emailIntent.setType("plain/text");
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Nächstes Treffen");
        emailIntent.putExtra(Intent.EXTRA_EMAIL,adressListe);
        if (emailIntent.resolveActivity(getPackageManager()) != null){
            startActivity(emailIntent);
        }
    }

offers me more than just the email apps. Using

public void emailSend(View view){
        Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
        emailIntent.setData(Uri.parse("mailto:"));
        emailIntent.setType("plain/text");
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Nächstes Treffen");
        emailIntent.putExtra(Intent.EXTRA_EMAIL,adressListe);
        if (emailIntent.resolveActivity(getPackageManager()) != null){
            startActivity(emailIntent);
        }
    }

Nothing happens when i click the button. For

emailIntent.setType("plain/text");

is also tried

emailIntent.setType("messageage/rfc822");

and

emailIntent.setType("*/*");

all with varying results, but none only dislaying the email apps.

Do you have any idea how to solve this? Help would be greatly appreciated!

Thanks!

Upvotes: 3

Views: 5363

Answers (5)

Torkel Velure
Torkel Velure

Reputation: 1336

Finally after trying a variation of all these posts, I came up with this which works nicely:

fun sendEmail(context: Context, email: String, subject: String = "") {
    try {
        context.startActivity(
            Intent(Intent.ACTION_SENDTO).apply {
                data = (Uri.parse("mailto:$email"))
                    .buildUpon()
                    .appendQueryParameter(
                        "subject", subject
                    ).appendQueryParameter(
                        "to", email
                    )
                    .build()
            }
        )
    } catch (e: Exception) {
        Timber.e("failed to open mail client")
    }
}

Upvotes: 0

iramm
iramm

Reputation: 148

I see only email clients when using the next approach:

    Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
    emailIntent.setData(Uri.parse("mailto:"));
    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String [] {[email protected]}); 
    emailIntent.putExtra(Intent.EXTRA_SUBJECT,"Email subject"));
    Intent chooser = Intent.createChooser(emailIntent, "Mail to ..");
    if (emailIntent.resolveActivity(getPackageManager()) != null) {
        startActivity(chooser);
    }
    else
       //Do something if there's no Email client

Upvotes: 2

Geovanny Buitrago
Geovanny Buitrago

Reputation: 166

When I use intent android.content.Intent.ACTION_SENDTO doesn't work for me because it shows many apps, some apps are not email clients. I found this way and it works perfectly for me.

Intent testIntent = new Intent(Intent.ACTION_VIEW);  
Uri data = Uri.parse("mailto:?subject=" + "text subject" + "&body=" + "text content" + "&to=" + "[email protected]");  
testIntent.setData(data);  
startActivity(testIntent);

Upvotes: 2

This is code that I have working:

Intent intentMail = new Intent(Intent.ACTION_SEND);
intentMail.setType("message/rfc822");
intentMail.putExtra(Intent.EXTRA_EMAIL, new String[]{
                    "[email protected]" }); // the To mail.
intentMail.putExtra(Intent.EXTRA_SUBJECT, "Subject goes here");
intentMail.putExtra(Intent.EXTRA_TEXT, "Content goes here");

// now we have created the mail, lets try and send it.
try {
    startActivity(Intent.createChooser(intentMail, "Message to User to do what next"));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(Activity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}

If this does not work, let me know. Oh, do you have the error logs?

*edit, this is where I found my code, so doublicate. Source

Upvotes: 1

Tulsi
Tulsi

Reputation: 719

This would do the job!

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, ""));

Upvotes: -2

Related Questions