Results 1 to 2 of 2
  1. #1
    creali is offline Member
    Join Date
    Aug 2014
    Posts
    2
    Rep Power
    0

    Default JavaMail - OAuth connexion failed

    Hello,

    I tryed to connect oAuth2 with gmail.

    In command line, no problem. But with Javamail it failed.

    The source of the exemple are here : https://code.google.com/p/google-mai...es/oauth2/?r=3

    Last modified is in 2012 then, maybe, this code is obsolete.

    In command line it works fine :
    Java Code:
    openssl s_client -crlf -connect imap.gmail.com:993
    A002 AUTHENTICATE XOAUTH2
    +
    my_token_who_end_with__
    Response : A002 OK myaddress@gmail.com ForName Name authenticated (Success)
    Did one of you know what is the problem ?

    For information :
    - it failed with token and refresh token
    - here are the logs :
    Java Code:
    DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,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: disable AUTH=LOGIN
    DEBUG IMAPS: disable AUTH=PLAIN
    DEBUG IMAPS: enable SASL
    DEBUG IMAPS: SASL mechanisms allowed: XOAUTH2
    DEBUG IMAPS: trying to connect to host "imap.gmail.com", port 993, isSSL true
     * OK Gimap ready for requests from 195.6.245.82 i1mb40455339wjn
    A0 CAPABILITY
     * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN
    A0 OK Thats all she wrote! i1mb40455339wjn
    DEBUG IMAPS: AUTH: XOAUTH
    DEBUG IMAPS: AUTH: XOAUTH2
    DEBUG IMAPS: AUTH: PLAIN
    DEBUG IMAPS: AUTH: PLAIN-CLIENTTOKEN
    DEBUG IMAPS: protocolConnect login, host=imap.gmail.com, user=myaddress@gmail.com, password=<non-null>
    DEBUG IMAPS: SASL authentication command trace suppressed
    DEBUG IMAPS: SASL Mechanisms:
    DEBUG IMAPS:  XOAUTH2
    DEBUG IMAPS:
    DEBUG IMAPS: SASL client XOAUTH2
    DEBUG IMAPS: SASL callback length: 1
    DEBUG IMAPS: SASL callback 0: javax.security.auth.callback.NameCallback@45258dfd
    DEBUG IMAPS: SASL no response
    Exception : -->javax.mail.AuthenticationFailedException: [ALERT] Invalid credentials (Failure)      at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:715)      at javax.mail.Service.connect(Service.java:364)   at com.myproject.MailboxHelper.testMailboxWithOAuth(MailboxHelper.java:179)
    Best regards

  2. #2
    creali is offline Member
    Join Date
    Aug 2014
    Posts
    2
    Rep Power
    0

    Default Re: JavaMail - OAuth connexion failed

    Finally it works with :

    Java Code:
    Properties props = new Properties();
    props.put("mail.imaps.ssl.enable", "true");
    props.put("mail.imaps.sasl.enable", "true");
    props.put("mail.imaps.sasl.mechanisms", "XOAUTH2");
    props.put("mail.imaps.auth.login.disable", "true");
    props.put("mail.imaps.auth.plain.disable", "true");
    props.put("mail.imaps.socketFactory", settings.getPort());
    props.put("mail.imaps.port", settings.getPort());
    props.put("mail.imaps.socketFactory.class" , "javax.net.ssl.SSLSocketFactory");
    props.put(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, oAuthToken);
     
    Session session = Session.getInstance(props, null);
    session.setDebug(true);
     
    Security.addProvider(new OAuth2Provider());
    Store store  = session.getStore("imaps");
    store.connect(settings.getHost(), settings.getPort(), address, oAuthToken);
    With oAuthToken, not refresh token.

Similar Threads

  1. Replies: 0
    Last Post: 07-18-2013, 02:21 AM
  2. Replies: 0
    Last Post: 07-15-2013, 08:35 PM
  3. JavaMail API
    By eponcedeleon in forum Advanced Java
    Replies: 1
    Last Post: 02-28-2010, 12:39 AM
  4. JavaMail Jar
    By rummy in forum Advanced Java
    Replies: 1
    Last Post: 01-21-2010, 03:51 PM
  5. connexion
    By thethunder in forum NetBeans
    Replies: 1
    Last Post: 12-14-2009, 11:15 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •