使用IMAP协议从Java读取邮件和从Java读取邮件,但是缺少一些文件

时间:2019-05-01 10:28:33

标签: java spring-boot spring-data-jpa javamail imap

我正在春季启动项目中,我需要阅读邮件的详细信息。

首先,我将未读邮件存储到message []数组中,然后再读取前50封邮件,再读取接下来的50封邮件,依此类推。

但是当我这样做时,有些邮件丢失了,即,在50封邮件中,有一些邮件没有被读取,并且没有记录详细信息。

下面是我执行所有这些操作的功能。

请帮助我改进代码。

public List<Object> read(){

    Properties properties = new Properties();
    properties.setProperty("mail.host", "imap.gmail.com");
    properties.setProperty("mail.port", "995");
    properties.setProperty("mail.transport.protocol", "imaps");
    String userName=resourceBundle.getString("mailUserName");
    String password=resourceBundle.getString("mailPassword");
    Session session = Session.getInstance(properties,
            new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(userName, password);
                }
            });

    try {

        Store store = session.getStore("imaps");
        store.connect();
        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_WRITE);
        SearchTerm recentTerm = new FlagTerm(new Flags(Flags.Flag.RECENT), true);
        SearchTerm fromTerm = new FromTerm(new InternetAddress(resourceBundle.getString("receiveMailFrom")));
        SearchTerm unreadTerm = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
        SearchTerm subjectTerm = new SubjectTerm(this.subjectTerm);
        SearchTerm subjectAndUnreadTerm = new AndTerm(subjectTerm, unreadTerm);
        SearchTerm recentAndFromTerm = new AndTerm(recentTerm, fromTerm);
        SearchTerm subjectAndUnreadAndFromTerm = new AndTerm(subjectAndUnreadTerm, fromTerm);
        Message messages[] = inbox.search(subjectAndUnreadAndFromTerm);
        List<Object> readParams=new ArrayList<>();
        if (messages.length<count)
        logger.info("Number of mails = " + messages.length);
        else
            logger.info("Number of mails = " + count);


        for (Message message : messages) {
            if(mailcount<count) {
                Address[] from = message.getFrom();
                logger.info("-------------------------------");
                logger.info("Date : " + message.getSentDate());
                logger.info("From : " + from[0]);
                logger.info("Subject: " + message.getSubject());
                logger.info("Content :");
                fileNames = processMessageBody(message);
                logger.info("--------------------------------");
                mailcount +=1;

            }
            else{
                break;
            }

        }

        inbox.close(true);
        store.close();
        if(messages.length==0)
        {
            readParams.add(0);
            return readParams;
        }
        else {
            readParams.add(1);
            readParams.add(fileNames);
            return readParams;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

0 个答案:

没有答案