简体   繁体   English

使用JavaMail访问Mailinator POP3

[英]Accessing Mailinator POP3 using JavaMail

I am trying to access mailinators messages using JavaMail API. 我正在尝试使用JavaMail API访问mailinators消息。

I can properly connect to the server etc. but when it comes to reading a message I keep receiving "Folder is not Open" exception, and when I am checking if folder is open and if not opening the folder, that wont help either. 我可以正确连接到服务器等,但是在阅读消息时,我会不断收到“文件夹未打开”异常,并且当我检查文件夹是否打开以及是否未打开文件夹时,也无济于事。 I guess for some reason mailinator ends the connection or so. 我猜由于某种原因mailinator结束了连接。

If I try to get inputstream of message instead of using getContent I can read from the inputstream fine and it contains the styling of the message etc. but for some reason it seems like the data i read from inputstream doesnt contain the message body.. 如果我尝试获取消息的inputstream而不是使用getContent,则可以从inputstream读取,并且它包含消息的样式等。但是由于某种原因,看来我从inputstream读取的数据不包含消息主体。

If this is about mailinator or you could offer me any other random email reading service that supports pop3 or other easily readable, it doesn't really matter if I use mailinator for this project. 如果这是关于mailinator的,或者您可以为我提供任何其他支持pop3或其他易于阅读的随机电子邮件阅读服务,那么我是否将mailinator用于该项目并不重要。

My current mail reading code. 我当前的邮件阅读代码。

private void checkMail(String user) {
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    try {
        Store store = session.getStore("pop3");
        store.connect("pop.mailinator.com", 110, user, "12345678");
        Folder inbox = store.getFolder("inbox");
        if(inbox == null) {
            System.out.println("no inbox");
        } else {
            inbox.open(Folder.READ_ONLY);
            for(Message message: inbox.getMessages()) {

                byte[] buffer = new byte[10000];

                int read = 0;

                try {

                    while((read = message.getInputStream().read(buffer, 0, 1024)) > 0) {
                        for(int i = 0; i < buffer.length; i++) {
                            System.out.print((char)buffer[i]);
                        }
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                /*try {
                    System.out.println(message.getContent().toString());
                } catch (IOException e) {
                    e.printStackTrace();
                }*/
            }
        }
        inbox.close(false);
        store.close();
    } catch (NoSuchProviderException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

also when reading from inputstream it looks like the inputstream is never ending, just starting over. 当从inputstream读取时,看起来inputstream永远不会结束,只是重新开始。 My purpose is to get the message body and subject. 我的目的是获取消息正文和主题。

If you're reading the InputStream from the message then clearly the folder is open. 如果您正在从消息中读取InputStream,则显然该文件夹已打开。 When are you getting the "Folder is not open" exception? 什么时候出现“文件夹未打开”异常? What does the protocol trace show? 协议跟踪显示什么? You can try using Gmail if you think your server is port of the problem. 如果您认为服务器是问题的根源,则可以尝试使用Gmail Also, you'll want to fix your use of getDefaultInstance . 另外,您将需要修复对getDefaultInstance的使用

Some time in the past, Mailinator changed the behavior, forbidding POP3 access, (or only reserving for paying customers). 过去一段时间,Mailinator更改了行为,禁止访问POP3(或仅保留付费客户)。 Maybe that was your problem (if the code worked with another mail provider). 也许那是您的问题(如果代码与其他邮件提供商一起使用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM