Connecting to Office 365 online mail with OAUTH 2.0 authentication always failed !

%3CLINGO-SUB%20id%3D%22lingo-sub-2164882%22%20slang%3D%22en-US%22%3EConnecting%20to%20Office%20365%20online%20mail%20with%20OAUTH%202.0%20authentication%20always%20failed%20!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2164882%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3E1%20Issue%20Description%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BThere%20is%20an%20connecting%20issue%20using%20OAuth%202.0%20to%20connect%20Office%20365%20mail%20server%20.%20I'm%20following%20the%26nbsp%3B%3CA%20title%3D%22guide%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fexchange%2Fclient-developer%2Flegacy-protocols%2Fhow-to-authenticate-an-imap-pop-smtp-application-by-using-oauth%23register-your-application%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3Eguide%3C%2FA%3E%26nbsp%3B%20to%20get%20access%20token%20and%20using%20the%20token%20to%20connect%20Exchange%20server%20.%20it%20always%20throw%20error%20about%26nbsp%3B%20authentication%20failed%20.%26nbsp%3B%20Is%20the%20permission%20configuration%20issue%20%3F%20The%20permission%20which%20I%20configured%20as%20below%26nbsp%3B%20(sensitive%20message%20has%20been%20hidden)%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Bland_Wu_0-1614235959175.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F257738iF12DEB85C7085486%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Bland_Wu_0-1614235959175.png%22%20alt%3D%22Bland_Wu_0-1614235959175.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3BCould%20you%20please%20give%20some%20help%20if%20you%20have%20some%20experience%20or%20meet%20the%20issue%20like%20this%20%3F%20Thanks%20!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E2%20Core%20Code%3C%2FSTRONG%3E%20(Java%20%2C%20using%20javamail%201.6.2)%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-java%22%3E%3CCODE%3EProperties%20props%20%3D%20System.getProperties()%3B%0A%20props.put(%22mail.imaps.auth.mechanisms%22%2C%22XOAUTH2%22)%3B%0A%20props.put(%22mail.imaps.port%22%2C%20%22993%22)%3B%0A%20props.put(%22mail.imaps.starttls.enable%22%2C%20%22true%22)%3B%0A%20props.put(%22mail.imaps.auth%22%2C%20%22true%22)%3B%0A%20props.put(%22mail.imaps.host%22%2C%22outlook.office365.com%22)%3B%0A%20props.setProperty(%22mail.transport.protocol%22%2C%20%22imaps%22)%3B%0A%20props.setProperty(%22mail.store.protocol%22%2C%20%22imaps%22)%3B%0A%20props.setProperty(%22mail.debug.auth%22%2C%22true%22)%3B%0A%20%20%20%20%20%20%20%20%0A%20Session%20session%20%3D%20Session.getInstance(props)%3B%20%20%0A%20IMAPStore%20store%20%3D%20(IMAPStore)session.getStore(%22imaps%22)%3B%0A%20String%20userEmail%20%3D%20%22****%22%3B%0A%20String%20accessToken%20%3D%20%22****%22%3B%20%0A%20store.connect(userEmail%2CaccessToken)%3B%20%20%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E3%20Error%20Message%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%20%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EDEBUG%3A%20getProvider()%20returning%20javax.mail.Provider%5BSTORE%2Cimaps%2Ccom.sun.mail.imap.IMAPSSLStore%2COracle%5D%0ADEBUG%20IMAPS%3A%20mail.imap.fetchsize%3A%2016384%0ADEBUG%20IMAPS%3A%20mail.imap.ignorebodystructuresize%3A%20false%0ADEBUG%20IMAPS%3A%20mail.imap.statuscachetimeout%3A%201000%0ADEBUG%20IMAPS%3A%20mail.imap.appendbuffersize%3A%20-1%0ADEBUG%20IMAPS%3A%20mail.imap.minidletime%3A%2010%0ADEBUG%20IMAPS%3A%20enable%20STARTTLS%0ADEBUG%20IMAPS%3A%20closeFoldersOnStoreFailure%0ADEBUG%20IMAPS%3A%20trying%20to%20connect%20to%20host%20%22outlook.office365.com%22%2C%20port%20993%2C%20isSSL%20true%0A*%20OK%20The%20Microsoft%20Exchange%20IMAP4%20service%20is%20ready.%20%5BSABLAEEAUABSADAAMwBDAEEAMAAwADEAMQAuAGEAcABjAHAAcgBkADAAMwAuAHAAcgBvAGQALgBvAHUAdABsAG8AbwBrAC4AYwBvAG0A%5D%0AA0%20CAPABILITY%0A*%20CAPABILITY%20IMAP4%20IMAP4rev1%20AUTH%3DPLAIN%20AUTH%3DXOAUTH2%20SASL-IR%20UIDPLUS%20MOVE%20ID%20UNSELECT%20CHILDREN%20IDLE%20NAMESPACE%20LITERAL%2B%0AA0%20OK%20CAPABILITY%20completed.%0ADEBUG%20IMAPS%3A%20AUTH%3A%20PLAIN%0ADEBUG%20IMAPS%3A%20AUTH%3A%20XOAUTH2%0ADEBUG%20IMAPS%3A%20protocolConnect%20login%2C%20host%3Doutlook.office365.com%2C%20user%3D****%40****.com%2C%20password%3D%3CNON-NULL%3E%0AA1%20AUTHENTICATE%20XOAUTH2%20dXNlcj1jY2NfYXNzaXN0YW50X3Vh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let.service()%20for%20servlet%20%5BdispatcherServlet%5D%20in%20context%20with%20path%20%5B%5D%20threw%20exception%20%5BRequest%20processing%20failed%3B%20nested%20exception%20is%20javax.mail.AuthenticationFailedException%3A%20AUTHENTICATE%20failed.%5D%20with%20root%20cause%0A%0Ajavax.mail.AuthenticationFailedException%3A%20AUTHENTICATE%20failed.%0A%20at%20com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java%3A732)%20~%5Bjavax.mail-1.6.2.jar%3A1.6.2%5D%0A%20at%20javax.mail.Service.connect(Service.java%3A366)%20~%5Bjavax.mail-api-1.6.2.jar%3A1.6.2%5D%0A%20at%20javax.mail.Service.connect(Service.java%3A246)%20~%5Bjavax.mail-api-1.6.2.jar%3A1.6.2%5D%0A%20at%20javax.mail.Service.connect(Service.java%3A267)%20~%5Bjavax.mail-api-1.6.2.jar%3A1.6.2%5D%0A%20at%20com.compony.service.impl.RestServiceImpl.connectOffice365Mail(RestServiceImpl.java%3A102)%20~%5Bclasses%2F%3Ana%5D%0A%20at%20com.compony.controller.IndexController.testEmail(IndexController.java%3A50)%20~%5Bclasses%2F%3Ana%5D%0A%20at%20com.compony.controller.IndexController%24%24FastClassBySpringCGLIB%24%24830877a4.invoke(%3CGENERATED%3E)%20~%5Bclasses%2F%3Ana%5D%0A%20at%20org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java%3A218)%20~%5Bspring-core-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.springframework.aop.framework.CglibAopProxy%24CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java%3A771)%20~%5Bspring-aop-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java%3A163)%20~%5Bspring-aop-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.springframework.aop.framework.CglibAopProxy%24CglibMethodInvocation.proceed(CglibAopProxy.java%3A749)%20~%5Bspring-aop-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java%3A119)%20~%5Bspring-context-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java%3A186)%20~%5Bspring-aop-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.springframework.aop.framework.CglibAopProxy%24CglibMethodInvocation.proceed(CglibAopProxy.java%3A749)%20~%5Bspring-aop-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.springframework.aop.framework.CglibAopProxy%24DynamicAdvisedInterceptor.intercept(CglibAopProxy.java%3A691)%20~%5Bspring-aop-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20com.compony.controller.IndexController%24%24EnhancerBySpringCGLIB%24%2487a3038.testEmail(%3CGENERATED%3E)%20~%5Bclasses%2F%3Ana%5D%0A%20at%20sun.reflect.NativeMethodAccessorImpl.invoke0(Native%20Method)%20~%5Bna%3A1.8.0_91%5D%0A%20at%20sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java%3A62)%20~%5Bna%3A1.8.0_91%5D%0A%20at%20sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java%3A43)%20~%5Bna%3A1.8.0_91%5D%0A%20at%20java.lang.reflect.Method.invoke(Method.java%3A498)%20~%5Bna%3A1.8.0_91%5D%0A%20at%20org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java%3A190)%20~%5Bspring-web-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java%3A138)%20~%5Bspring-web-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java%3A105)%20~%5Bspring-webmvc-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java%3A878)%20~%5Bspring-webmvc-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java%3A792)%20~%5Bspring-webmvc-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java%3A87)%20~%5Bspring-webmvc-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java%3A1040)%20~%5Bspring-webmvc-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java%3A943)%20~%5Bspring-webmvc-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java%3A1006)%20~%5Bspring-webmvc-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java%3A898)%20~%5Bspring-webmvc-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20javax.servlet.http.HttpServlet.service(HttpServlet.java%3A626)%20~%5Btomcat-embed-core-9.0.38.jar%3A4.0.FR%5D%0A%20at%20org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java%3A883)%20~%5Bspring-webmvc-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20javax.servlet.http.HttpServlet.service(HttpServlet.java%3A733)%20~%5Btomcat-embed-core-9.0.38.jar%3A4.0.FR%5D%0A%20at%20org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java%3A231)%20~%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java%3A166)%20~%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java%3A53)%20~%5Btomcat-embed-websocket-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java%3A193)%20~%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java%3A166)%20~%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java%3A123)%20~%5Bdruid-1.1.10.jar%3A1.1.10%5D%0A%20at%20org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java%3A193)%20~%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java%3A166)%20~%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java%3A100)%20~%5Bspring-web-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java%3A119)%20~%5Bspring-web-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java%3A193)%20~%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java%3A166)%20~%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java%3A93)%20~%5Bspring-web-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java%3A119)%20~%5Bspring-web-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java%3A193)%20~%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java%3A166)%20~%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java%3A201)%20~%5Bspring-web-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java%3A119)%20~%5Bspring-web-5.2.9.RELEASE.jar%3A5.2.9.RELEASE%5D%0A%20at%20org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java%3A193)%20~%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java%3A166)%20~%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java%3A202)%20~%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java%3A97)%20%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java%3A541)%20%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java%3A143)%20%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java%3A92)%20%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java%3A78)%20%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java%3A343)%20%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.coyote.http11.Http11Processor.service(Http11Processor.java%3A374)%20%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java%3A65)%20%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.coyote.AbstractProtocol%24ConnectionHandler.process(AbstractProtocol.java%3A868)%20%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.tomcat.util.net.NioEndpoint%24SocketProcessor.doRun(NioEndpoint.java%3A1590)%20%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java%3A49)%20%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java%3A1142)%20%5Bna%3A1.8.0_91%5D%0A%20at%20java.util.concurrent.ThreadPoolExecutor%24Worker.run(ThreadPoolExecutor.java%3A617)%20%5Bna%3A1.8.0_91%5D%0A%20at%20org.apache.tomcat.util.threads.TaskThread%24WrappingRunnable.run(TaskThread.java%3A61)%20%5Btomcat-embed-core-9.0.38.jar%3A9.0.38%5D%0A%20at%20java.lang.Thread.run(Thread.java%3A745)%20%5Bna%3A1.8.0_91%5D%20%20%3C%2FGENERATED%3E%3C%2FGENERATED%3E%3C%2FNON-NULL%3E%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2164882%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExchange%20Server%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOutlook%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Regular Visitor

1 Issue Description

       There is an connecting issue using OAuth 2.0 to connect Office 365 mail server . I'm following the guide  to get access token and using the token to connect Exchange server . it always throw error about  authentication failed .  Is the permission configuration issue ? The permission which I configured as below  (sensitive message has been hidden)

     

Bland_Wu_0-1614235959175.png

 

   Could you please give some help if you have some experience or meet the issue like this ? Thanks !

 

2 Core Code (Java , using javamail 1.6.2)

   

Properties props = System.getProperties();
 props.put("mail.imaps.auth.mechanisms","XOAUTH2");
 props.put("mail.imaps.port", "993");
 props.put("mail.imaps.starttls.enable", "true");
 props.put("mail.imaps.auth", "true");
 props.put("mail.imaps.host","outlook.office365.com");
 props.setProperty("mail.transport.protocol", "imaps");
 props.setProperty("mail.store.protocol", "imaps");
 props.setProperty("mail.debug.auth","true");
        
 Session session = Session.getInstance(props);		
 IMAPStore store = (IMAPStore)session.getStore("imaps");
 String userEmail = "****";
 String accessToken = "****"; 
 store.connect(userEmail,accessToken);		

   

3 Error Message

   

DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle]
DEBUG IMAPS: mail.imap.fetchsize: 16384
DEBUG IMAPS: mail.imap.ignorebodystructuresize: false
DEBUG IMAPS: mail.imap.statuscachetimeout: 1000
DEBUG IMAPS: mail.imap.appendbuffersize: -1
DEBUG IMAPS: mail.imap.minidletime: 10
DEBUG IMAPS: enable STARTTLS
DEBUG IMAPS: closeFoldersOnStoreFailure
DEBUG IMAPS: trying to connect to host "outlook.office365.com", port 993, isSSL true
* OK The Microsoft Exchange IMAP4 service is ready. [SABLAEEAUABSADAAMwBDAEEAMAAwADEAMQAuAGEAcABjAHAAcgBkADAAMwAuAHAAcgBvAGQALgBvAHUAdABsAG8AbwBrAC4AYwBvAG0A]
A0 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+
A0 OK CAPABILITY completed.
DEBUG IMAPS: AUTH: PLAIN
DEBUG IMAPS: AUTH: XOAUTH2
DEBUG IMAPS: protocolConnect login, host=outlook.office365.com, user=****@****.com, password=<non-null>
A1 AUTHENTICATE XOAUTH2 dXNlcj1jY2NfYXNzaXN0YW50X3VhdEBzeW5uZXguY29tAWF1dGg9QmVhcmVyIGV5SjBlWEFpT2lKS1YxUWlMQ0p1YjI1alpTSTZJbVJqT1RNeU1WbE1SMFpIYTFvemFUQTNTalJ3VWxwcGNFMU9lbUUwV1MxSWNpMVhValo1ZUhCWE0yTWlMQ0poYkdjaU9pSlNVekkxTmlJc0luZzFkQ0k2SW01UGJ6TmFSSEpQUkZoRlN6RnFTMWRvV0hOc1NGSmZTMWhGWnlJc0ltdHBaQ0k2SW01UGJ6TmFSSEpQUkZoRlN6RnFTMWRvV0hOc1NGSmZTMWhGWnlKOS5leUpoZFdRaU9pSm9kSFJ3Y3pvdkwyZHlZWEJvTG0xcFkzSnZjMjltZEM1amIyMGlMQ0pwYzNNaU9pSm9kSFJ3Y3pvdkwzTjBjeTUzYVc1a2IzZHpMbTVsZEM4NU56SmhNR1V5Tnkxak16VTFMVFJtWkRjdE9USXlOUzA1TWpZMll6aG1Nek15WVdNdklpd2lhV0YwSWpveE5qRTBNak16TURZMUxDSnVZbVlpT2pFMk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weXdfbVZ0bEJEWjJPcmFzWnFORF9XNUttb0ZTZGpVV1VBNjdDdVZQdkl1WkdCWTVFM2I3MDBGZTBCLWdDdnQzUDJSekxQOWsxbkFUTTZmQ09tYldpZDd2WklHaFlKU1czZHlJX0lkcV9Oa1d2Mk96NU9rLXNRNThNaXFiZzZqWlNYZUEzUTdKVXNuUFBBX2Z2dFVhLWUyV25RRFlZYktvcDl1WWdick9TM3NOYS1OajNXMkxvQnhWVU80NHVNZC0tak5TMEtuN2xjUkY5cW5NbEE5OFJCTE42V3I0clBYMGZtMjZrNG9MYjU4RFAycFZraG4tOW5MZFFwcXZkbTZ1cGJ1OEZabkd1NkNXVHcBAQ==
A1 NO AUTHENTICATE failed.
2021-02-25 14:09:38.935 ERROR 10416 --- [nio-9000-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is javax.mail.AuthenticationFailedException: AUTHENTICATE failed.] with root cause

javax.mail.AuthenticationFailedException: AUTHENTICATE failed.
	at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:732) ~[javax.mail-1.6.2.jar:1.6.2]
	at javax.mail.Service.connect(Service.java:366) ~[javax.mail-api-1.6.2.jar:1.6.2]
	at javax.mail.Service.connect(Service.java:246) ~[javax.mail-api-1.6.2.jar:1.6.2]
	at javax.mail.Service.connect(Service.java:267) ~[javax.mail-api-1.6.2.jar:1.6.2]
	at com.compony.service.impl.RestServiceImpl.connectOffice365Mail(RestServiceImpl.java:102) ~[classes/:na]
	at com.compony.controller.IndexController.testEmail(IndexController.java:50) ~[classes/:na]
	at com.compony.controller.IndexController$$FastClassBySpringCGLIB$$830877a4.invoke(<generated>) ~[classes/:na]
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:119) ~[spring-context-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at com.compony.controller.IndexController$$EnhancerBySpringCGLIB$$87a3038.testEmail(<generated>) ~[classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_91]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_91]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_91]
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) ~[tomcat-embed-core-9.0.38.jar:4.0.FR]
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.38.jar:4.0.FR]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123) ~[druid-1.1.10.jar:1.1.10]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_91]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_91]
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at java.lang.Thread.run(Thread.java:745) [na:1.8.0_91]		

 

0 Replies