这是生成错误的代码:
import com.cisco.s3.util.Logging;
import javax.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class EmailController {
private static final Logger log = Logging.getLogger(EmailController.class);
@Autowired
private JavaMailSender sender;
@RequestMapping("/simpleemail")
@ResponseBody
String home() {
try {
sendEmail();
return "Email Sent!";
}catch(Exception ex) {
return "Error in sending email: " + ex;
}
}
private void sendEmail() throws Exception{
MimeMessage message = sender.createMimeMessage();
// Enable the multipart flag!
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setTo("[email protected]");
helper.setFrom("[email protected]");
helper.setText("How are you?");
helper.setSubject("Hi");
ClassPathResource file = new ClassPathResource("cat.jpg");
helper.addAttachment("eagle.jpg", file);
sender.send(message);
}
}
错误很长,让我添加一个错误:
```
[ERROR] 2019-05-22 09:14:17 @ org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: javax.mail.Session$1: method <init>()V not found] with root cause
java.lang.NoSuchMethodError: javax.mail.Session$1: method <init>()V not found
at javax.mail.Session.<clinit>(Session.java:240) ~[soar-toolbox-2.0.0-beta.31.jar!/:na]
at org.springframework.mail.javamail.JavaMailSenderImpl.getSession(JavaMailSenderImpl.java:161) ~[spring-context-support-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at org.springframework.mail.javamail.JavaMailSenderImpl.createMimeMessage(JavaMailSenderImpl.java:339) ~[spring-context-support-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at com.cisco.s3.controller.EmailController.sendEmail(EmailController.java:44) ~[classes!/:na]
at com.cisco.s3.controller.EmailController.home(EmailController.java:36) ~[classes!/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_191]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_191]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_191]
```
我已经阅读了很多有关同一项目的内容,似乎在某个地方存在兼容性问题,但我一直无法对其进行跟踪。
我正在使用gradle进行依赖:
dependencies {
compile 'com.cisco.s3:soar-toolbox:2.0.0-beta.31'
compile 'org.apache.tomcat.embed:tomcat-embed-jasper'
compile 'org.slf4j:jcl-over-slf4j'
compile 'org.slf4j:jul-to-slf4j'
compile 'org.springframework.boot:spring-boot-starter-actuator'
compile 'org.springframework.boot:spring-boot-starter-security'
compile 'org.springframework.boot:spring-boot-starter-web'
compile'org.springframework.boot:spring-boot-starter-mail'
compile 'org.springframework.security:spring-security-ldap'
compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.10.2'
testCompile 'org.springframework.boot:spring-boot-starter-test'
testCompile 'org.springframework.security:spring-security-test'
}
自一周前以来,我一直试图解决此问题,但成功率为零。
谢谢!!!
最佳答案
NoSuchMethodError
表示com.cisco.s3:soar-toolbox:2.0.0-beta.31
依赖项与您正在运行的javax.mail
版本不兼容。soar-toolbox
看起来是专有的,因此您必须参考Cisco提供的文档以查看支持哪些Java和Java EE版本。