gpt4 book ai didi

spring-integration - Spring集成IMAP-文件夹关闭异常

转载 作者:行者123 更新时间:2023-12-04 07:58:23 25 4
gpt4 key购买 nike

我正在尝试使用 spring 集成获取基本的 Imapmailreceiver,但我无意中发现了一个粘性文件夹关闭异常。阅读电子邮件内容时,我经常收到文件夹关闭异常,但我能够正确获取主题。

非常感谢任何帮助。

<强>1。我的应用上下文如下

<int:channel id="emails"/>

<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>
<int-mail:imap-idle-channel-adapter id="mailAdapter"
store-uri="ACTUALCREDENTIALHERE"
java-mail-properties="javaMailProperties"
channel="emails"
should-delete-messages="true"
auto-startup="false"
should-mark-messages-as-read="true" />
<int:service-activator id="receiveEmail" input-channel="emails" ref="MessageReceiver"/>
<bean id="MessageReceiver" class="org.springframework.integration.mail.imapidle.MessageReceiver" />
</beans>

<强>2。主APP如下

    ApplicationContext ac = new ClassPathXmlApplicationContext("/META-INF/spring/integration/gmail.xml");


ImapIdleChannelAdapter ic=(ImapIdleChannelAdapter)ac
.getBean("mailAdapter");

ic.start();

<强>3。服务激活器如下

public class MessageReceiver {
public void handle(javax.mail.Message message){
MailToStringTransformer m2s = new MailToStringTransformer();
m2s.setCharset("utf-8");


//payload instanceof MimeMultipart
if (message instanceof MimeMessage) {
try {
System.out.println("inside mimemessagehandler");
// javax.mail.Message mailMessage = (javax.mail.Message) payload;
System.out.println(message.getSubject());
System.out.println(getTextFromMessage(message));

Address[] receipts = message.getAllRecipients();
System.out.println("RECEIPIENTS MAIL ID");
if (receipts != null && receipts.length > 0) {
for (int i = 0; i < receipts.length; i++) {
System.out.println(((InternetAddress) receipts[i]).getAddress());
}
}

System.out.println("FROM MAIL ID");
Address[] froms = message.getFrom();
String email = froms == null ? null
: ((InternetAddress) froms[0]).getAddress();
System.out.println(email);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}



private static String getTextFromMessage(javax.mail.Message message) throws Exception {
String result = "";
if (message.isMimeType("text/plain")) {
result = message.getContent().toString();
} else if (message.isMimeType("multipart/*")) {
MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();
result = getTextFromMimeMultipart(mimeMultipart);
}
return result;
}

private static String getTextFromMimeMultipart(MimeMultipart mimeMultipart) throws Exception {
String result = "";

int count = mimeMultipart.getCount();
System.out.println("CCC "+count);
for (int i = 0; i < count; i++) {
// mimeMultipart.toString()
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
if (bodyPart.isMimeType("text/plain")) {
result = mimeMultipart.toString();//(String) bodyPart.getContent();

} else if (bodyPart.isMimeType("text/html")) {
String html = (String) bodyPart.getContent();
// result = result + "\n" + org.jsoup.Jsoup.parse(html).text();
} else if (bodyPart.getContent() instanceof MimeMultipart) {
result = result + getTextFromMimeMultipart((MimeMultipart) bodyPart.getContent());
}
}
return result;
}

}

--- 异常(exception)情况

javax.mail.FolderClosedException

at javax.mail.internet.MimeBodyPart.getContent(MimeBodyPart.java:659)
at org.springframework.integration.mail.imapidle.MessageReceiver.getTextFromMimeMultipart(MessageReceiver.java:101)
at org.springframework.integration.mail.imapidle.MessageReceiver.getTextFromMessage(MessageReceiver.java:83)
at org.springframework.integration.mail.imapidle.MessageReceiver.handle(MessageReceiver.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at

最佳答案

好吧,如果 Folder 已经关闭,看起来我们无法从 MimeBodyPart 获取内容。看起来我们在 AbstractMailReceiver.receive() 中遇到了这种情况:

            }
return converted;
}
else {
return filteredMessages;
}
}
finally {
MailTransportUtils.closeFolder(this.folder, this.shouldDeleteMessages);
}

不确定为什么我们在接收后但在发送数据流之前关闭那里的文件夹,不过我建议您尝试为 ImapMailReceiver 使用 DefaultMailHeaderMapper。在那种情况下,MimeMessage 的内容在提到的 AbstractMailReceiver.receive() 内部转换为 org.springframework.messaging.Message:

if (this.headerMapper != null) {
org.springframework.messaging.Message<?>[] converted =
new org.springframework.messaging.Message<?>[filteredMessages.length];
int n = 0;
for (MimeMessage message : filteredMessages) {
Map<String, Object> headers = this.headerMapper.toHeaders(message);
converted[n++] = getMessageBuilderFactory().withPayload(extractContent(message, headers))
.copyHeaders(headers)
.build();
}
return converted;
}

与此同时,请提出 JIRA就此事,我们考虑修复 Folder.close() 不一致的问题。在我看来,根本不应该这样做,或者我们应该将它作为标题提供给最终用户,让他/她决定何时何地关闭文件夹。

关于spring-integration - Spring集成IMAP-文件夹关闭异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44640449/

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