Java 使用apache camel读取gmail收件箱中的所有邮件
我试图阅读谷歌邮件(Gmail-imaps)账户中的所有邮件并下载其附件,但我只能收到一封未读邮件及其附件 发布我的代码片段Java 使用apache camel读取gmail收件箱中的所有邮件,java,email,apache-camel,inbox,Java,Email,Apache Camel,Inbox,我试图阅读谷歌邮件(Gmail-imaps)账户中的所有邮件并下载其附件,但我只能收到一封未读邮件及其附件 发布我的代码片段 // Download function public void download() throws Exception { PollingConsumer pollingConsumer = null; CamelContext context = new DefaultCamelContext(); Endpo
// 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);
}
}
}
//下载功能
public void download()引发异常{
PollingConsumer PollingConsumer=null;
CamelContext=新的DefaultCamelContext();
端点=
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=pollingConsumer.receive();
log.info(“exchange:+exchange.getExchangeId());
过程(交换);
}
//邮件处理功能
公共作废进程(Exchange)引发异常{
映射附件=exchange.getIn().getAttachments();
messageCopy=exchange.getIn().copy();
如果(messageCopy.getAttachments().size()>0){
对于(Map.Entry:messageCopy.getAttachments().entrySet()){
DataHandler dHandler=entry.getValue();
//获取文件名
字符串文件名=dHandler.getName();
//获取内容并将其转换为字节[]
字节[]数据=
exchange.getContext().getTypeConverter().convertTo(字节[].class,dHandler.getInputStream());
FileOutputStream out=新的FileOutputStream(文件名);
输出。写入(数据);
out.flush();
out.close();
log.info(“下载的附件,文件名:”+文件名);
}
}
}
帮助我遍历所有邮件(来自收件箱,未读)。您需要运行
Exchange=pollingConsumer.receive()代码>在循环中
例如
Exchange ex = pollingConsumer.receive();
while (ex != null) {
process(ex);
ex = pollingConsumer.receive();
}
但我的问题是,我如何反复浏览我的邮件。现在我收到1封未读邮件,但我需要所有来自inboxI的未读邮件。对不起。我根据题目误读了你的问题。编辑修复的答案。