Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) throws FileNotFoundException {
- KeyStoreParameters ksp = new KeyStoreParameters();
- ksp.setResource("C://Program Files (x86)//Java//jre1.8.0_161//lib//security");
- ksp.setPassword("changeit");
- KeyManagersParameters kmp = new KeyManagersParameters();
- kmp.setKeyStore(ksp);
- kmp.setKeyPassword("changeit");
- SSLContextParameters scp = new SSLContextParameters();
- scp.setKeyManagers(kmp);
- BasicConfigurator.configure();
- RutaMail routeBuilder = new RutaMail();
- SimpleRegistry jndi= new SimpleRegistry();
- jndi.put("keystore", scp);
- CamelContext ctx = new DefaultCamelContext(jndi);
- try {
- ctx.addRoutes(routeBuilder);
- ctx.start();
- Thread.sleep(30000);
- ctx.stop();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public void configure() throws Exception {
- from("imaps://imap.gmail.com:993?username=user@gmail.com&password=password"
- + "&delete=false&unseen=true&consumer.delay=60000&contentType=multipart/MIXED")
- .convertBodyTo(String.class, "UTF-8")
- .process(new MailProcessor())
- .to("file:C:/outputFolder/?flatten=true&fileName=${header.nombref}")
- .log("el nombre del archivo es ${header.nombref}").end();
- }
- }
- public class MailProcessor implements Processor {
- public void process(Exchange exchange) throws Exception {
- String nombre="";
- // the API is a bit clunky so we need to loop
- Map<String, DataHandler> attachments = exchange.getIn().getAttachments();
- String file="";
- if (attachments.size() > 0) {
- for (String name : attachments.keySet()) {
- DataHandler dh = attachments.get(name);
- String filename =dh.getName();
- nombre =filename;
- byte[] data = exchange.getContext().getTypeConverter()
- .convertTo(byte[].class, dh.getInputStream());
- exchange.getIn().setBody( data);
- exchange.getIn().setHeader("nombref", filename);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment