Java 从Gmail下载邮件附件,获取Exchange[MailMessage:null]
我需要使用java程序从Gmail帐户下载附件。 使用 当我跑步的时候 Exchange[MailMessage:null],因此attachments.size()为0 我也发布了我的测试代码Java 从Gmail下载邮件附件,获取Exchange[MailMessage:null],java,email,apache-camel,email-attachments,Java,Email,Apache Camel,Email Attachments,我需要使用java程序从Gmail帐户下载附件。 使用 当我跑步的时候 Exchange[MailMessage:null],因此attachments.size()为0 我也发布了我的测试代码 @Test public void configureExcange() throws Exception { try { CamelContext context = new DefaultCamelContext(); Endpoint endpoint
@Test
public void configureExcange() throws Exception {
try {
CamelContext context = new DefaultCamelContext();
Endpoint endpoint =
context.getEndpoint("imaps://imap.gmail.com?username=" + mailId + "&password=" + password
+ "&delete=false&unseen=true&consumer.delay=60000");
// PollingConsumer consumer = endpoint.createPollingConsumer();
Exchange exchange = endpoint.createExchange();
process(exchange);
} catch (Exception e) {
e.printStackTrace();
}
}
public void process(Exchange exchange) throws Exception {
// the API is a bit clunky so we need to loop
Map<String, DataHandler> attachments = exchange.getIn().getAttachments();
if (attachments.size() > 0) {
for (String name : attachments.keySet()) {
DataHandler dh = attachments.get(name);
// get the file name
String filename = dh.getName();
// get the content and convert it to byte[]
byte[] data = exchange.getContext().getTypeConverter().convertTo(byte[].class, dh.getInputStream());
// write the data to a file
FileOutputStream out = new FileOutputStream(filename);
out.write(data);
out.flush();
out.close();
}
}
}
@测试
public void configureExchange()引发异常{
试一试{
CamelContext=新的DefaultCamelContext();
端点=
context.getEndpoint(“imaps://imap.gmail.com?username=“+mailId+”&password=“+password”
+“&delete=false&unseen=true&consumer.delay=60000”);
//PollingConsumer=endpoint.createPollingConsumer();
Exchange=endpoint.createExchange();
过程(交换);
}捕获(例外e){
e、 printStackTrace();
}
}
公共作废进程(Exchange)引发异常{
//API有点笨重,所以我们需要循环
映射附件=exchange.getIn().getAttachments();
如果(附件.size()>0){
for(字符串名称:attachments.keySet()){
DataHandler dh=attachments.get(名称);
//获取文件名
字符串filename=dh.getName();
//获取内容并将其转换为字节[]
byte[]data=exchange.getContext().getTypeConverter().convertTo(byte[].class,dh.getInputStream());
//将数据写入文件
FileOutputStream out=新的FileOutputStream(文件名);
输出。写入(数据);
out.flush();
out.close();
}
}
}
如何获取附件 Endpoint.createExchange()只是创建要使用的exchange,它不会从邮件服务器中提取邮件。
您需要使用从gmail服务器提取邮件 更新函数,显示如何轮询邮件帐户
@Test
public void configureExcange() throws Exception {
PollingConsumer pollingConsumer = null;
try {
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");
// options unseen=true, will only poll unread mails
//Polling an END point
pollingConsumer = endpoint.createPollingConsumer();
pollingConsumer.start();
pollingConsumer.getEndpoint().createExchange();
Exchange exchange = pollingConsumer.receive(60000);
while (exchange != null) {
process(exchange);
//each time "pollingConsumer" will poll 1 mail at a time
exchange = pollingConsumer.receive(60000);
}
} catch (Exception e) {
e.printStackTrace();
}
}