I can’t send email using android studio

Asked

Viewed 1,689 times

-1

I am making a form that will be sent by email, I have already created the email of gmail to use in the application, I have imported the necessary libraries, added permissions in Manifest, the code does not show errors but also does not send the email. erro here is the layout of the form called requests:

<?xml version="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 email Activity:

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 emulate the application on mobile and open the form, it’s like you haven’t even run the code, just fill in the fields. Can anyone tell me what I’m doing wrong or missing for my code to work?

1 answer

1


Recently I was also running around with this problem. I tried several ways and all on account of security did not work. I noticed that I would have a lot of work if I followed that line and there was a much easier way if I followed protocol. Below is my resolution according to 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 that, 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 by the email service that the user choose that is installed on his mobile. Sender will be the same as the one registered on your device.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.