0

I'm trying to develop an app for sending emails, actually I just started studying java.

I created strings "email_address" and "pass" in my sharedpreferences so user can change them. Then I try to send email, using that data -->

  //Creating a new session
    session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                //Authenticating the password
                protected PasswordAuthentication getPasswordAuthentication() {

                    return new PasswordAuthentication(sharedPrefs.getString("email_address", "a"), sharedPrefs.getString("pass", "a")); }
            });

    try { <...>   

And then I got this :

Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String android.content.SharedPreferences.getString(java.lang.String, java.lang.String)' on a null object reference

Android Studio points me at this line -->

return new PasswordAuthentication(sharedPrefs.getString("email_address", "a"), sharedPrefs.getString("pass", "a")); }    

If I hardcode my gmail and password in that line, it works perfectly, but I need an option to change these (in case of user can't build this app from my source code). Please help, I already spent 3 hours browsing this site and androiddevelopers too. :(

me_yy
  • 11
  • 1
  • Did you initialize `sharedPrefs` by getting it from `context`? – AndiGeeky Dec 21 '16 at 11:49
  • Because you are learning Java, you have to know what is a null pointer and how to solve it: http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – adalpari Dec 21 '16 at 11:50
  • And as @AndiGeeky says, your `sharedPrefs` variable is not initialized. That is the problem. – adalpari Dec 21 '16 at 11:51
  • I've got this public class SendMail extends AsyncTask { SharedPreferences sharedPrefs; – me_yy Dec 21 '16 at 11:56
  • @adalPaRi I searched about this and it seems like it must be initialized in onCreate, but I don't have oncreate in this class. – me_yy Dec 21 '16 at 12:00
  • @me_yy you can initialize just before your `//Creating a new session`, but you need to have also the context. So, to help you, can you post all your code? Your AsyncTask is in a single file or is a subclass? – adalpari Dec 21 '16 at 12:06
  • @adalPaRi yes of course, http://pastebin.com/27v9040Y ( I just added onpreexecute method from this http://stackoverflow.com/questions/23596903/how-do-i-get-sharedpreferences-in-asynctask but this don't works anyway. – me_yy Dec 21 '16 at 12:15
  • @adalPaRi I just did this http://pastebin.com/u7i5YyX5 and it almost worked! But 'this' is marked red and it says : Error:(53, 112) error: incompatible types: SendMail cannot be converted to Context. // What is this?? O_o How do I get that code to work.. – me_yy Dec 21 '16 at 12:22
  • @me_yy best follow my answer – adalpari Dec 21 '16 at 12:29

2 Answers2

0

It really isn't recommended to hold any form of passwords in your sharedPreferences, if you're creating an application where you would like the user to choose where they want to send it from e.g. their own personal Gmail use Intents instead but if you are determined to send emails automatically, I would recommend setting up a Gmail SMTP email address to send it from and pass the username and password to your Authenticator through there. (Java mail API)

Email Intent Example: Send Email Intent

but if you would like to send an email automatically: Example of sending email using Java Mail API

Community
  • 1
  • 1
Bradley Wilson
  • 1,197
  • 1
  • 13
  • 26
0

Aswering from your comment in question:

First of all, this is doing nothing, due to variable is LOCAL inside the method:

@Override
protected void onPreExecute() {
    super.onPreExecute();
    //get sharedPreferences here
    SharedPreferences sharedPreferences = getSharedPreferences(PreferenceManager.getDefaultSharedPreferences(this);
}

Then, one solution could be, initialize SharedPreferences when initialize AsyncTask with the constructor:

public SendMail(Context context, String email, String subject, String sharedText1) {
    //Initializing variables
    Context context1 = context; // this line is doing nothing..
    this.email = email;
    this.subject = subject;
    this.sharedText1 = sharedText1;
    sharedPrefs = context.getSharedPreferences("MySharedPreferences", Context.MODE_PRIVATE); // add this line to initialize variable
}

And finally, you can acces to sharedPrefs variable without null pointer exception:

sharedPrefs.getString("email_address", "a")
adalpari
  • 3,052
  • 20
  • 39