gpt4 book ai didi

java - 使用 Spring 集成接收邮件?

转载 作者:行者123 更新时间:2023-12-01 18:17:02 24 4
gpt4 key购买 nike

我在 spring 集成中有一个简单的项目正在尝试接收邮件:

@Service
public class MailReceiverService implements CommandLineRunner {

@Bean
TestMailServer.ImapServer imapServer() {
return TestMailServer.imap(0);
}

private static Log logger = LogFactory.getLog(MailReceiverService.class);

public static void manageMessage() {

@SuppressWarnings("resource")
ClassPathXmlApplicationContext ac =
new ClassPathXmlApplicationContext(
"/integration/gmail-imap-idle-config.xml");
DirectChannel inputChannel = ac.getBean("receiveChannel", DirectChannel.class);


inputChannel.subscribe(message -> {
logger.info("Message: " + message);
});



}

@Override
public void run(String... args) {
manageMessage();
}


}

gmail-imap-idle-config.xml 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/mail https://www.springframework.org/schema/integration/mail/spring-integration-mail.xsd
http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-mail="http://www.springframework.org/schema/integration/mail"
xmlns:util="http://www.springframework.org/schema/util">

<int:channel id="receiveChannel" />
<int-mail:imap-idle-channel-adapter id="customAdapter"
store-uri="imaps://username:password@imap.gmail.com:993/inbox"
channel="receiveChannel"
auto-startup="true"
should-delete-messages="false"
should-mark-messages-as-read="false"
java-mail-properties="javaMailProperties"/>

<util:properties id="javaMailProperties">
<prop key="mail.imap.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.imap.socketFactory.fallback">false</prop>
<prop key="mail.store.protocol">imaps</prop>
<prop key="mail.debug">false</prop>
</util:properties>

</beans>

但是当我运行它时,我收到以下消息:

channel “application.errorChannel”有 0 个订阅者。停止 bean '_org.springframework.integration.errorLogger'

然后它就关闭了!关于如何让它发挥作用有什么建议吗?谢谢!

最佳答案

您的问题是您没有使应用程序保持 Activity 状态。 <int-mail:imap-idle-channel-adapter>在后台启动一个长期运行的进程。但是,当您启动应用程序时,您会立即离开主线程,然后主线程就会退出。与此同时,您会从电子邮箱收到一条消息,但订阅者是 receiveChannel已经消失了,因为您的应用程序根据主线程行为处于关闭状态。

考虑设置一些障碍 run()不要让主线程退出。

在示例中,我们通常有这样的内容:

System.out.println("Hit 'Enter' to terminate");
System.in.read();
ctx.close();

这样您就不会让 JVM 退出,因此您将收到电子邮件,直到您终止应用程序。

关于java - 使用 Spring 集成接收邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60347757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com