2015-01-05 5 views
0

Я начинаю андроид, и я внедрил свое первое приложение для отправки текста электронной почты. Проблема в том, что если я нажимаю на кнопку, приложение разбивается, и я получаю сообщение об ошибке unfortunately the app has stopped Я думаю, что проблема заключается в методе onClick, но я не знаю, как это сделать.Как отправить электронную почту с Android?

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 Я признателен за любую помощь.

+6

где ваш LogCat? –

+1

Вы тестируете в эмуляторе? Если да, то сначала настройте учетную запись google и повторите попытку. там может быть notFound Exception, потому что в эмуляторе нет учетной записи configure – Anjali

+1

Мы больше не можем помочь вам без logcat. –

ответ

0

использование

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

Это работает, но я не знаю, где, но вариабельные «emailaddress» и «message» в «fromParts»? –

+0

@Aex Sun см. Мой обновленный ответ. – Karthikeyan

+0

Сейчас он работает спасибо большое :) –

0

Вот код

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

     String[] TO = {"[email protected]"}; 
     String[] CC = {"[email protected]"}; 
     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(); 
     } 
    } 
Смежные вопросы