通过 Gmail 发送电子邮件 [英] Send email via gmail

查看:74
本文介绍了通过 Gmail 发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于发送电子邮件的代码

I have a code the fires intent for sending email

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL,
                new String[] { to });
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT, msg);
try {
    startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(Start.this,
                    "There are no email clients installed.",
                    Toast.LENGTH_SHORT).show();
}

但是当这个意图被触发时,我会在列表中看到很多项目,比如 sms 应用程序、gmail 应用程序、facebook 应用程序等等.

But when this intent is fired I see many item in the list like sms app , gmail app, facebook app and so on.

如何过滤并仅启用 Gmail 应用(或仅启用电子邮件应用)?

How can I filter this and enable only gmail app (or maybe just email apps)?

推荐答案

使用 android.content.Intent.ACTION_SENDTO (new Intent(Intent.ACTION_SENDTO);) 来仅获取电子邮件客户端列表,没有 facebook 或其他应用程序.只是电子邮件客户端.

Use android.content.Intent.ACTION_SENDTO (new Intent(Intent.ACTION_SENDTO);) to get only the list of e-mail clients, with no facebook or other apps. Just the email clients.

我不建议您直接访问电子邮件应用.让用户选择他最喜欢的电子邮件应用程序.不要束缚他.

I wouldn't suggest you get directly to the email app. Let the user choose his favorite email app. Don't constrain him.

如果您使用 ACTION_SENDTO,则 putExtra 无法向意图添加主题和文本.使用 Uri 添加主题和正文.

If you use ACTION_SENDTO, putExtra does not work to add subject and text to the intent. Use Uri to add the subject and body text.

Intent send = new Intent(Intent.ACTION_SENDTO);
String uriText = "mailto:" + Uri.encode("email@gmail.com") + 
          "?subject=" + Uri.encode("the subject") + 
          "&body=" + Uri.encode("the body of the message");
Uri uri = Uri.parse(uriText);

send.setData(uri);
startActivity(Intent.createChooser(send, "Send mail..."));

这篇关于通过 Gmail 发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆