【发布时间】:2020-01-19 05:15:04
【问题描述】:
这是我的 context.xml - 它按预期工作,我可以将收到的电子邮件提取到我的 gmail 帐户
但主要问题是我尝试了许多解决方案来获取电子邮件的正文,但我失败了 getContent() give null pointer exception with all method found。
有谁遇到过这个问题并知道如何解决?
<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>
<!-- In case you didn't notice: should-delete-messages="false" will make it not delete your emails from the inbox, and should-mark-messages-as-read="true" will mark them as read. -->
<int-mail:inbound-channel-adapter
id="imapAdapter"
store-uri="${imap.uri}"
channel="recieveEmailChannel"
should-delete-messages="false"
should-mark-messages-as-read="true"
auto-startup="true"
java-mail-properties="javaMailProperties">
<int:poller fixed-delay="${imap.poolerSecondsDelay}" time-unit="SECONDS" />
</int-mail:inbound-channel-adapter>
<int:channel id="recieveEmailChannel">
<int:interceptors>
<int:wire-tap channel="logger" />
</int:interceptors>
</int:channel>
<int:logging-channel-adapter id="logger"
level="DEBUG" />
<int:service-activator
input-channel="recieveEmailChannel" ref="emailReceiverService"
method="receive" />
<bean id="emailReceiverService"
class="com.dtp.integration.EmailReceiverService">
</bean>
</beans>
这是我试过的代码
public void receive(Message<?> message) throws MessagingException, IOException {
MimeMessage mimeMessage = (MimeMessage) message.getPayload();
mimeMessage.getAllHeaderLines();
String messageContent = getTextFromMessage(mimeMessage);
}
private String getTextFromMessage(MimeMessage message) throws MessagingException, IOException {
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 String getTextFromMimeMultipart(
MimeMultipart mimeMultipart) throws MessagingException, IOException{
String result = "";
int count = mimeMultipart.getCount();
for (int i = 0; i < count; i++) {
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
if (bodyPart.isMimeType("text/plain")) {
result = result + "\n" + bodyPart.getContent();
break; // without break same text appears twice in my tests
} 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;
}
【问题讨论】:
-
您介意分享有关此事的堆栈跟踪吗?
-
感谢@ArtemBilan 的贡献,问题在于消息有效负载的内容始终为空,最后我发现我应该将这个简单的行放在我的入站适配器配置
simple-content="true"跨度> -
好。将其放入答案并接受自己。它真的对其他人有用
-
您可能还会发现这个JavaMail FAQ entry 很有用。
-
@BillShannon 谢谢你,是的,它对于 MimeMessage 解析也很有用,但没有这个
simple-content="true"它总是会给出空指针异常。
标签: spring spring-integration jakarta-mail mime-message