我正在尝试从gmail服务器接收电子邮件,然后以某种方式处理它们。因此,每当新电子邮件到达邮件服务器时,我的应用程序都应下载并处理它,这意味着将异步地调用其他服务,这些服务将被注册为侦听器。
我在Spring集成方面还很陌生,我不完全了解它是如何工作的或者它是否正确。
[1)到目前为止,我有这段代码-我能够阅读所有电子邮件,但不确定是否<,或者这是否正确? ListeningExample类
@Configuration
public class ListeningExample {
@Bean
public HeaderMapper<MimeMessage> mailHeaderMapper() {
return new DefaultMailHeaderMapper();
}
@Bean
public IntegrationFlow imapMailFlow() {
IntegrationFlow flow = IntegrationFlows
.from(Mail.imapInboundAdapter("imap://user:[email protected]/INBOX")
.userFlag("testSIUserFlag")
.javaMailProperties(javaMailProperties()),
e -> e.autoStartup(true)
.poller(p -> p.fixedDelay(5000)))dostane to detailni zpravy
.transform(Mail.toStringTransformer())
.channel(MessageChannels.queue("imapChannel"))
.get();
return flow;
}
@Bean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata defaultPoller() {
PollerMetadata pollerMetadata = new PollerMetadata();
pollerMetadata.setTrigger(new PeriodicTrigger(1000));
return pollerMetadata;
}
}
MailRecieverService类
@Service
public class MailRecieverService {
private List<EmailAction> services;
@Bean
@ServiceActivator(inputChannel = "imapChannel")
public MessageHandler processNewEmail() {
MessageHandler messageHandler = new MessageHandler() {
@Override
public void handleMessage(org.springframework.messaging.Message<?> message) throws MessagingException {
System.out.println("New email:" + message.toString());
//Processing emails do with them something..
for (EmailAction emailAction : services) {
emailAction.performAction(null);
}
}
};
return messageHandler;
}
}
主类
@SpringBootApplication
@EnableIntegration
public class Main extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Main.class);
}
public static void main(String[] args) throws Exception {
SpringApplicationBuilder builder = new SpringApplicationBuilder(Main.class);
builder.headless(false).run(args);
}
}
如果是,请提供一些例子吗?我正在尝试从gmail服务器接收电子邮件,然后以某种方式处理它们。因此,每当新电子邮件到达邮件服务器时,我的应用程序都应下载并处理它,这意味着调用...2)在春季启动中,是否还可以移动/删除电子邮件,创建文件夹以及使用电子邮件帐户执行其他操作,或者我必须自己使用
javax.mail
库?
for (EmailAction emailAction : services) {
,则需要考虑自己将这一部分设为异步。您绝对可以将PublishSubscribeChannel
与Executor
和几个订户一起使用,以真正在那些服务中并行处理同一封电子邮件。