如何使ImageButton在点击时直接打开电子邮件作曲家?

时间:2011-06-21 06:40:52

标签: android email imagebutton

我已经为epaper / emagazine制作了一个应用程序,其中我想为电子邮件作曲家提供一个图像按钮,如果我点击该按钮,它将直接打开电子邮件作曲家,将该页面的所有数据插入到电子邮件作曲家邮件正文中只询问收件人地址。

我有输出但不是电子邮件,而是打开弹出列表询问消息和蓝牙。

这是我的代码:

final Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "lets.think.android@gmail.com" });
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "App Error Report");
emailIntent.putExtra(Intent.EXTRA_TEXT, "stacktrace");
activity(Intent.createChooser(emailIntent, "Send error report..."));

2 个答案:

答案 0 :(得分:12)

点击按钮时调用sendEmail()方法:

final Context context = getApplicationContext();    
Button button = (Button) findViewById(R.id.openpdfbutton);          
button.setOnClickListener(new OnClickListener() {               
    public void onClick(View arg0) {
        sendEmail(context, new String[]{"abc@xyz.com"}, "Sending Email",
                  "Test Email", "I am body");
    }
});

定义sendEmail()方法:

public static void sendEmail(Context context, String[] recipientList,
            String title, String subject, String body) {
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);    
    emailIntent.setType("plain/text");    
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipientList);
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);   
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
    context.startActivity(Intent.createChooser(emailIntent, title));
}

并在AndroidManifest.xml文件中设置权限:

<uses-permission android:name="android.permission.INTERNET" />

答案 1 :(得分:0)

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT   , "body of email");
try {
    startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}

使用意图我们不需要许可..