【问题标题】:read all mails from gmail inbox using apache camel使用 apache camel 从 gmail 收件箱中读取所有邮件
【发布时间】:2014-09-10 10:59:25
【问题描述】:

我正在尝试从 google mail (Gmail - imaps) 帐户读取所有邮件并下载其附件,但我只能获得一封未读邮件及其附件。

发布我的代码 sn-p。

// Download function 

public void  download() throws Exception {

        PollingConsumer pollingConsumer = null;
        CamelContext context = new DefaultCamelContext();

        Endpoint endpoint =
                context.getEndpoint("imaps://imap.gmail.com?username="
                        + mailId + "&password=" + password 
                        + "&delete=false&peek=false&unseen=true&consumer.delay=60000&closeFolder=false&disconnect=false");

        pollingConsumer = endpoint.createPollingConsumer();
        pollingConsumer.start();

        pollingConsumer.getEndpoint().createExchange();
        Exchange exchange = pollingConsumer.receive();

        log.info("exchange : " + exchange.getExchangeId());
        process(exchange);

}

// mail process function

public void process(Exchange exchange) throws Exception {
    Map<String, DataHandler> attachments = exchange.getIn().getAttachments();

    Message messageCopy = exchange.getIn().copy();

    if (messageCopy.getAttachments().size() > 0) {
        for (Map.Entry<String, DataHandler> entry : messageCopy.getAttachments().entrySet()) {
            DataHandler dHandler = entry.getValue();
            // get the file name
            String filename = dHandler.getName();

            // get the content and convert it to byte[]
            byte[] data =
                    exchange.getContext().getTypeConverter().convertTo(byte[].class, dHandler.getInputStream());

            FileOutputStream out = new FileOutputStream(filename);
            out.write(data);
            out.flush();
            out.close();
            log.info("Downloaded attachment, file name : " + filename);

        }
    }
}

帮助我遍历所有邮件(来自收件箱,未读)。

【问题讨论】:

    标签: java email apache-camel inbox


    【解决方案1】:

    你需要循环运行Exchange exchange = pollingConsumer.receive();

    例如

    Exchange ex = pollingConsumer.receive();
    while (ex != null) {
        process(ex);
        ex = pollingConsumer.receive();
    }
    

    【讨论】:

    • 但我的问题是如何遍历我的邮件。现在我收到 1 封未读邮件,但我需要收件箱中的所有未读邮件
    • 对不起。我根据标题误读了您的问题。编辑修复的答案。
    猜你喜欢
    • 1970-01-01
    • 2012-11-27
    • 2015-04-07
    • 1970-01-01
    • 2019-10-24
    • 2015-03-13
    • 2017-09-02
    • 1970-01-01
    • 2010-10-14
    相关资源
    最近更新 更多