我正在尝试阅读来自谷歌邮件(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();
循环中。
For e.g.
Exchange ex = pollingConsumer.receive();
while (ex != null) {
process(ex);
ex = pollingConsumer.receive();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)