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

Unable to login to Google Apps Account #5

Open
adam-g-pullen opened this issue Jun 25, 2013 · 4 comments
Open

Unable to login to Google Apps Account #5

adam-g-pullen opened this issue Jun 25, 2013 · 4 comments

Comments

@adam-g-pullen
Copy link

When attempting to login to at google apps account where the domain name is required after the "@" i.e. notarealemployee"@"finalconcept.com.au the IMapGmailConnection anyway appends "@google.com" to the username.

Maybe a better option would be to first check for the @ symbol in the username then if not present append the @google.com

If I am misusing the lib please let me know, thanks

store.connect(gmailImapHost, loginCredentials.getUsername()
                .concat(CommonConstants.GMAIL_EXTENTION),
                new String(loginCredentials.getPasword()));
@adam-g-pullen
Copy link
Author

PS: I am using the MVN dep 0.4

<dependency>
    <groupId>com.googlecode.gmail4j</groupId>
    <artifactId>gmail4j</artifactId>
    <version>0.4</version>
    <scope>test</scope>
</dependency>

@adam-g-pullen
Copy link
Author

Here is another option:

Create a subclass called GoogleAppsImapGmailConnection that extends ImapGmailConnection and overrides the
openGmailStore()

I had to create a new store var, but it only seems to be used in the openGmailStore and disconnect methods

package au.com.finalconcept.imapclient;

import java.io.IOException;
import java.util.List;

import javax.mail.Store;

import com.googlecode.gmail4j.GmailException;
import com.googlecode.gmail4j.GmailMessage;
import com.googlecode.gmail4j.auth.Credentials;
import com.googlecode.gmail4j.javamail.ConnectionInfo;
import com.googlecode.gmail4j.javamail.ImapConnectionHandler;
import com.googlecode.gmail4j.javamail.ImapGmailClient;
import com.googlecode.gmail4j.javamail.ImapGmailConnection;
import com.googlecode.gmail4j.javamail.ImapGmailLabel;

public class Main {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

        ImapGmailClient client;
        ImapGmailConnection connection;
        List<GmailMessage> messages;

        client = new ImapGmailClient(ImapGmailLabel.INBOX);

        Credentials creds;
        creds = new Credentials(args[0], args[1].toCharArray());
        connection = new GoogleAppsImapGmailConnection();
        connection.setLoginCredentials(creds);
        client.setConnection(connection);

        try {
            messages = client.getUnreadMessages();
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        for (GmailMessage message : messages) {
            System.out.println(message);
        }

    }

    public static class GoogleAppsImapGmailConnection extends ImapGmailConnection {
        private Store store;

        /**
         * Opens Gmail {@link Store}
         * 
         * @return singleton instance of Gmail {@link Store}
         */
        @Override
        public Store openGmailStore() {
            try {
                // if stort object instance is not null and the service
                // connection state
                // is connected then close this service and terminate its
                // connection.
                // Note : due to security concerns gmail imap only allow max 10
                // connections because of this reason any existing connection
                // that
                // is open should terminate.
                if (isConnected()) {
                    disconnect();
                }

                store = getSession().getStore("imaps");
                store.addConnectionListener(new ImapConnectionHandler(
                        new ConnectionInfo(loginCredentials.getUsername(), getGmailImapHost(), getGmailImapPort())));
                store.connect(getGmailImapHost(), loginCredentials.getUsername(), new String(loginCredentials.getPasword()));
                setConnected(store.isConnected());
            } catch (final Exception e) {
                throw new GmailException("Failed opening Gmail IMAP store", e);
            }
            return store;

        }

        /**
         * Disconnects from Gmail
         */
        public void disconnect() {
            try {
                if (isConnected()) {
                    store.close();
                }
            } catch (final Exception e) {
                //log.warn("Failed closing Gmail IMAP store", e);
            }
        }
    }

}

@PavelVinogradov
Copy link
Contributor

This issue was fixed in trunk. Maybe it published to maven repository under version 0.5-SNAPSHOT.

@adam-g-pullen
Copy link
Author

thanks mate,

I will keep a look out for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants