I can not send email using android studio

-1

I am making a form that will be sent by email, I have already created the email from gmail to use in the application, I have already imported the necessary libraries, I have added the permissions in Manifest, the code does not display errors but also does not send the e- mail address. hereisthelayoutoftheformcalledrequests:

<?xmlversion="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="To"
    android:textColor="@android:color/white"
    />

<EditText
    android:id="@+id/etTO"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:background="@android:color/darker_gray"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Subject"
    android:textColor="@android:color/white"
    />

<EditText
    android:id="@+id/etSubject"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/darker_gray"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Message"
    android:textColor="@android:color/white"
    />

<EditText
    android:id="@+id/etMsg"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="top"
    android:inputType="textMultiLine"
    android:lines="5"
    android:background="@android:color/darker_gray"
    />

<Button
    android:id="@+id/send_email_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Send"
    />

My activity e-mail:

public class Send_email extends MainActivity {

Button btnSend;
EditText To;
EditText Subject;
EditText Message;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_send_email);

    To = (EditText) findViewById(R.id.etTO);
    Subject = (EditText) findViewById(R.id.etSubject);
    Message = (EditText) findViewById(R.id.etMsg);
    btnSend = (Button) findViewById(R.id.send_email_button);


    btnSend.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String to = To.getText().toString();
            String subject = Subject.getText().toString();
            String message = Message.getText().toString();

            Intent email = new Intent(Intent.ACTION_SEND);
            email.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
            email.putExtra(Intent.EXTRA_SUBJECT, subject);
            email.putExtra(Intent.EXTRA_TEXT, message);
            email.setType("message/rfc822");
            startActivity(Intent.createChooser(email, "Select Email Client"));

        }
    });
}

When emulating the application on the cell phone and opening the form, it's as if you did not even have the code running, just fill in the fields. Can anyone tell me what I'm doing wrong or missing for my code to work?

    
asked by anonymous 23.05.2016 / 14:50

1 answer

1

Recently I was also dealing with this problem. I tried it in several ways and all by security did not work. I noticed that there would be plenty of work if I followed that line and there was a much easier way if I followed the protocol. Here is my resolution according to the protocol:

GlobalProperties gp = GlobalProperties.getInstance();
String email = "";
if (gp.getParametros().getString("EMAIL_DEFAULT") != null)
    email = gp.getParametros().getString("EMAIL_DEFAULT");

Date date = new Date();
StringBuilder msg = new  StringBuilder();

msg.append("Prezados,");
msg.append('\n' + '\n');
msg.append("  Segue em anexo base de dados " + dbname + " atualizada " );
msg.append("até a presente data ( " +  dateFormat.format(date)  + " )." + '\n' + '\n' );
msg.append("Atenciosamente,");


File sd = getActivity().getBaseContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS + "/backups" );
String backupDBPath = "BKP_" + dbname;
File backupDB = new File(sd, backupDBPath);

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(backupDB));
i.putExtra(Intent.EXTRA_EMAIL  , new String[]{ email });
i.putExtra(Intent.EXTRA_SUBJECT, "Backup Diário");
i.putExtra(Intent.EXTRA_TEXT   , (CharSequence) msg);
try {
    startActivity( Intent.createChooser(i, "Send mail..."));
       } 
catch (android.content.ActivityNotFoundException ex) 
     {
      Toast.makeText(getActivity(), "Não foi localizado software para o envio de email.", Toast.LENGTH_SHORT).show();
     }

See, in my case, I get the recipient's email in the parameters. I create the body of the email via StringBuilder. Attach to the email the file that is in the backups folder and send it by the email service that the user chooses that is installed on his cell phone. The sender will be the same as the one registered on your device.

    
23.05.2016 / 15:44