从 mime 消息中获取消息内容?
Get message content from mime message?
我有一个 java spring 集成项目,它通过以下代码接收电子邮件:
ClassPathXmlApplicationContext ac =
new ClassPathXmlApplicationContext(
"/integration/gmail-imap-idle-config.xml");
DirectChannel inputChannel = ac.getBean("receiveChannel", DirectChannel.class);
inputChannel.subscribe(message -> {
org.springframework.messaging.Message<MimeMailMessage> received =
(org.springframework.messaging.Message<MimeMailMessage>) message;
log.info("content" + message);
List<String> sentences = null;
try {
} catch (Exception e) {
}
我收到了电子邮件,我可以得到主题,但我永远无法真正提取邮件正文。我该怎么做呢?
谢谢!
您必须在通道适配器上使用此选项:
simple-content="true"
查看其描述:
When 'true', messages produced by the source will be rendered by 'MimeMessage.getContent()'
which is usually just the body for a simple text email. When false (default) the content
is rendered by the 'getContent()' method on the actual message returned by the underlying
javamail implementation.
For example, an IMAP message is rendered with some message headers.
This attribute is provided so that users can enable the previous behavior, which just
rendered the body.
但这仍然值得怀疑,因为我看到 GMail 消息从来都不是简单的。内容是一个MimeMultipart
,我们需要阅读它的部分才能访问实体。
所以,这也是您更改代码的方式:
log.info("content" + ((MimeMultipart) ((MimeMessage) message.getPayload()).getContent()).getBodyPart(0).getContent());
我有一个 java spring 集成项目,它通过以下代码接收电子邮件:
ClassPathXmlApplicationContext ac =
new ClassPathXmlApplicationContext(
"/integration/gmail-imap-idle-config.xml");
DirectChannel inputChannel = ac.getBean("receiveChannel", DirectChannel.class);
inputChannel.subscribe(message -> {
org.springframework.messaging.Message<MimeMailMessage> received =
(org.springframework.messaging.Message<MimeMailMessage>) message;
log.info("content" + message);
List<String> sentences = null;
try {
} catch (Exception e) {
}
我收到了电子邮件,我可以得到主题,但我永远无法真正提取邮件正文。我该怎么做呢? 谢谢!
您必须在通道适配器上使用此选项:
simple-content="true"
查看其描述:
When 'true', messages produced by the source will be rendered by 'MimeMessage.getContent()' which is usually just the body for a simple text email. When false (default) the content is rendered by the 'getContent()' method on the actual message returned by the underlying javamail implementation. For example, an IMAP message is rendered with some message headers. This attribute is provided so that users can enable the previous behavior, which just rendered the body.
但这仍然值得怀疑,因为我看到 GMail 消息从来都不是简单的。内容是一个MimeMultipart
,我们需要阅读它的部分才能访问实体。
所以,这也是您更改代码的方式:
log.info("content" + ((MimeMultipart) ((MimeMessage) message.getPayload()).getContent()).getBodyPart(0).getContent());