如何在安卓手机上打开Gmail

7

我想从我的应用程序打开 Gmail 应用,并希望从我的应用程序中设置电子邮件、主题和消息。

我尝试过 GmailService,但它不支持密送或抄送电子邮件。 链接: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();

我希望能够使用抄送和密送功能,并附加附件、主题和消息。


这个问题是关于如何在安卓中打开Gmail应用程序的... - deHaar
似乎yesidlazaro不再维护这个仓库了。有一个fork - Basi
可能是[发送电子邮件意图]的重复问题。 - AxelH
1
同时,将“抄送”、“密送”和“主题”字段添加到发送电子邮件的Android应用程序中的消息 - AxelH
6个回答

10

通过Intent打开Gmail

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

只需在意图参数中传递EXTRA_CCEXTRA_BCC

编辑

以下答案适用于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
编辑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..."))

9

// 适用于所有应用程序的电子邮件

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

这将使用用户的偏好,比强制应用程序要好得多! - AxelH

0

//这是用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();
            }

我收到了“Gmail应用程序未安装”的错误提示。 - K Pradeep Kumar Reddy
这里我定义了 Gmail 包名,所以它只会在我提到的 Gmail 中打开。@KPradeepKumarReddy - Toufiqul Haque Mamun
1
在 Android 11 中,除非我们在清单文件中添加 <queries> <!-- 与之交互的特定应用程序,例如: --> <package android:name="com.google.android.gm" /> </queries> ,否则此功能将无法正常工作。 - K Pradeep Kumar Reddy

0

// 这是为 Gmail 应用程序准备的

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

0

我正在使用这个来启动 Gmail 应用程序。

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

    if (intent != null) {
        startActivity(intent)
    }
    else{
        showToast("Sorry...You don't have gmail app")
    }

-1

我正在使用这个

Intent mailClient = new Intent(Intent.ACTION_VIEW);
mailClient.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivity");
startActivity(mailClient);

你也可以试试这个

final Intent intent = new Intent(Intent.ACTION_VIEW)
    .setType("plain/text")
    .setData(Uri.parse("test@gmail.com"))
    .setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail")
    .putExtra(Intent.EXTRA_EMAIL, new String[]{"test@gmail.com"})
    .putExtra(Intent.EXTRA_SUBJECT, "test")
    .putExtra(Intent.EXTRA_TEXT, "hello. this is a message sent from my demo app :-)");
startActivity(intent);

用于大量电子邮件:

intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "test@gmail.com" });

对于单个电子邮件:

intent.setData(Uri.parse("test@gmail.com"));

他在询问是否支持抄送和密送功能。 - A Honey Bustard

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接