3

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!

JohnDoe
  • 77
  • 2
  • 12
  • 1
    Did you try without using the `setType`? – Prerak Sola Jan 02 '17 at 16:00
  • 3
    `plain/text` is not a valid MIME type. Use your second approach as a starting point, but get rid of `EXTRA_EMAIL` and put that address in the `Uri` (as the `...` in `mailto:...`). Bear in mind that users might not have access to an email program that supports this, so you need to have an `else` block if `resolveActivity()` returns `null`. – CommonsWare Jan 02 '17 at 16:04
  • You can also check https://developer.android.com/guide/components/intents-common.html Under heading **Email** – Raghunandan Jan 02 '17 at 16:10
  • @PrerakSola yes I did. – JohnDoe Jan 02 '17 at 16:37
  • @CommonsWare how would I add it? `Intent emailIntent = new Intent(Intent.ACTION_SENDTO); emailIntent.setData(Uri.parse("mailto:"+adressListe)); emailIntent.setType("message/rfc822");` Does not do anything. – JohnDoe Jan 02 '17 at 16:37
  • @JohnDoe: Most likely, you do not have an email app that supports that particular `Intent` structure. You can confirm this by adding an `else` block to handle the case where `resolveActivity()` returns `null`. In general, you should allow the user to [share where the user wants](https://commonsware.com/blog/2011/06/28/share-where-the-user-wants.html), rather than trying to artificially constrain the user. – CommonsWare Jan 02 '17 at 16:44
  • @CommonsWare i did that , letting a toast being displayed. Just as you said the `resolveActivity()` does return `null` . But should not at least the Gmail App support this intent structure? I do understand the concept about letting the user choose. As a matter of fact I am trying to create a little app for one of my family members though which would probably find it way easier if as few as possible and only email apps would show up. – JohnDoe Jan 04 '17 at 09:42
  • @JohnDoe: "But should not at least the Gmail App support this intent structure?" -- I have no idea, sorry. – CommonsWare Jan 04 '17 at 12:16
  • @CommonsWare thanks for your help in any case finding out about the problem ! :) – JohnDoe Jan 06 '17 at 10:00

5 Answers5

1

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[]{
                    "mailto@email.com" }); // 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

Community
  • 1
  • 1
  • Hi, thanks for the answer. I have everything as you with the difference that my adressListe i declared earlier `String [] adressListe = adressen.split(",");` with adressen being a String containing several addresses. However when i click on my button it offers me even oneDrive.... – JohnDoe Jan 02 '17 at 16:44
1

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@example.com");  
testIntent.setData(data);  
startActivity(testIntent);
1

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 [] {someaddress@gmail.com}); 
    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
iramm
  • 113
  • 5
0

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")
    }
}
Torkel Velure
  • 638
  • 6
  • 11
-2

This would do the job!

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some@email.address" });
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail body");
startActivity(Intent.createChooser(intent, ""));
Tulsi
  • 699
  • 7
  • 14
  • Think this is going in sort of the right direction. At least now i am offered even to directly send an email to one of my frequently contacted contacts. But still offered "save in google drive" and stuff..... – JohnDoe Jan 02 '17 at 16:48