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



我是 Spring 集成的新手,尝试使用 Spring 集成邮件依赖项和 DSL 配置发送带有附件(excel 表(的电子邮件。 我的问题是我不知道如何在Mail.outboundadapterIntegrationFlow中添加附件。 是否有人有附件示例,请发布它或分享一些示例代码?我已经浏览了春季文档,无法理解那里的附件概念。下面是我的代码。

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有各种使用SpringMailMessage抽象的映射策略。如果收到的消息的有效负载已经是MailMessage实例,则会直接发送。因此,我们通常建议您在此消费者之前使用变压器,以满足非平凡的MailMessage结构要求。但是,Spring 集成支持一些简单的消息映射策略。例如,如果邮件有效负载是映射到附件的字节数组。

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

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。使用代码将其注入到一些转换器中,以构建这样的MimeMailMessageMail.outboundAdapter()。请参阅JavaMailSenderImpl如何执行此操作。

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

相关内容

  • 没有找到相关文章

最新更新