Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication issues trying to access POP mailbox #614

Open
benjaminxie opened this issue Jul 20, 2022 · 2 comments
Open

Authentication issues trying to access POP mailbox #614

benjaminxie opened this issue Jul 20, 2022 · 2 comments
Labels
question Further information is requested

Comments

@benjaminxie
Copy link

benjaminxie commented Jul 20, 2022

Edit: I just realized that I'm using angus-mail:1.0.0 . I'm sorry if this is the wrong place to be posting my issue.

I'm trying to access my Office365 Outlook mailbox. I've attempted to authenticate via two-legged OAuth as well as just basic auth. Nonetheless, I hit similar errors. In the case of OAuth

jakarta.mail.AuthenticationFailedException: Authentication failure: unknown user name or bad password.

and in the case of basic auth

jakarta.mail.AuthenticationFailedException: Logon failure: unknown user name or bad password.

I'm guessing there's something simple that I'm doing incorrectly but I haven't been able to find any relevant code examples. My code is pasted below

package mail;

import jakarta.mail.Authenticator;
import jakarta.mail.PasswordAuthentication;
import jakarta.mail.Session;
import jakarta.mail.Store;

import java.io.IOException;
import java.util.Properties;

public class JavaMailUtil {

    public static void accessMailOauth() throws Exception{
        System.out.println("Starting...");

        // Load OAuth settings
        final Properties oAuthProperties = new Properties();
        try {
            oAuthProperties.load(JavaMailUtil.class.getResourceAsStream("oAuth.properties"));
        } catch (IOException e) {
            System.out.println("Unable to read OAuth configuration. Make sure you have a properly formatted oAuth.properties file. See README for details.");
            return;
        }

        final String appId = oAuthProperties.getProperty("app.id");
        final String[] appScopes = oAuthProperties.getProperty("app.scopes").split(",");
        final String clientSecret = oAuthProperties.getProperty("app.clientSecret");
        final String authority = oAuthProperties.getProperty("app.authority");

        // Get an access token
        Authentication.initialize(appId, authority, clientSecret);
        final String accessToken = Authentication.getUserAccessToken(appScopes);
        System.out.println("Access token = " + accessToken);

        String myAccountEmail = "[email protected]";

        Properties properties = new Properties();

        properties.setProperty("mail.store.protocol", "pop3s");
        properties.setProperty("mail.pop3s.auth", "true");
        properties.setProperty("mail.pop3s.auth.mechanisms", "XOAUTH2");
        properties.setProperty("mail.pop3s.host", "outlook.office365.com");
        properties.setProperty("mail.pop3s.port", "995");
        properties.setProperty("mail.pop3s.auth.xoauth2.two.line.authentication.format", "true");

        Session session = Session.getInstance(properties);
        session.setDebug( true );
        final Store store = session.getStore();
        store.connect(myAccountEmail, accessToken);

        System.out.println("Connected to store successfully!!");
    }

    public static void accessMailBasicAuth() throws Exception{
        System.out.println("Starting with Basic Auth...");

        String myAccountEmail = "[email protected]";

        Properties passwordProperties = new Properties();

        passwordProperties.setProperty("mail.store.protocol", "pop3s");
        passwordProperties.setProperty("mail.pop3s.host", "outlook.office365.com");
        passwordProperties.setProperty("mail.pop3s.port", "995");
        passwordProperties.setProperty("mail.pop3s.auth.xoauth2.disable", "true");


        Session passwordSession = Session.getInstance(passwordProperties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(myAccountEmail, "examplepassword");
            }
        });

        passwordSession.setDebug( true );
        final Store store = passwordSession.getStore();
        store.connect(myAccountEmail, "examplepassword");

        System.out.println("Connected to store successfully!!");
    }

    public static void main(String[] args) throws Exception {
        JavaMailUtil.accessMailBasicAuth();
    }
}

I think I have everything properly setup on the Azure Active Directory and mailbox settings side but any suggestions are welcome.

@jbescos
Copy link
Member

jbescos commented Aug 16, 2022

Maybe the password is wrong. I think in Gmail for example you cannot use the user-password you use to login in your emails. You need to use other password. Maybe this is the same case.

You can try to login with telnet to discard whether this is a jakarta.mail/angus issue:

$ telnet host port
USER myusername
+OK User name accepted, password please
PASS mysecretpassword
+OK Mailbox open, 3 messages

If the password is wrong, you should also receive an error there.

@jbescos
Copy link
Member

jbescos commented Aug 16, 2022

Also you can add the next property to obtain more information:

properties.setProperty("mail.debug", "true")

@jmehrens jmehrens added the question Further information is requested label Feb 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants