使用 apache camel 从 gmail 收件箱中读取所有邮件 [英] read all mails from gmail inbox using apache camel
本文介绍了使用 apache camel 从 gmail 收件箱中读取所有邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试阅读来自 google mail (Gmail - imaps) 帐户的所有邮件并下载其附件,但我只能收到一封未读邮件及其附件.
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.
发布我的代码片段.
// 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).
推荐答案
您需要循环运行 Exchange exchange = pollingConsumer.receive();
.
例如
Exchange ex = pollingConsumer.receive();
while (ex != null) {
process(ex);
ex = pollingConsumer.receive();
}
这篇关于使用 apache camel 从 gmail 收件箱中读取所有邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文