title: Javamail IMAP OAUTH2/SASL Exception
body: <p>I am currently working on a IMAP Messenger, I am trying to get the users google username and authorization token from the built in account manager, and keep getting errors based around SASL as my store tries to connect. </p>
  
  <p>Here is my current code:</p>
  
  <pre><code>private class logIn extends AsyncTask&lt;String,Void,String&gt;{
      ProgressDialog progressDialog;
      @Override
      protected String doInBackground(String... params){
          // Get Username &amp;&amp; Auth Token
          AccountManager accountManager=AccountManager.get(getActivity());
          Account[] accounts=accountManager.getAccountsByType("com.google");
          if(accounts[0].name.toLowerCase().endsWith("gmail.com")){
              username=accounts[0].name.toLowerCase();
              System.out.println(username);
              accountManager.getAuthToken(accounts[0],"oauth2:https://mail.google.com/",null,getActivity(),new onPasswordAcquired(),null);
          }
          // Start Store Session
          if(password!=null){
              Properties props = new Properties();
              props.put("mail.imap.ssl.enable","true");
              props.put("mail.imap.sasl.enable","true");
              props.put("mail.imap.sasl.mechanisms","XOAUTH2");
              props.put("mail.imap.auth.login.disable","true");
              props.put("mail.imap.auth.plain.disable","true");
              try{
                  Session session=Session.getDefaultInstance(props,new Gmail_Authenticator(username,password));
                  session.setDebug(true);
                  store=session.getStore("imap");
                  store.connect("imap.gmail.com",username,password);
              }catch(MessagingException e){e.printStackTrace();}
              if(store!=null &amp;&amp; store.isConnected()){loadmsg=true;}
          }
          return "Executed";
      }
  </code></pre>
  
  <p>Here is the Error I am getting:</p>
  
  <pre><code>12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime: Process: com.example.wq.myapp, PID: 10711
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at android.os.AsyncTask$3.done(AsyncTask.java:304)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:  Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/security/sasl/Sasl;
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at com.sun.mail.imap.protocol.IMAPSaslAuthenticator.authenticate(IMAPSaslAuthenticator.java:143)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at com.sun.mail.imap.protocol.IMAPProtocol.sasllogin(IMAPProtocol.java:846)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at com.sun.mail.imap.IMAPStore.login(IMAPStore.java:805)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:703)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at javax.mail.Service.connect(Service.java:364)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at javax.mail.Service.connect(Service.java:245)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at com.example.wq.myapp.GmailMessagesFrag$logIn.doInBackground(GmailMessagesFrag.java:418)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at com.example.wq.myapp.GmailMessagesFrag$logIn.doInBackground(GmailMessagesFrag.java:394)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818) 
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:  Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.security.sasl.Sasl" on path: DexPathList[[zip file "/data/app/com.example.wq.myapp-2/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at com.sun.mail.imap.protocol.IMAPSaslAuthenticator.authenticate(IMAPSaslAuthenticator.java:143) 
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at com.sun.mail.imap.protocol.IMAPProtocol.sasllogin(IMAPProtocol.java:846) 
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at com.sun.mail.imap.IMAPStore.login(IMAPStore.java:805) 
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:703) 
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at javax.mail.Service.connect(Service.java:364) 
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at javax.mail.Service.connect(Service.java:245) 
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at com.example.wq.myapp.GmailMessagesFrag$logIn.doInBackground(GmailMessagesFrag.java:418) 
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at com.example.wq.myapp.GmailMessagesFrag$logIn.doInBackground(GmailMessagesFrag.java:394) 
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at android.os.AsyncTask$2.call(AsyncTask.java:292) 
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818) 
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:   Suppressed: java.lang.ClassNotFoundException: javax.security.sasl.Sasl
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.lang.Class.classForName(Native Method)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:     at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:           ... 15 more
  12-16 18:56:22.836 10711-11216/com.example.wq.myapp E/AndroidRuntime:  Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
  </code></pre>
  
  <p>I have tried adding some libraries that included "javax.security" classes and get an error saying:</p>
  
  <pre><code>ill-advised or mistaken usage of a core class (java.* or javax.*)
  ...
  Error:Execution failed for task ':app:preDexDebug'.
  &gt; com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.7.0_79\bin\java.exe'' finished with non-zero exit value 1
  </code></pre>
  
  <p>Any help would be greatly appreciated as I have been struggling on this for a week now.</p>