简体   繁体   English

如何使用android发送电子邮件?

[英]How to send email with android?

I'm android novice and I implemented my first app to send email text. 我是android新手,我实现了我的第一个应用程序来发送电子邮件文本。 The problem is if I click on the button, the app is being crashed and I'm getting the error unfortunately the app has stopped I think the problem is in the onClick method but I do not how to manage it. 问题是,如果我单击按钮,则应用程序崩溃了,很unfortunately the app has stopped我收到错误消息,但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();
    }

}

在此处输入图片说明 I appreciate any help. 感谢您的帮助。

use 采用

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

instead 代替

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

Here is the code 这是代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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