Need any working example of

2019-09-09 20:44发布

问题:

Can anyone provide a working example (config) for ?

I checked "Spring Integration Reference Manual" and github where all si samples have been provided but i could not find anything for .

回答1:

Something like this:

<int:chain input-channel="inputChannel">
    <int-mail:header-enricher>
        <int-mail:to value="mailto"/>
        <int-mail:from value="mailfrom"/>
        <int-mail:subject value="With Content Type"/>
        <int-mail:content-type value="text/html"/>
    </int-mail:header-enricher>

    <int-mail:outbound-channel-adapter host="smtp.gmail.com"
                                       port="587"
                                       username="user"
                                       password="password"
                                       java-mail-properties="javaMailProperties"/>
</int:chain>


<util:properties id="javaMailProperties">
    <prop key="mail.debug">true</prop>
    <prop key="mail.smtps.auth">true</prop>
    <prop key="mail.smtp.starttls.enable">true</prop>
</util:properties>

UPDATE

That's right: the entire payload is converted to the email. Of course it is typical task to transform the payload to appropriate object before sending.

The MailSendingMessageHandler these types for payload:

if (payload instanceof MimeMessage) {
        mailMessage = new MimeMailMessage((MimeMessage) payload);
    }
    else if (payload instanceof MailMessage) {
        mailMessage = (MailMessage) payload;
    }
    else if (payload instanceof byte[]) {
        mailMessage = this.createMailMessageFromByteArrayMessage((Message<byte[]>) message);
    }
    else if (payload instanceof String) {
        String contentType = (String) message.getHeaders().get(MailHeaders.CONTENT_TYPE);
        if (StringUtils.hasText(contentType)) {
            mailMessage = this.createMailMessageWithContentType((Message<String>) message, contentType);
        }
        else {
            mailMessage = new SimpleMailMessage();
            mailMessage.setText((String) payload);
        }
    }
    else {
        throw new MessageHandlingException(message, "Unable to create MailMessage from payload type ["
                + message.getPayload().getClass().getName() + "], expected MimeMessage, MailMessage, byte array or String.");
    }


回答2:

Here's another sample extracted from a previous solution where a directory is read for files, each file converted to bytes[], then sent to mail.

    <bean id="placeholderConfig"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="locations">
        <list>
            <value>classpath:integration.properties</value>
            <!-- override property files -->
            <value>file:integration.properties</value>
        </list>
    </property>
</bean>

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="${integration.mail.host}" />
</bean>

<file:inbound-channel-adapter id="fileWorkingChannel" 
                directory="file:${integration.file.working.directory}" 
                prevent-duplicates="${integration.file.incoming.prevent.duplicate}" 
                filename-pattern="*.${integration.file.type}">
    <integration:poller fixed-rate="${integration.file.working.poll.rate}"/>
</file:inbound-channel-adapter> 


<file:file-to-bytes-transformer 
    input-channel="fileWorkingChannel"
    output-channel="mailTransportChannel"
    delete-files="true"/>

<integration:channel id="mailTransportChannel"/>

<integration:header-enricher input-channel="mailTransportChannel" output-channel="mailMessageChannel">
    <integration:header name="mail_from" value="${integration.mail.from}"/>
    <integration:header name="mail_subject" expression="'filepath:${integration.mail.file.path}' + '|' + 'filename:' + headers.file_name"/>
    <integration:header name="mail_to" value="${integration.mail.to}"/>
    <integration:header name="mail_attachmentFilename" expression="headers.file_name"/>
</integration:header-enricher>  

<integration:channel id="mailMessageChannel"/>

<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="successChannel" ref="afterSuccessChannel"/>
            <property name="onFailureExpression" value="payload.length"/>
            <property name="failureChannel" ref="afterFailChannel" />
        </bean>
        <bean id="retryAdvice" class="org.springframework.integration.handler.advice.RequestHandlerRetryAdvice">
            <property name="retryTemplate" ref="retryTemplate"/>
        </bean>
    </mail:request-handler-advice-chain>
</mail:outbound-channel-adapter>