0

I'm using javax.mail.Authenticator to send an Email but I'm got nothing. When I'm try to less secure my gmail I'm still not get an email. Don't know what I;m did wrong, but there are no error in my code.

I'm just remind something. Some time this code got an broken pipe error. But it very rarely.

This is my MainActivity.java

package com.example.yggdrasil.sendmail;

import android.os.Bundle;
import android.app.Activity;
import android.os.Environment;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.os.AsyncTask;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    private Button send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);


        send = (Button) findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                final GMailSender sender = new GMailSender("fxxxxxxxx@gmail.com",
                        "xxxxxxxxx");
                new AsyncTask<Void, Void, Void>() {
                    @Override public Void doInBackground(Void... arg) {
                        try {
                            sender.sendMail("This is Subject",
                                    "This is Body",
                                    "aaaaaaaaa",
                                    "xxxxxxxxx@gmail.com");
                        } catch (Exception e) {
                            Log.e("SendMail", e.getMessage(), e);
                            }
                        return null;
                    }
                    @Override

                    protected void onPostExecute(Void result) {

                        super.onPostExecute(result);
                        Toast.makeText(getApplicationContext(), "Email send", Toast.LENGTH_SHORT).show();

                    }
                }.execute();
                Toast.makeText(getApplicationContext(), "After Execute", Toast.LENGTH_SHORT).show();

            }
        });
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Hello World
  • 740
  • 5
  • 18
  • did you add valid email addresses in sendMail??? – Hemant Parmar Jan 08 '18 at 07:44
  • In your doInBackground method remove your return statement – Vikas singh Jan 08 '18 at 07:59
  • Email is correct. And when I'm remove return statement, Code error. – Hello World Jan 08 '18 at 09:23
  • You may want to check the sample code in this [thread](https://stackoverflow.com/a/14374639/5832311) which is referred in this [documentation](https://developer.android.com/reference/android/os/AsyncTask.html). You need to pass your `GMailSender` object in to an `AsyncTask`, and call `GMailSender#sendMail` during `doInBackground`. – abielita Jan 08 '18 at 15:30

0 Answers0