java - 带有数据库的spring boot邮件接收器
问题描述
嗨,我正在尝试使用 spring 邮件集成来读取最终用户的邮箱。我使用了以下代码,它工作正常。
@SpringBootApplication
public class ImapTestApplication {
public static void main(String[] args) {
SpringApplication.run(ImapTestApplication.class, args);
ApplicationContext ac = new ClassPathXmlApplicationContext("/META-INF/gmail-imap-idle-config.xml");
DirectChannel inputChannel = ac.getBean("receiveChannel", DirectChannel.class);
inputChannel.subscribe(new MessageHandler() {
public void handleMessage(Message<?> message) throws MessagingException {
System.out.println("===================================");
System.out.println("Message: " + message);
System.out.println("===================================");
}
});
}
还有我的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 http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail.xsd
http://www.springframework.org/schema/util http://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" />
<!-- replace 'userid and 'password' with the real values -->
<int-mail:imap-idle-channel-adapter id="customAdapter"
store-uri="imaps://mailaddress:password@host: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.imaps.auth">true</prop>
<prop key="mail.debug">false</prop>
</util:properties>
</beans>
在我的应用程序中,我的数据库中有 100 多个用户邮箱信息。那么我需要如何从我的数据库进行配置。而且在运行时,如果用户更改密码更改等邮件设置,它也应该执行我的邮件接收器,我尝试使用 Spring Integration Java DSL 来实现,但我不能使用用户邮件数据库中的 imap 设置。
解决方案
使用 XML 配置确实没有简单的方法可以做到这一点。
在此处查看一些示例:https ://github.com/spring-projects/spring-integration-samples/tree/master/advanced/dynamic-ftp
为每个客户创建一个新的应用程序上下文。
使用 Java DSL 及其动态流功能,可以更轻松地IntegrationFlow
在运行时从任意代码中编写一些内容。
您还可以将它们映射到这些用户,以便在属性更改时能够销毁并创建一个新用户。
推荐阅读
- react-native - TypeError:网络请求在本机中失败
- javascript - 从在 react-native 外部呈现的另一个下拉列表中一次更改地图函数内所有下拉列表的值
- asp.net-web-api - 后台任务中的 App Insights 内存泄漏
- python - 如何使用 Pandas (.map) 进行标记?
- wifi - 我的互联网出了什么问题,我曾经能够使用我的 cox 互联网在游戏中获得 <20 ping。现在我每 30 秒延迟一次,必须重新连接?
- html - 在 xamarin 表单中加载页面后,HTML 标签文本不会呈现
- windows - 使用需要交互的弹出屏幕远程安装软件安装程序
- python - 试图覆盖在 python 3.9 中识别的 Django 3.2 应用程序上的 allauth 模板
- python - 在循环中创建 Tkinter 按钮并为每个按钮设置命令以获取循环的索引
- c - 该程序在第 37 行显示错误,我试图显示一个字符,但它说它是一个整数。因此,它不显示第 37 行并跳过它