简体   繁体   中英

sending email in android app

So I'm trying to make it to where, when a user redeems a prize, He has to send me his email and the amount that he is redeeming. This is the code I have so far... This is my xml file.

    <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" />

And this is my 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"));
        }

    });
}

}

After I run my app, When I click the button it says that it is unsupported. Anyone know what I am missing?

Use .setType("message/rfc822") and try-catch block to avoid error

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

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM