Java Mail Api - IMAP Problem

v Ralle v

Aktives Mitglied
Hallo,

ich versuche mich mittels der JAVA Mail Api und IMAP mit einem Server zu verbinden. Mit Pop3 klappt dies super, doch bei imap nicht und ich finde den Fehler einfach nicht.

Meine Properties:
Java:
props.setProperty("mail.imaps.host", posteingang_host);
				props.setProperty("mail.imaps.user", benutzerName);
				props.setProperty("mail.imaps.password", password);
				props.setProperty("mail.imaps.auth", "true");
				props.setProperty("mail.imaps.starttls.enable", "true");
				props.setProperty("mail.imaps.socketFactory.port", String.valueOf(posteingang_port));
				props.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
				props.setProperty("mail.imaps.socketFactory.fallback", "false");

Einloggen:
Java:
Session session = Session.getInstance(props, new PassAuthenticator(getUsername(), getPassword()));
		session.setDebug(true);
		
		store = isSSL ? session.getStore(protocol + "s") : session.getStore(protocol);
		store.connect();

Bei store.connect(); fliegt er raus. Meine Konsolenausgabe sieht wie folgt aus:

Java:
DEBUG: setDebug: JavaMail version 1.4.3
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc]
DEBUG: mail.imap.fetchsize: 16384
DEBUG: mail.imap.statuscachetimeout: 1000
DEBUG: mail.imap.appendbuffersize: -1
DEBUG: mail.imap.minidletime: 10
DEBUG: enable STARTTLS
DEBUG: trying to connect to host "pop.gmail.com", port 993, isSSL true
+OK Gpop ready for requests from 217.17.195.94 b16pf683590fab.14
javax.mail.MessagingException: +OK Gpop ready for requests from 217.17.195.94 b16pf683590fab.14;
  nested exception is:
	com.sun.mail.iap.ConnectionException: +OK Gpop ready for requests from 217.17.195.94 b16pf683590fab.14
	at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:616)
	at javax.mail.Service.connect(Service.java:291)

Der Server antwortet doch korrekt? Warum dann die Fehlermeldung? Ich habe es auch bei verschiedenen Servern probiert (Gmail und noch ein anderer Anbieter) und der Fehler sieht immer gleich aus. Also muss er an meinem Code liegen, aber wo? Ich bin mittlerweile echt ratlos.

Wie gesagt, pop3 funktioniert bei SSL oder bei der normalen Verbindung. Imap bei keinem von beiden.

Über Hilfe wäre ich sehr dankbar!
 
Zuletzt bearbeitet:
Und der host (pop.gmail.com) stimmt für _IMAP_ anfragen?
Denn:
dig pop.gmail.com -> 74.125.43.109
dig imap.gmail.com -> 74.125.39.109
 
Und der host (pop.gmail.com) stimmt für _IMAP_ anfragen?
Denn:
dig pop.gmail.com -> 74.125.43.109
dig imap.gmail.com -> 74.125.39.109

Hm habe das nochmal probiert, das spielt keine Rolle ob imap/pop.gmail.com.

Java:
DEBUG: setDebug: JavaMail version 1.4.3
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc]
DEBUG: mail.imap.fetchsize: 16384
DEBUG: mail.imap.statuscachetimeout: 1000
DEBUG: mail.imap.appendbuffersize: -1
DEBUG: mail.imap.minidletime: 10
DEBUG: enable STARTTLS
DEBUG: protocolConnect returning false, host=imap.gmail.com, user=r.wondr, password=<null>
DEBUG: trying to connect to host "imap.gmail.com", port 993, isSSL true
+OK Gpop ready for requests from 217.17.195.94 j25pf3198856fah.6
javax.mail.MessagingException: +OK Gpop ready for requests from 217.17.195.94 j25pf3198856fah.6;
  nested exception is:
	com.sun.mail.iap.ConnectionException: +OK Gpop ready for requests from 217.17.195.94 j25pf3198856fah.6
	at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:616)
	at javax.mail.Service.connect(Service.java:313)
	at javax.mail.Service.connect(Service.java:172)
	at javax.mail.Service.connect(Service.java:121)

Echt zum Verzweifeln diese Angelegenheit...
 
Zuletzt bearbeitet:
Also, ich weiß auch nicht wo nun direkt das Problem ist...

Vielleicht(und ich kann es nur empfehlen) hilft dir das Integrations-Framework Apache Camel weiter ...

IMAP, POP3 und alles weitere mit und ohne SSL funktioniert bei mir wunderbar. Sich da reinzulesen ist auch recht einfach. Ein Stückchen Code hab ich in einem anderen Thread hier zu stehen.

Also vllt. als Alternative, wenn deins gar nicht funktionieren möchte 🙂


Edit: Funktioniert auch auf Grundlage von Java Mail 😉
 
Okay - ich hab das eben mal lokal probiert - und wie ich es eingangs schon vermutet hatte geht hier irgendwas mit pop/imap durcheinander:
+OK Gpop ready for requests from 217.17.195.94 j25pf3198856fah.6
Da steht definitiv Gpop ready. Das hatte mich schon die ganze Zeit stutzig gemacht. Da einige Stellen in Deinem Quelltext mit variablen ausgestattet sind deren Inhalt ich nicht kenne, habe ich es mal schnell lokal nachgebaut und bei mir funktioniert es. Auch mit dem gmail imap-Server:
* OK Gimap ready for requests from ...
Hier mein Programm (nur mal quick&dirty runter getippt)
Java:
public class IMAPTest {
    public static void main(String[] args) throws MessagingException {
        String posteingang_host = "imap.gmail.com";
        String benutzerName = "blubb";
        String password = "blah";
        Properties props = new Properties();
        props.setProperty("mail.imaps.host", posteingang_host);
        props.setProperty("mail.imaps.user", benutzerName);
        props.setProperty("mail.imaps.password", password);
        props.setProperty("mail.imaps.auth", "true");
        props.setProperty("mail.imaps.starttls.enable", "true");
        //props.setProperty("mail.imaps.socketFactory.port", String.valueOf(posteingang_port));
        props.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.imaps.socketFactory.fallback", "false");
        
        Session session = Session.getInstance(props, new PassAuthenticator(benutzerName, password));
        session.setDebug(true);
        boolean isSSL = true;
        Store store = isSSL ? session.getStore("imaps") : session.getStore("imap");
        store.connect();
    }
}

class PassAuthenticator extends Authenticator  {
    String userName;
    String password;
    
    public PassAuthenticator(String userName, String password) {
        super();
        this.userName = userName;
        this.password = password;
    }

    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(userName, password);
    }
    
}
 
Ich habe die Lösung gefunden. Vielen Dank mfernau!!! Mein Code war theoretisch richtig, nur meine Eingabe hat nicht gestimmt 😳 Ich habe als Port immer 995 verwendet zum richtigen Host, er hätte aber 993 lauten müssen. Schon etwas blöd.

Darauf gekommen bin ich durch deine auskommentierte Zeile, da bei dir nun der Defaultport verwendet wird und da hab ich nochmal darüber nachgedacht, welcher der Defaultport ist 😀

Jetzt geht es! Super 🙂
 

Zurück
Oben