我知道此问题之前会被问到,但我认为这是2 - 3年前,事情发生了变化。现在没有任何代码示例正在运行。 我在Java 1.6中尝试以下代码:
Properties props = new Properties();
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.debug", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("uname","password");
}
});
session.setDebug(true);
Transport transport = session.getTransport();
InternetAddress addressFrom = new InternetAddress("abc@gmail.com");
MimeMessage message = new MimeMessage(session);
message.setSender(addressFrom);
for (int j = 0; j < 20; j++) {
message.setSubject("Testing javamail plain"+Math.random());
message.setContent("This is a test", "text/plain");
String sendTo [] = {"abc@gmail.com"};
if (sendTo != null) {
InternetAddress[] addressTo = new InternetAddress[sendTo.length];
for (int i = 0; i < sendTo.length; i++) {
addressTo[i] = new InternetAddress(sendTo[i]);
}
message.setRecipients(Message.RecipientType.TO, addressTo);
}
transport.connect();
Transport.send(message);
transport.close();
System.out.println("DONE");
}
并抛出此异常:
org.apache.cxf.interceptor.Fault: javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp
org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:155)
org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:121)
org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:164)
org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:91)
org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
java.util.concurrent.FutureTask.run(FutureTask.java:138)
org.apache.cxf.workqueue.SynchronousExecutor.execute(SynchronousExecutor.java:37)
org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:106)
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:206)
org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:218)
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:161)
org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:114)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:184)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:112)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:163)
我使用tomcat作为webserver,使用cxf framefork作为webservices。 非常感谢任何帮助。
答案 0 :(得分:6)
我假设你的类路径中有java.mail jar文件,而cxf支持jar文件。实际上CXF使用Apache Geronimo Javamail /激活实现,所以现在你的应用程序类路径中将有两个不同版本的Sun Javamail库。当你的应用程序试图发送一封电子邮件时,Java会对它应该是哪个版本的库感到困惑使用,并摔倒。所以解决方案可能是删除javamail jar文件或者在pom文件中排除geronimo javamail,如:
<exclusions>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-javamail_1.4_spec</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-activation_1.1_spec</artifactId>
</exclusion>
希望这对你有所帮助。
答案 1 :(得分:3)
这可能对您有所帮助: 如果您使用Maven
,请在pom.xml中添加以下内容<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>${cxf.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-javamail_1.4_spec</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-activation_1.1_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-databinding-jaxb</artifactId>
<version>${cxf.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-javamail_1.4_spec</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-activation_1.1_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
然后,添加Sun javamail / activation实现:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
答案 2 :(得分:0)
您需要实现javax.mail
依赖关系。如果您使用的是Maven,请将以下内容添加到pom.xml
文件中:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.4</version>
</dependency>