Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

So I am making an app which is almost finished. The only problem is that I want to be able to send a message from an app to a particular account. I thought the easiest way to do that is to send the message via e-mail using the gmail smtp server. I've already finished the code for doing that. The problem is that the e-mail is only sent from the app when I'm connected to a Cellular network (the internet you buy on your phone; Whatever you call it in English), and it doesn't work when I'm connected to a home wifi.

The java code to my app:

public class slanje_narudzbe extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    Button sendbutton;
    private static Context context;
    private Context mContext;
    private Session mSession;
    public static JavaMailAPI javaMailAPI;
    private String mEmail;
    private String mSubject;
    private String mMessage;

    private ProgressDialog mProgressDialog;
    private Spinner spinnertwo;
    String[] list = { "Izaberite na?in dostave:", "Li?na dostava(Sarajevo, Pale, Lukavica)", "Brza po?ta, Preporu?enom po?iljkom"};
    String sEmail, sPassword;
    //@RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slanje_narudzbe);


        sEmail = "mymail@gmail.com";
        sPassword = "mypass";
        context = this;
        spinnertwo = (Spinner) findViewById(R.id.spinner2);
        spinnertwo.setOnItemSelectedListener(this);
        ArrayAdapter<String> options = new ArrayAdapter<String>(this, R.layout.spinner_item, list);
        options.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spinnertwo.setAdapter(options);
        sendbutton = (Button) findViewById(R.id.send);
        sendbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
                    if(checkSelfPermission(Manifest.permission.INTERNET) == PackageManager.PERMISSION_GRANTED){
                        Toast.makeText(getApplicationContext(), "You have the permission", Toast.LENGTH_SHORT).show();
                        Properties properties = new Properties();
                        properties.put("mail.smtp.auth", "true");
                        properties.put("mail.smtp.starttls.enable", "true");
                        properties.put("mail.smtp.host", "smtp.gmail.com");
                        properties.put("mail.smpt.port", "587");

                        Session session = Session.getInstance(properties, new Authenticator() {
                            @Override
                            protected PasswordAuthentication getPasswordAuthentication() {
                                return new PasswordAuthentication(sEmail, sPassword);
                            }
                        });
                        try {
                            Message message = new MimeMessage(session);
                            message.setFrom(new InternetAddress((sEmail)));
                            message.setRecipients(Message.RecipientType.TO,
                                    InternetAddress.parse("everwhat31415@gmail.com"));
                            message.setSubject("Narudzba");
                            message.setText("odlicno");
                            new SendMail().execute(message);
                        } catch (MessagingException e) {
                            e.printStackTrace();
                        }
                    }else{
                        requestPermissions(new String[]{Manifest.permission.INTERNET}, 1);
                        Toast.makeText(getApplicationContext(), "No permission", Toast.LENGTH_SHORT).show();



                    }

            }
        });
    }
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }

    private class SendMail extends AsyncTask<Message, String, String> {
        private ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = ProgressDialog.show(slanje_narudzbe.this, "Please wait", "Sending email", true, false);
        }

        @Override
        protected String doInBackground(Message... messages) {
            try {
                Transport.send(messages[0]);
                return "success";
            } catch (MessagingException e) {
                e.printStackTrace();
                return "error";
            }
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            progressDialog.dismiss();
            if(s.equals(("success"))){
                AlertDialog.Builder builder = new AlertDialog.Builder(slanje_narudzbe.this);
                builder.setCancelable(false);
                builder.setTitle(Html.fromHtml("<font color='#509324>Success</font>"));
                builder.setMessage("Mail sent succesfully");
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        dialog.dismiss();
                    }
                });
                builder.show();
            }else{
                Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

Do I have to setup the server first online or something cause I haven't done that. Also I thought the problem may be in the ports but I've heard that these ports should work and I've tried using the app on multiple wifi networks and it didn't work. It only works when I pay the internet on my phone and turn on mobile data.

Also if you now a better way to send a message from an app than that would be useful info too

question from:https://stackoverflow.com/questions/65938370/smtp-server-in-android-app-not-working-when-connected-to-wifi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
80 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...