发送纯文本到电子邮件android

时间:2016-07-07 15:43:09

标签: android mobile

我希望在按下按钮时将下面表格中输入的数据发送到myemail@gmail.com - 我该怎么做?

enter image description here

代码:

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


    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_EMAIL, "djalise@gmail.com");
    intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
    intent.putExtra(Intent.EXTRA_TEXT, "I'm email body.");

    startActivity(Intent.createChooser(intent, "Send Email"));


}

2 个答案:

答案 0 :(得分:0)

您需要使用setOnClickListener点击按钮。您还需要使用require_once('Zend/Mail/Transport/Smtp.php'); require_once 'Zend/Mail.php'; $config = array( 'ssl' => 'tls', 'auth' => 'login', 'username' => 'somemail@mysite.com', 'password' => 'somepass' ); $transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config); $mail = new Zend_Mail(); $mail->setBodyText('This is the text of the mail.'); $mail->setFrom('somemail@mysite.com', 'Some Sender'); $mail->addTo('somemail@mysite.com', 'Some Recipient'); $mail->setSubject('TestSubject'); $mail->send($transport); EditText获取文字。

要获取getText().toString()Button,您需要使用EditText,其中 your_id 与您在XML布局文件中为{{1}分配的ID相对应}和findViewById(R.id.your_id);(更多关于Button here)。需要在下面的代码中将ID更改为正确的ID。

EditText

答案 1 :(得分:0)

您的代码中有两个错误:

#1

您需要从EditText获取文字并将其转换为String,然后将此电子邮件Intent放入onClickListener的{​​{1}}内:

试试这个:

Button

第二个错误就在这里:

EditText yourEditText = (EditText) findViewById(R.id.yourEditTextId);
Button yourButton = (Button) findViewById(R.id. yourButton);
yourButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String enteredText = yourEditText.getText().toString();
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_EMAIL,new String[]{"myemail@gmail.com"}); //2nd mistake is here.
        intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        intent.putExtra(Intent.EXTRA_TEXT, enteredText);                      

        startActivity(Intent.createChooser(intent, "Send Email"));

    }
});

电子邮件意图需要intent.putExtra(Intent.EXTRA_EMAIL, "djalise@gmail.com"); (已记录here),但您提供的是String array,请使用此功能:

String

<强>更新

由于您要自动打开Gmail应用以发送电子邮件,请尝试以下操作:

intent.putExtra(Intent.EXTRA_EMAIL,new String[]{"myemail@gmail.com"}); 

即,您需要添加此额外行以将目标类名设置为Intent intent = new Intent(Intent.ACTION_SEND); intent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail"); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_EMAIL,new String[]{"myemail@gmail.com"}); intent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); intent.putExtra(Intent.EXTRA_TEXT, "hi"); startActivity(Intent.createChooser(intent, "Send Email"));

ComposeActivityGmail