PLAIN + IMAP + SSL Android Java - failed connection

28 Views Asked by At

I am trying to connect to a mail server using the imap protocol over ssl and authorization using the plain method.

Properties props = new Properties();
        props.put("mail.store.protocol", "imaps");
        props.put("mail.imap.host", "imap.***my-server-domain***");
        props.put("mail.imap.port", "993");
        props.put("mail.imap.ssl.enable", "true");

        final Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(
                        login,
                        password
                );
            }
        };
        Session session = Session.getInstance(props, authenticator);
        Store store = null;
        try {
            store = session.getStore("imap");
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
        try {
            store.connect();
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

But no matter how I try, I get either such errors:

at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1667)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:115)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
at java.net.InetAddress.getByName(InetAddress.java:1106)
at jakarta.mail.URLName.getHostAddress(URLName.java:502)
at jakarta.mail.URLName.hashCode(URLName.java:478)
at java.util.Hashtable.get(Hashtable.java:365)
at jakarta.mail.Session.getPasswordAuthentication(Session.java:889)
at jakarta.mail.Service.connect(Service.java:325)
at jakarta.mail.Service.connect(Service.java:222)
at jakarta.mail.Service.connect(Service.java:171)

Or one like this (if I change the settings a little somewhere):

at jakarta.mail.Service.connect(Service.java:342)

I tried changing imap, imaps and so on, the port (although the server is running on 993) but still can't connect. The Internet in the application is included in the manifest, I also did a separate method for checking it, and yes, it worked.

It seems to me that I am not working correctly with the plain method and have not connected something in the Properties.

0

There are 0 best solutions below