您的代码显示您忽略了消息本身的关键部分,即payload
- 将您的出站消息适配器与
JavaMailSender
(您可能已经拥有)
MimeMessage
使用org.springframework.mail.javamail.JavaMailSender
你连线的 and构造一个正则org.springframework.mail.javamail.MimeMessageHelper
,如
public void sendEmail(){
MimeMessage message = mailSender.createMimeMessage(); //mailSender is an instance of JavaMailSender from 1.
try{
MimeMessageHelper helper = new MimeMessageHelper(message,true);
//set the fields
helper.setFrom("J'onn Doe");
helper.setTo("SpamRecce");
helper.setSubject("")
helper.setText("Text goes here")
//load attachment file using spring's help. Basic interface is called a Resource and there are many implementations. Here, I've used the ClassPathResource which means your attachment file must be in your classpath
ClassPathResource theAttachment = new ClassPathResource("My File.Doc");
helper.addAttachment("Add the desired filename here.doc",theAttachment);
}
catch(MessagingException me){
Logger.getAnonymousLogger.info("Something bad happened!);
me.printStacktrace();
}
mailSender.send(message);
}
我不知道您的实施细节,但我相信发送电子邮件的端点应该是终止端点,即可以直接从该端点发送电子邮件,您不需要发送到另一个通道。
但是,如果您仍然需要将消息刷新到另一个通道,则需要重写您要阅读的代码:
Message<MimeMessage> msg = MessageBuilder.withPayload(message)
.setHeader(MailHeaders.SUBJECT, subject )
.setHeader(MailHeaders.ATTACHMENT_FILENAME, args[0].toString())
.build();