如何在Spring ImapIdleChannelAdapter

时间:2017-02-17 12:41:30

标签: spring spring-integration imap

弹簧配置示例如下。

<int-mail:imap-idle-channel-adapter id="mailAdapter"
    store-uri="imaps://${"username"}:${"password"}@imap-server:993/INBOX"
    java-mail-properties="javaMailProperties"
    channel="emails"
    should-delete-messages="false"
    should-mark-messages-as-read="true">
</int-mail:imap-idle-channel-adapter>

我希望将密码字段保存在属性文件中并在代码中解密。我不确定如何将ImapIdleChannelAdapter的mailReceiver属性设置为我的自定义版本的ImapMailReceiver。 如果有任何办法,请告诉我。

我的所有配置都是XML格式,如上所述。 以上解决方案添加defifnation没有用,可能是我做错了。然后我尝试使用XML + Java配置,如下所示。

    @Configuration
public class EmailConfiguration {
    @Bean
    public ImapIdleChannelAdapter customAdapter() {
        ImapIdleChannelAdapter adapter = new ImapIdleChannelAdapter(mailReceiver());
        adapter.setOutputChannel(outputChannel());
        adapter.setErrorChannel(errorChannel());
        adapter.setAutoStartup(true);
        adapter.setShouldReconnectAutomatically(true);
        adapter.setTaskScheduler(taskScheduler());
        return adapter;
    }

    @Bean
    public TaskImapMailReceiver mailReceiver() {
        TaskImapMailReceiver mailReceiver = new TaskImapMailReceiver("imaps://[username]:[password]@imap.googlemail.com:993/inbox");
        mailReceiver.setShouldDeleteMessages(false);
        mailReceiver.setShouldMarkMessagesAsRead(true);
        //mailReceiver.setJavaMailProperties(javaMailProperties());
        mailReceiver.setMaxFetchSize(Integer.MAX_VALUE);
        return mailReceiver;
    }
} 

还创建了空的errorChannel,outputChannel等。我观察到Spring创建了两个实例,一个是xml配置,另一个是java @Configuration。预计只使用java配置的地方。如果我删除xml配置标记  然后它提供了我的mailReceiver的sigle imap实例,但只运行一次不定期。也没有显示IMAPS日志。

只是想知道我是否需要做这么多来加密密码。我的做法有点不对劲。

2 个答案:

答案 0 :(得分:0)

使用Java配置而不是XML ...

@Configuration
public class MyConfigClass {

    @Bean
    public MyMailReceiver receiver() {
        ...
    }

    @Bean
    public ImapIdleChannelAdapter adapter() {
        ImapIdleChannelAdapter adapter = new ImapIdleChannelAdapter(receiver());
        ...
        return adapter;
    }

}

如果您将XML用于其他所有内容,只需将此类作为<bean/>添加到您的XML中。

修改

这是一个适合我的例子......

@SpringBootApplication
public class So42298254Application {

    public static void main(String[] args) {
        SpringApplication.run(So42298254Application.class, args);
    }

    @Bean
    public TestMailServer.ImapServer imapServer() {
        return TestMailServer.imap(0);
    }

    @Bean
    public ImapMailReceiver receiver() {
        ImapMailReceiver imapMailReceiver = new ImapMailReceiver(imapUrl("user", "pw"));
        imapMailReceiver.setHeaderMapper(new DefaultMailHeaderMapper()); // converts the MimeMessage to a String
        imapMailReceiver.setUserFlag("testSIUserFlag"); // needed by the SI test server
        Properties javaMailProperties = new Properties();
        javaMailProperties.put("mail.debug", "true");
        imapMailReceiver.setJavaMailProperties(javaMailProperties);
        return imapMailReceiver;
    }

    private String imapUrl(String user, String pw) {
        return "imap://"
                + user + ":" + pw
                + "@localhost:" + imapServer().getPort() + "/INBOX";
    }

    @Bean
    public ImapIdleChannelAdapter adapter() {
        ImapIdleChannelAdapter adapter = new ImapIdleChannelAdapter(receiver());
        adapter.setOutputChannelName("handleMail");
        return adapter;
    }

    @ServiceActivator(inputChannel = "handleMail")
    public void handle(String mail, @Header(MailHeaders.FROM) Object from) {
        System.out.println(mail + " from:" + from);
        imapServer().resetServer(); // so we'll get the email again
    }

}

答案 1 :(得分:0)

我的意图是在属性文件中使用加密密码。 所以我改变了进入电子邮件接收课程的方法。我添加了继承的PropertyPlaceholderConfigurer并实现了方法convertPropertyValue(),如下所示。

RxTextView
.textChanges(queryEditText)
.debounce(100, TimeUnit.MILLISECONDS)
.doOnNext(new Action1<CharSequence>() {
    @Override
    public void call(CharSequence charSequence) {

    }
})
.subscribe();

并更改属性文件以将加密值括在custuom ENC标记中 如

CGRect basketTopFrame = self.upperview.frame;
basketTopFrame.origin.y = -basketTopFrame.size.height;

CGRect basketBottomFrame = self.lowerview.frame;
basketBottomFrame.origin.y = self.view.bounds.size.height;

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelay:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

self.upperview.frame = basketTopFrame;
self.lowerview.frame = basketBottomFrame;

[UIView commitAnimations];