gpt4 book ai didi

java - 在 Android 应用程序中发送电子邮件

转载 作者:行者123 更新时间:2023-12-01 10:14:57 24 4
gpt4 key购买 nike

所以我试图做到这一点,当用户兑换奖品时,他必须向我发送他的电子邮件和他兑换的金额。这是我到目前为止的代码......这是我的 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"));
}

});
}

}

运行我的应用程序后,当我单击按钮时,它说它不受支持。有人知道我缺少什么吗?

最佳答案

使用.setType("message/rfc822")和try-catch block 来避免错误

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

});

关于java - 在 Android 应用程序中发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35954185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com