在Android应用中发送电子邮件

时间:2016-03-12 05:33:36

标签: java android

所以我试图把它带到哪里,当用户兑换奖品时,他必须向我发送他的电子邮件和他正在兑换的金额。这是我到目前为止的代码...... 这是我的xml文件。

    <Button
    tools:ignore="HardcodedText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="$1                     (100K)"
    android:drawableLeft="@drawable/amazon"
    android:drawableStart="@drawable/amazon"
    android:id="@+id/button7"
    android:textSize="35sp"
    android:layout_gravity="center_horizontal" />

这是我的activity.java

    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;


    public class AmazonActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.amazon_layout);

    Button mEmail = (Button) findViewById(R.id.button7);
    mEmail.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
        // open email client using intent
            Intent emailIntent = new Intent(Intent.ACTION_SENDTO,
                    Uri.fromParts("mailto", "pepsiass666@gmail.com",null));
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Welcome To  My App");
            emailIntent.putExtra(Intent.EXTRA_TEXT, "Body Of Email");
            startActivity(Intent.createChooser(emailIntent, "Send Email"));
        }

    });
}

}

运行我的应用程序后,当我单击该按钮时,它表示它不受支持。谁知道我错过了什么?

1 个答案:

答案 0 :(得分:1)

使用.setType(&#34; message / rfc822&#34;)和try-catch块以避免错误

 mEmail.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {

    String[] emails = {"pepsiass666@gmail.com"};
                        String subject = "Welcome To  My App";
                        String message = "Body Of Email";

                        Intent email = new Intent(Intent.ACTION_SEND);
                        email.putExtra(Intent.EXTRA_EMAIL, emails);
                        email.putExtra(Intent.EXTRA_SUBJECT, subject);
                        email.putExtra(Intent.EXTRA_TEXT, message);

                        // need this to prompts email client only
                        email.setType("message/rfc822");

        try
        {
                        startActivity(Intent.createChooser(email, "Choose an Email client :"));
        }
         catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(MyActivity.this, "No Email application", Toast.LENGTH_SHORT).show();
        }
}

    });