spring集成邮件处理器

时间:2015-06-29 14:01:56

标签: spring email spring-boot spring-integration

我希望使用此示例作为构建某种“邮件处理器”的指南,其中包括:

  1. 列表项
  2. 从IMAP阅读
  3. 保存附件
  4. 在数据库中保留发件人信息和附件路径
  5. 将确认电子邮件发回给发件人
  6. 我已经构建了bean来读取mbox并像Gunnar Hillert intermediate email samples一样保存附件:

        (...)
        <int:channel id="sendChannel" />
    
        <int-mail:outbound-channel-adapter
            channel="sendChannel" host="${smtp.host}" username="${imap.username}"
            password="${imap.password}" />
    
        <int:channel id="receiveChannel" />
    
        <!-- replace 'userid and 'password' wit the real values -->
        <int-mail:imap-idle-channel-adapter
            id="customAdapter"
            store-uri="imap://${imap.username}:${imap.password}@${imap.host}:${imap.port}/inbox"
            channel="receiveChannel" auto-startup="true" should-delete-messages="false"
            should-mark-messages-as-read="false" java-mail-properties="javaMailProperties" />
    
        <util:properties id="javaMailProperties">
            <prop key="mail.imap.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
            <prop key="mail.imap.socketFactory.fallback">true</prop>
            <prop key="mail.store.protocol">imap</prop>
            <prop key="mail.debug">${mail.debug}</prop>
        </util:properties>
    
        <int:chain id="transform-split" input-channel="receiveChannel"
            output-channel="outputChannel">
            <int:transformer>
                <bean class="it.lrkwz.imap.support.EmailTransformer" />
            </int:transformer>
            <int:splitter>
                <bean class="it.lrkwz.imap.support.EmailSplitter" />
            </int:splitter>
        </int:chain>
    
        <int:channel id="outputChannel" />
    
        <int-file:outbound-channel-adapter
            id="save-as-file" auto-create-directory="true" channel="outputChannel"
            directory-expression="'target/out/' + headers.directory" />
    </beans>
    

    在哪里(main(),EmailTransformer(),EmailSplitter(),...)我应该拦截“无附件”状态? 如何向发件人发送电子邮件?

    @SpringBootApplication
    @ImportResource("classpath:META-INF/spring/integration/aruba-imap-idle-config.xml")
    public class ImapProcessorApplication implements CommandLineRunner {
        private static Logger logger = LoggerFactory.getLogger(ImapProcessorApplication.class);
    
        public static void main(String[] args) {
            ConfigurableApplicationContext applicationContext = SpringApplication.run(ImapProcessorApplication.class, args);
        }
    
        @Autowired
        DirectChannel receiveChannel;
    
        @Autowired
        DirectChannel sendChannel;
    
        @Override
        public void run(String... arg0) throws Exception {
            receiveChannel.subscribe(new MessageHandler() {
                public void handleMessage(Message<?> message) throws MessagingException{
        ...
    

    谢谢。

1 个答案:

答案 0 :(得分:0)

要处理“无附件”状态,您应该遵循标准的Spring Integration配方并将<filter>添加到您的流程中。看起来就在你的<splitter>之后(如果你用`Multipart分割你的信息)。在这里,您可以找到BTW,如何拆分和过滤:Download attachments using Java Mail

您已经<int-mail:outbound-channel-adapter>发送电子邮件。那么,您需要的是outputChannel<publish-subscriber-channel>并添加更多订阅者(<outbound-channel-adapter><service-activator><outbound-gateway>等)并发送确认电子邮件,将数据存储到DB。任何你想要和需要的东西!