jakartaee / mail-api

Jakarta Mail Specification project
https://jakartaee.github.io/mail-api
Other
245 stars 102 forks source link

Authentication issues trying to access POP mailbox #614

Open benjaminxie opened 2 years ago

benjaminxie commented 2 years ago

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 = "myemail@myemail.onmicrosoft.com";

        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 = "myemail@myemail.onmicrosoft.com";

        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 commented 2 years ago

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 commented 2 years ago

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

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