gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:46:55 29 4
gpt4 key购买 nike

我正在尝试阅读来自谷歌邮件(Gmail - imaps)帐户的所有邮件并下载其附件,但我只能获得一封未读邮件及其附件。

发布我的代码片段。

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

}
}
}

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

最佳答案

您需要在循环中运行 Exchange exchange = pollingConsumer.receive();

例如

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

关于java - 使用 apache camel 从 gmail 收件箱中读取所有邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25763655/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com