我正在尝试编写一些代码来读取电子邮件并提供宁静的网络服务。
我有一个简单的测试文件,可以从 gmail(通过 pop)读取消息。但是,似乎当我将 cxf-rf-frontend-jaxrs 添加到我的 pom.xml 时,它会停止该测试文件的工作。
有谁能解开这个?
谢谢,代码和内容如下。
错误是:
org.springframework.integration.core.MessagingException: failure occurred while receiving from folder
at org.springframework.integration.mail.AbstractMailReceiver.receive(AbstractMailReceiver.java:173)
at mail.GmailManualTester.testPop(GmailManualTester.java:36)
Caused by: java.lang.NullPointerException
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1166)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1135)
at javax.mail.internet.MimeMessage.<init>(MimeMessage.java:182)
at org.springframework.integration.mail.Pop3MailReceiver.deleteMessages(Pop3MailReceiver.java:78)
at org.springframework.integration.mail.AbstractMailReceiver.receive(AbstractMailReceiver.java:164)
测试文件是 src/test/java/mail/GmailManualTester.java
package mail;
import java.util.Properties;
import javax.mail.Message;
import org.junit.Test;
import org.springframework.integration.mail.Pop3MailReceiver;
public class GmailManualTester {
@Test
public void testPop() throws Exception {
Pop3MailReceiver mail = new Pop3MailReceiver(
"pop3://USERNAME%40DOMAIN:PASSWORD@pop.gmail.com:995/INBOX");
Properties props = new Properties();
props.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.pop3.socketFactory.fallback", "false");
mail.setJavaMailProperties(props);
Message[] messages = mail.receive();
System.out.println("Got " + messages.length + " messages");
for (Message message : messages) {
System.out.println("Subject: " + message.getSubject());
}
}
}
pom.xml 是
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.sf</groupId>
<artifactId>boom</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>Example</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mail</artifactId>
<version>1.0.2SR1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<debug>false</debug>
</configuration>
</plugin>
</plugins>
</build>
</project>
编辑:从 ~/.m2/.../cxf-rt-frontend-jaxrs-2.3.0.pom 文件中获取所有依赖项,并将它们作为排除项放在我自己的 pom.xml 中。打开和关闭它们,直到我发现只有一个导致它:
如果我把它放到我的 pom 文件中,我可以检索消息。如果我将其注释掉,我会从上面得到错误。不过,现在仍需要进一步深入研究。排除这可能会破坏我的应用程序中的网络服务。
<exclusion>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
</exclusion>