无法使用IMAPClient连接到IMAP服务器

时间:2018-01-12 15:29:21

标签: java email connection imap apache-commons-net

我正在尝试连接到gmail的IMAP / SMTP服务器。

我可以将whitout问题连接到SMTP服务器(smtp.gmail.com:587),但不能连接到IMAP服务器(imap.gmail.com:993)。异常的堆栈跟踪只是说连接已关闭。

想象一下这样的虚构main类:

public class Main{

    public void main(String[] args) {
        MailDomains domainChecker = new MailDomains();
        domainChecker.pingServer("smtp.gmail.com:587", "imap.gmail.com:993");
    }
}

这是我的代码

public HashMap<String,Boolean> pingServer(String smtp_address, String imap_address) {
    HashMap<String, Boolean> areConnected = new HashMap<String, Boolean>();
    IMAPClient imapClient = new IMAPClient();
    SMTPClient smtpClient = new SMTPClient();

    //Comprobación SMTP
    try {
        //smtp_address -> [HOSTNAME]:[PORT] (already checked the substring's)
        smtpClient.connect(
                smtp_address.substring(0, smtp_address.indexOf(':')),
                Integer.parseInt(smtp_address.substring(smtp_address.indexOf(':')+1,smtp_address.length())));

        areConnected.put("smtp", smtpClient.isConnected());
        smtpClient.disconnect();
        l.info("Conectado satisfactoriamente al servidor SMTP");
    } catch (IOException e) {

        l.error("Dirección SMTP errónea. Host inaccesible");
        areConnected.put("smtp", false);
        e.printStackTrace();
    }

    //Comprobación IMAP
    try {
        //imap_address -> [HOSTNAME]:[PORT] (already checked the substring's)
        imapClient.connect(
                imap_address.substring(0, imap_address.indexOf(':')),
                Integer.parseInt(imap_address.substring(imap_address.indexOf(':')+1,imap_address.length())));

        areConnected.put("imap", imapClient.isConnected());
        imapClient.disconnect();
        l.info("Conectado satisfactoriamente al servidor IMAP");
    } catch (IOException e) {

        l.error("Dirección IMAP errónea. Host inaccesible");
        areConnected.put("imap", false);
        e.printStackTrace();
    }

    smtpClient = null;
    imapClient = null;

    return areConnected;
}

它需要一点时间才会抛出与IMAP相关的异常。这是堆栈跟踪:

java.io.EOFException: Connection closed without indication.
at org.apache.commons.net.imap.IMAP.__getReply(IMAP.java:151)
at org.apache.commons.net.imap.IMAP._connectAction_(IMAP.java:227)
at org.apache.commons.net.imap.IMAPSClient._connectAction_(IMAPSClient.java:175)
at org.apache.commons.net.SocketClient._connect(SocketClient.java:244)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:202)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:306)
at Utils.MailDomains.pingServer(MailDomains.java:81)
at interfaz.controller.AddServerMailController.registerAddress(AddServerMailController.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)

有关正在发生的事情的任何想法?我在互联网上搜索但没有太多的文档,或者至少,我找不到它。如果您发布一些关于此的文章,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

我没有看到你为imap连接打开SSL。端口993是SSL / TLS安全的IMAP端口。