0

I'm android novice and I implemented my first app to send email text. 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.

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 I appreciate any help.

Aex Sun
  • 337
  • 5
  • 13
  • 6
    where is your logcat? – M D Jan 05 '15 at 10:56
  • 1
    are you testing in emulator? If yes then first configure google account and try again. there might be notFound Exception this is because there is no account configure in emulator – Anjali Jan 05 '15 at 10:58
  • 1
    We can't help you anymore without logcat.. – M D Jan 05 '15 at 11:02
  • Try this link [Send auto email programmatically][1], is an answer to your question. [1]: http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-android-ap/2033124#2033124 – Cristian Sicilia Jan 05 '15 at 11:02
  • I'm testing in my S4 mobile mobile device. – Aex Sun Jan 05 '15 at 11:02
  • @AexSun Can you post your logcat stacktrace ??? – Code Geek Jan 05 '15 at 11:06
  • Can you post your pemissions as well? That seems to be the issue. – G_V Jan 05 '15 at 11:15
  • sorry what do you mean with the permissions? – Aex Sun Jan 05 '15 at 11:23
  • All android apps require permissions to go outside of their sandbox for security reasons, they should be in your app's manifest. Without permissions, certain actions will be blocked because your app does not have access to them, like using the internet or reading/writing to external storage. – G_V Jan 05 '15 at 11:35

3 Answers3

1

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);
Karthikeyan
  • 1,119
  • 1
  • 15
  • 33
  • It works but I dont know where to but the varaibles "emailaddress" and "message" in the "fromParts"? – Aex Sun Jan 05 '15 at 11:48
0

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();
      }
   }
akash yadav
  • 349
  • 4
  • 14
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();
        }
    }
}
PeterPan
  • 1
  • 1
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Rence Apr 23 '18 at 10:52