我需要使用Java程序从Gmail帐户下载附件。
使用Apache Camel

在跑步时,我越来越


  Exchange [MailMessage:null],因此,attachments.size()为0


我也在发布我的测试代码

@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();
        }
    }
}


我如何获得附件?

最佳答案

Endpoint.createExchange()只是创建要使用的交换,它不会从邮件服务器中提取消息。
您需要使用ConsumerTemplate从Gmail服务器提取消息。

08-05 01:31
查看更多