spring 集成邮件处理器

spring integration mail processor

我希望使用此示例作为构建一种 "mail processor" 的指南,其中:

  1. 列表项
  2. 从 IMAP 读取
  3. 保存附件
  4. 在数据库中保留发件人信息和附件路径
  5. 向发件人发回确认邮件

我已经构建了 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>

我应该在哪里拦截 "no attachment" 状态? 如何将电子邮件发回给发件人?

@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{
    ...

谢谢。

要处理 "no attachment" 状态,您应该真正遵循标准 Spring 集成方法并将 <filter> 添加到您的流程中。看起来就在您的 <splitter> 之后(如果您将消息拆分为“Multipart”)。在这里你可以找到,顺便说一句,如何拆分和过滤:Download attachments using Java Mail.

您已经 <int-mail:outbound-channel-adapter> 可以发送电子邮件。因此,您只需将 outputChannel 设置为 <publish-subscriber-channel> 并添加更多订阅者(<outbound-channel-adapter><service-activator><outbound-gateway> 等)并发送确认电子邮件,然后将数据存储到数据库。任何你想要和需要的东西!