如何用android发送电子邮件?

时间:2015-01-05 10:54:03

标签: android

我是android新手,我实现了我的第一个应用程序来发送电子邮件文本。问题是,如果我点击按钮,应用程序崩溃了,我收到错误unfortunately the app has stopped我认为问题出在onClick方法中,但我不知道如何管理它。

package com.test;

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

public class Email extends Activity implements View.OnClickListener {

    EditText personsEmail, intro, personsName, stupidThings, hatefulAction,
            autro;
    String emailAdd, beginning, name, stupidAction, hatefulAct, out;
    Button sendEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.email);
        initializeVars();
        sendEmail.setOnClickListener(this);

    }

    private void initializeVars() {
        // TODO Auto-generated method stub


        personsEmail = (EditText) findViewById(R.id.etEmails);
        intro = (EditText) findViewById(R.id.etIntro);
        personsName = (EditText) findViewById(R.id.etName);
        stupidThings = (EditText) findViewById(R.id.etStupidTHings);
        hatefulAction = (EditText) findViewById(R.id.ethatefulAction);
        autro = (EditText) findViewById(R.id.etAuto);

        sendEmail = (Button) findViewById(R.id.bSendEmail);

    }

    @Override
    public void onClick(View v) {
        //TODO Auto-generated method stub
        convertEditTextVarsToString();
        String emailaddress[] = {emailAdd};
        String message = "Well hello"
                +name
                +"I just wanted to say"
                +beginning
                +". Not only I hate when you"
                + stupidAction
                +", that just really makes me crazy. I just want to make you"
                +hatefulAct
                +". Welp, thats all I wanted to chit-chatter about, oh"
                +out
                +". Oh also if you get bored you should check out"
                +'\n'+"PS. I think I love you.......";


         Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
         emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress);
         emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
         startActivity(emailIntent);

    }

    private void convertEditTextVarsToString() {
        // TODO Auto-generated method stub
        emailAdd = personsEmail.getText().toString();
        beginning = intro.getText().toString();
        name = personsName.getText().toString();
        stupidAction = stupidThings.getText().toString();
        hatefulAct = hatefulAction.getText().toString();
        out = autro.getText().toString();
    }

    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }

}

enter image description here 我感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

使用

Intent emailIntent = new Intent(android.content.Intent.ACTION_SENDTO,Uri.fromParts("mailto", "", null));
     emailIntent.putExtra(Intent.EXTRA_EMAIL, emailaddress);
     emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
     emailIntent.putExtra(Intent.EXTRA_TEXT,message);
     startActivity(emailIntent);

代替

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
     emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress);
     emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
     startActivity(emailIntent);

答案 1 :(得分:0)

这是代码

protected void sendEmail() {
      Log.i("Send email", "");

      String[] TO = {"amrood.admin@gmail.com"};
      String[] CC = {"mcmohd@gmail.com"};
      Intent emailIntent = new Intent(Intent.ACTION_SEND);
      emailIntent.setData(Uri.parse("mailto:"));
      emailIntent.setType("text/plain");


      emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
      emailIntent.putExtra(Intent.EXTRA_CC, CC);
      emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
      emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");

      try {
         startActivity(Intent.createChooser(emailIntent, "Send mail..."));
         finish();
         Log.i("Finished sending email...", "");
      } catch (android.content.ActivityNotFoundException ex) {
         Toast.makeText(MainActivity.this, 
         "There is no email client installed.", Toast.LENGTH_SHORT).show();
      }
   }

答案 2 :(得分:0)

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button startBtn = (Button) findViewById(R.id.button);
        startBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                sendEmail();
            }
        });
    }

    protected void sendEmail() {
        Log.i("Send email", "");
        String[] TO = {""};
        String[] CC = {""};
        Intent emailIntent = new Intent(Intent.ACTION_SEND);

        emailIntent.setData(Uri.parse("mailto:"));
        emailIntent.setType("text/plain");
        emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
        emailIntent.putExtra(Intent.EXTRA_CC, CC);
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
        emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");

        try {
            startActivity(Intent.createChooser(emailIntent, "Send mail..."));
            finish();
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(MainActivity.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
        }
    }
}