0

I recently used the code of the first answer on this question for an app. To make it less hardcoded I made an EditText widget in Android Studio for the sender email address (and password) and extracted the information from it in my OnClick function by these two lines of code:

et_email_from = (EditText) findViewById (R.id.et_email_from);
final String user = et_email_from.getText().toString();

The problem is that once I click the button and the sendMail function is called, I can't change the sender address anymore. I mean I can, but the app will still send it from the first sender email address. Can someone show me how I can solve this problem? I think it has something to do with where in the code the connection is made with the server, but I'm new to coding in Java and I don't know where to find this code.

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Hi @ruben-troch , your problem is you are making your variable as final. Check my answer below it will help you – Sachin Rajput Apr 19 '18 at 18:14
  • The code you copied is full of these [common JavaMail mistakes](https://javaee.github.io/javamail/FAQ#commonmistakes). Try correcting them and see if it works better. Make sure you're using the official [JavaMail for Android](https://javaee.github.io/javamail/Android). Also, you don't need your own ByteArrayDataSource; [there's one in JavaMail](https://javaee.github.io/javamail/docs/api/javax/mail/util/ByteArrayDataSource.html). – Bill Shannon Apr 19 '18 at 20:46

1 Answers1

0

You need to make your String user as non-final.

in place of final String user = et_email_from.getText().toString(); //Dont use as final

use String user = et_email_from.getText().toString(); //use like this

then you will be able to send to different-different email (multiple users) Programatically.

Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29
  • But then I get the error: Variable is accessed within inner class. Needs to be declared final. I think this is because I make use of the variables inside a new AsyncTask(). I solved the problem now by assigning the variables inside the AsyncTask. I don't know if this is a good solution, but it works for now. –  Apr 20 '18 at 11:56