7

I just wanted to open the Gmail app through my app and wanted to set email, subject and message from my application.

I have tried GmailService but it is not supporting bcc or cc emails. Link: https://github.com/yesidlazaro/GmailBackground

BackgroundMail.newBuilder(this)
    .withUsername("username@gmail.com")
    .withPassword("password12345")
    .withMailto("toemail@gmail.com")
    .withType(BackgroundMail.TYPE_PLAIN)
    .withSubject("this is the subject")
    .withBody("this is the body")
    .withOnSuccessCallback(new BackgroundMail.OnSuccessCallback() {
        @Override
        public void onSuccess() {
            //do some magic
        }
    }).withOnFailCallback(new BackgroundMail.OnFailCallback() {
        @Override
        public void onFail() {
            //do some magic
        }
    }).send();

I would like to use bcc and cc functionality along with the attachment, subject, and message.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Faran Tariq
  • 153
  • 1
  • 8
  • [This question](https://stackoverflow.com/questions/3470042/intent-uri-to-launch-gmail-app) is about how to open gmail in android... – deHaar Apr 25 '19 at 06:23
  • It seems that [yesidlazaro](https://github.com/yesidlazaro/GmailBackground) did not take care this repo anymore. there is a [fork](https://github.com/luongvo/GmailBackground) – Basi Apr 25 '19 at 06:37
  • Possible duplicate of [Send Email Intent](https://stackoverflow.com/questions/8701634/send-email-intent) – AxelH Apr 25 '19 at 07:33
  • 1
    And also [Adding Cc, Bcc and Subject fields to a message in an email sending Android app](https://stackoverflow.com/questions/6234905/adding-cc-bcc-and-subject-fields-to-a-message-in-an-email-sending-android-app) – AxelH Apr 25 '19 at 07:35

5 Answers5

9

open gmail via Intent

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("abc@gmail.com"));
intent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
intent.putExtra(Intent.EXTRA_CC, new String[]{"xyz@gmail.com"});
intent.putExtra(Intent.EXTRA_BCC, new String[]{"pqr@gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "your subject goes here...");
intent.putExtra(Intent.EXTRA_TEXT, "Your message content goes here...");
    
startActivity(intent);

just pass EXTRA_CC & EXTRA_BCC in intent argument

Edit

Below answer will work on android 11

Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"abc@gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Your subject here...");
intent.putExtra(Intent.EXTRA_TEXT,"Your message here...");
startActivity(intent);

Edit 2

val selectorIntent = Intent(Intent.ACTION_SENDTO)
selectorIntent.data = Uri.parse("mailto:")

val emailIntent = Intent(Intent.ACTION_SEND)
emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf("recipient@mail.com"))
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject here...")
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email Body...")
emailIntent.selector = selectorIntent

activity!!.startActivity(Intent.createChooser(emailIntent, "Send email..."))
Priyanka
  • 3,369
  • 1
  • 10
  • 33
9

// For Email by Any app

Intent email= new Intent(Intent.ACTION_SENDTO);
                email.setData(Uri.parse("mailto:your.email@gmail.com"));
                email.putExtra(Intent.EXTRA_SUBJECT, "Subject");
                email.putExtra(Intent.EXTRA_TEXT, "My Email message");
                startActivity(email);
touhid udoy
  • 4,005
  • 2
  • 18
  • 31
Rahul Khatri
  • 1,502
  • 2
  • 13
  • 25
0

// This is for Gmail App

Intent email= new Intent(Intent.ACTION_VIEW);
            email.setType("message/rfc822")
            .setData(Uri.parse("mailto:your.email@gmail.com"))
            .putExtra(Intent.EXTRA_EMAIL, "your.email@gmail.com")
            .putExtra(Intent.EXTRA_SUBJECT, "Subject")
            .putExtra(Intent.EXTRA_TEXT, "My Email message")
            .setPackage("com.google.android.gm");
            startActivity(email);
Rahul Khatri
  • 1,502
  • 2
  • 13
  • 25
0

I m using this to launch gmail app.

val intent: Intent? = activity.packageManager.getLaunchIntentForPackage("com.google.android.gm")

    if (intent != null) {
        startActivity(intent)
    }
    else{
        showToast("Sorry...You don't have gmail app")
    }
Yasham Ihsan
  • 46
  • 1
  • 7
0

//This is open with gmail

Intent i = new Intent(Intent.ACTION_SENDTO);
            i.setType("text/plain");
            i.setData(Uri.parse("mailto:"));
            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@gmail.com"});
            i.putExtra(Intent.EXTRA_SUBJECT, "Mail Subject");
            i.putExtra(Intent.EXTRA_TEXT   , "massage");
            i.setPackage("com.google.android.gm");
            try {
                startActivity(Intent.createChooser(i, "Send mail..."));
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(AnotherActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
            }