如何使用带有 DSL 配置的 spring 集成邮件在邮件中添加附件

How to add attachments in mail using spring integration mail with DSL configuration

我是 Spring 集成的新手,尝试使用 spring 集成邮件依赖项和 DSL 配置来发送带附件的电子邮件 (excel sheet)。 我的问题是我不知道如何在 Mail.outboundadapter IntegrationFlow 中添加附件。是否有人有附件示例 post 或分享一些示例代码?我浏览了 spring 文档,无法理解附件概念 there.Below 是我的代码。

SampleClass.java

@EnableAutoConfiguration 
@SpringBootApplication(scanBasePackages = { "c.v.flan"})
@IntegrationComponentScan
public class SampleClass {

    public static void main(String[] args)  {
        new SpringApplicationBuilder(SampleClass.class)
                .web(WebApplicationType.SERVLET)
                .run(args);

    }

    @Bean
    @Qualifier("get_send_channel")
    MessageChannel getSendChannel() {
        return MessageChannels.direct().get();
    }





    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        JavaMailSender m = new JavaMailSender() {

            @Override
            public void send(SimpleMailMessage[] arg0) throws MailException {
                // TODO Auto-generated method stub

            }

            @Override
            public void send(SimpleMailMessage arg0) throws MailException {
                // TODO Auto-generated method stub

            }

            @Override
            public void send(MimeMessagePreparator[] arg0) throws MailException {
                // TODO Auto-generated method stub

            }

            @Override
            public void send(MimeMessagePreparator arg0) throws MailException {
                // TODO Auto-generated method stub

            }

            @Override
            public void send(MimeMessage[] arg0) throws MailException {
                // TODO Auto-generated method stub

            }

            @Override
            public void send(MimeMessage arg0) throws MailException {
                // TODO Auto-generated method stub

            }

            @Override
            public MimeMessage createMimeMessage(InputStream arg0) throws MailException {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public MimeMessage createMimeMessage() {
                // TODO Auto-generated method stub
                return null;
            }
        };
        MimeMessage mimeMessage = m.createMimeMessage();
           MailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setTo("da.a");
            mailMessage.setFrom("d.cfa");
            mailMessage.setText("test");
            mailMessage.setSubject("tedt syb");

        System.out.println("inside the command liner ");
        return args -> {
            Message<?> message = MessageBuilder.withPayload(mailMessage).build();
            System.out.println("Message [ayload =>"+ message.getPayload());
            getSendChannel().send(message);
//          System.out.println("we are getting out in demosi "+getReceiveChannel().receive().getPayload());
        };
    }




    @Bean
    public IntegrationFlow sendMailFlowl() {

        return IntegrationFlows.from(getSendChannel())
                .enrichHeaders(Mail.headers().
                        subjectFunction(m -> "Sub test").from("sue@hm.com")
                        .toFunction(m -> new String[] { "1@gm"})
                        // Dont know how to add function and point to an excel sheet 
                        // .attachmentFilenameFunction(attachmentFilename)
                        )
                .handle(Mail.outboundAdapter("c.v.com").port(245)
                        .credentials("ccc@gm", "Twenty21").protocol("smtp"),
                        e -> e.id("endCHannel"))
                .get();
    }


}`

查看文档:https://docs.spring.io/spring-integration/docs/current/reference/html/mail.html#mail

MailSendingMessageHandler has various mapping strategies that use Spring’s MailMessage abstraction. If the received message’s payload is already a MailMessage instance, it is sent directly. Therefore, we generally recommend that you precede this consumer with a transformer for non-trivial MailMessage construction requirements. However, Spring Integration supports a few simple message mapping strategies. For example, if the message payload is a byte array, that is mapped to an attachment.

关于此事的代码是这样的:

    MimeMessage mimeMessage = ((JavaMailSender) this.mailSender).createMimeMessage();
    try {
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, multipartMode);
        helper.addAttachment(attachmentFileName, new ByteArrayResource(message.getPayload()));
        return new MimeMailMessage(helper);
    }
    catch (MessagingException e) {
        throw new MessageMappingException(message, "failed to create MimeMessage", e);
    }

因此,您需要将 mailSender 作为一个独立的 bean。将它注入到你的一些转换器中,并使用代码来构建这样一个 MimeMailMessage 和那个 Mail.outboundAdapter() 。请参阅 JavaMailSenderImpl 如何执行此操作。

另请参阅此文档以获取更多信息:https://docs.spring.io/spring/docs/5.2.6.RELEASE/spring-framework-reference/integration.html#mail-javamail-mime-attachments