简体   繁体   English

使用apache camel从gmail收件箱中读取所有邮件

[英]read all mails from gmail inbox using apache camel

I am trying to read all mails from google mail (Gmail - imaps) account and download its attachments but I am only able to get one unread mail and its attachments. 我试图从谷歌邮件(Gmail - imaps)帐户阅读所有邮件并下载其附件,但我只能获得一封未读邮件及其附件。

Posting my code snippet. 发布我的代码段。

// 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);

        }
    }
}

Help me to iterate through all the mails(from inbox,unread). 帮助我遍历所有邮件(来自收件箱,未读)。

You need to run Exchange exchange = pollingConsumer.receive(); 您需要运行Exchange exchange = pollingConsumer.receive(); in a loop. 在一个循环中。

For eg 例如

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

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

相关问题 阅读收件箱中的所有邮件 - Read all mails from inbox 如何从 Java 中的 GMail 帐户的收件箱中读取电子邮件? - How to read e-mails from inbox of a GMail account in Java? 使用 java 使用 selenium webdriver 从 Gmail 收件箱打开邮件 - Open mails from Gmail inbox using selenium webdriver using java 通过对 IntegrationFlows.from(Mail) 使用多个 mailId,未阅读收件箱中的所有邮件 - Not reading all mails are in the inbox by using multiple mailIds for IntegrationFlows .from(Mail 如何使用 Apache Camel 阅读 gmail - How to read a gmail with Apache Camel 在java中使用JavaMail Api从gmail一次又一次地读取邮件 - Read mails again and again from gmail using JavaMail Api in java 如何使用java socket通过imap从gmail中读取邮件 - how to read mails from gmail with imap by using java socket 是否可以使用mailgun在Java中阅读未读的收件箱邮件? - Is it possible to read unread inbox mails in java using mailgun? 检索 Gmail 收件箱中存在的相关邮件的算法 - Algorithm to retrieve associated mails present in Inbox of Gmail 使用Java邮件api通过gmail发送的邮件会传递到垃圾邮件。 如何使它们传递到收件箱? - Mails sent through gmail using java mail api are delivered to spam. How to make them delivered to the inbox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM