【问题标题】:spring integration mail processorspring 集成邮件处理器
【发布时间】:2015-06-29 14:01:56
【问题描述】:

我希望以此示例为指导来构建一种“邮件处理器”:

  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>

我应该在哪里(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{
    ...

谢谢。

【问题讨论】:

  • 看看 handleMessage() 如何实际提取电子邮件部分(正文、附件)会很有趣

标签: spring email spring-boot spring-integration


【解决方案1】:

要处理“无附件”状态,您应该真正遵循标准 Spring Integration 配方并将&lt;filter&gt; 添加到您的流程中。看起来就像在您的 &lt;splitter&gt; 之后(如果您按“Multipart”拆分您的消息)。顺便说一句,您可以在这里找到如何拆分和过滤:Download attachments using Java Mail

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

【讨论】:

    猜你喜欢
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 2018-07-01
    • 2011-05-13
    • 1970-01-01
    相关资源
    最近更新 更多