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

非 Y 不嫁゛ 提交于 2021-01-28 13:40:31

问题


I am new to Spring integration, trying to send an email with attachment (excel sheet) using spring integration mail dependency with DSL configuration. My problem is I don't know how to add the attachments in Mail.outboundadapter IntegrationFlow. Is anyone having the attachment sample please post it or share some sample code? I have gone through the spring docs, could not understand the attachment concept there.Below is my code.

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();
    }


}`

回答1:


See docs: 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.

The code over on the matter is like this:

    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);
    }

So, you need to have that mailSender as an independent bean. Inject it into some your transformer with the code to build such a MimeMailMessage and into that Mail.outboundAdapter() as well. See JavaMailSenderImpl how to do that.

Also see this docs for more info: https://docs.spring.io/spring/docs/5.2.6.RELEASE/spring-framework-reference/integration.html#mail-javamail-mime-attachments



来源:https://stackoverflow.com/questions/61874126/how-to-add-attachments-in-mail-using-spring-integration-mail-with-dsl-configurat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!