使用 Spring 集成通过电子邮件读取文件和发送内容

Read file and send contents via Email using Spring Integration

我是 Spring 集成的新手,我想读取文件的内容并将其用作电子邮件的 body/text。这是使用 Spring 的最新版本(不确定确切的版本号)。请注意,我只会根据 Spring 库和 类 本身

中可用的内容来使用 Spring XML 配置文件

这是我目前尝试过的方法:

<int-file:inbound-channel-adapter id="filesReader"
                                  directory="file:/C:/data/inputDirectory"
                                  filename-pattern="*.txt"
                                  channel="filesIn"
                                  prevent-duplicates="true">

    <int:poller id="poller" fixed-delay="1000" max-messages-per-poll="1" />
</int-file:inbound-channel-adapter>

<!-- convert contents to string --> 
<file:file-to-string-transformer input-channel="filesIn" output-channel="outputContent" delete-files="true"/>

<!-- output content to a file so the payload can be read by the logger -->
<int-file:outbound-channel-adapter id="filesOut" directory="file:/C:/data/outputDirectory" channel="outputContent"/>

<int:channel id="outputContent">
    <int:interceptors>
        <int:wire-tap channel="LoggingChannel" />
    </int:interceptors>
</int:channel>

<int:channel id="LoggingChannel" />
<int:service-activator input-channel="LoggingChannel" expression="@LOG.trace('LOG Payload is: {} ', payload)" />

<bean id="LOG" class="SafeLoggerFactory" factory-method="getLogger">
    <constructor-arg value="integrationEmailLogger"/>
</bean> 

<int:channel id="mailMessageChannel"/>

<int:header-enricher input-channel="outputContent" output-channel="mailMessageChannel">
    <int:header name="mail_from" value="${email}"/>
    <int:header name="mail_subject" value="Subject goes here"/>
    <int:header name="mail_to" value="${email}"/>
</int:header-enricher>

<mail:outbound-channel-adapter channel="mailMessageChannel" mail-sender="mailSender">
    <mail:request-handler-advice-chain>
        <bean class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice">
            <property name="onSuccessExpression" value="payload.length"/>
            <property name="onFailureExpression" value="payload.length"/>
        </bean>
    </mail:request-handler-advice-chain>
</mail:outbound-channel-adapter>

<!--<int:channel id="outboundMailChannel" />
<int:transformer method="transformToMimeMessage" input-channel="outputContent" output-channel="outboundMailChannel"/>

<mail:outbound-channel-adapter mail-sender="mailSender" channel="outboundMailChannel">
    <mail:request-handler-advice-chain>
        <int:retry-advice max-attempts="5">
            <int:fixed-back-off interval="10000"/>
        </int:retry-advice>
    </mail:request-handler-advice-chain>
</mail:outbound-channel-adapter>-->

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.gmail.com"/>
    <property name="port" value="587"/>
    <property name="protocol" value="smtp"/>
    <property name="username" value="${email}"/>
    <property name="password" value="${password}"/>
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.debug">true</prop>
            <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
            <prop key="mail.smtp.starttls.required">true</prop>
        </props>
    </property>
</bean>

我可以将文件的内容打印到日志文件中,但我的 gmail 帐户似乎没有发送任何电子邮件。任何指针将不胜感激

到目前为止,我看到您有一个 outputContent 作为 DirectChannel,这个有两个订阅者:<int-file:outbound-channel-adapter id="filesOut"> 和 <int:header-enricher

默认情况下,DirectChannel 为其订阅者应用循环 消息调度。因此,第一条消息发送给第一个订阅者,第二个发送给第二个,第三个发送给第一个。

请考虑不要使用 int-file:outbound-channel-adapter:您刚刚阅读了 <int-file:inbound-channel-adapter 的文件。另外我看到你看完就删了。那么,如果您的目标是发送电子邮件,将其写回到其他目录有什么意义呢?