简体   繁体   中英

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

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


}`

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM