在春季启动时发送电子邮件时出现NoSuchMethodException



我有一个带有Gradle的spring-boot应用程序。

这是我的依赖


dependencies {
compile('javax.xml.bind:jaxb-api:'+ jaxbVersion)
compile('org.apache.xmlgraphics:fop:' + apacheFOPVersion)
compile('com.amazonaws:aws-java-sdk-s3:' + amazonS3Version)
compile('org.apache.commons:commons-lang3:' + commonsLangVersion)
compile('commons-fileupload:commons-fileupload:' + commonsFileUploadVersion)
compile('org.springframework:spring-test:4.3.13.RELEASE')
compile('net.authorize:anet-java-sdk:' + anetSDKVersion)
compile('org.springframework.boot:spring-boot-starter-web')
compile('io.springfox:springfox-swagger2:' + swaggerUiVersion)
compile('io.springfox:springfox-swagger-ui:' + swaggerUiVersion)
compile('org.springframework.boot:spring-boot-starter-data-redis')
compile('org.springframework.boot:spring-boot-starter-data-mongodb')
compile('org.springframework.cloud:spring-cloud-security')
compile('org.springframework.cloud:spring-cloud-starter-config')
compile('org.springframework.cloud:spring-cloud-starter-security')
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
compile('org.springframework.security.oauth:spring-security-oauth2')
compile('org.springframework.cloud:spring-cloud-starter-openfeign')
compile files('../models/build/libs/quickboard.models-' + modelsVersion + '.jar')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile("com.intuit.quickbooks-online:ipp-v3-java-data:3.0.0")
compile(group: 'com.intuit.quickbooks-online', name: 'ipp-v3-java-devkit', version: '3.0.5', classifier: 'jar-with-dependencies')
{
exclude group: 'javax.mail', module: 'mailapi'
}
compile(group: 'com.intuit.quickbooks-online', name: 'oauth2-platform-api', version: '3.0.5', classifier: 'jar-with-dependencies')
{
exclude group: 'javax.mail', module: 'mailapi'
}
compile group: 'com.ecwid', name: 'maleorang', version: '3.0-0.9.6'
compile group: 'com.sun.mail', name: 'javax.mail', version: '1.5.0'
compileOnly 'org.projectlombok:lombok:1.18.4'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
configurations {
all*.exclude group: 'javax.mail'
}

这是我发送电子邮件的代码

Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("myemail@domain.com"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(toEmail, emailSubject));
msg.setSubject(emailSubject);
//            msg.setText(message);
msg.setContent(message, "text/html; charset=utf-8");
Transport.send(msg);

Transport.send(msg);线上,我得到了异常

java.lang.NoSuchMethodError: com.sun.mail.util.TraceInputStream.<init>(Ljava/io/InputStream;Lcom/sun/mail/util/MailLogger;)V
at com.sun.mail.smtp.SMTPTransport.initStreams(SMTPTransport.java:2016) ~[javax.mail-1.5.0.jar:1.5.0]
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1936) ~[javax.mail-1.5.0.jar:1.5.0]
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654) ~[javax.mail-1.5.0.jar:1.5.0]
at javax.mail.Service.connect(Service.java:313) ~[ipp-v3-java-devkit-3.0.5-jar-with-dependencies.jar:na]
at javax.mail.Service.connect(Service.java:172) ~[ipp-v3-java-devkit-3.0.5-jar-with-dependencies.jar:na]
at javax.mail.Service.connect(Service.java:121) ~[ipp-v3-java-devkit-3.0.5-jar-with-dependencies.jar:na]
at javax.mail.Transport.send0(Transport.java:190) ~[ipp-v3-java-devkit-3.0.5-jar-with-dependencies.jar:na]
at javax.mail.Transport.send(Transport.java:120) ~[ipp-v3-java-devkit-3.0.5-jar-with-dependencies.jar:na]

我阅读了关于这个异常的每一个主题,大多数情况下,一些依赖项包括旧版本的mailapi依赖项,并在我的build.gradle中添加了这个排除项。我正在Intellij idea中检查依赖关系树(编译时和运行时(,并且只显示了jaxa.mail依赖关系,而我在build.gradle中有这个依赖关系,其他任何依赖关系都不包括它,但我仍然得到了这个异常。

你还有其他建议吗?我应该尝试什么?原因可能是什么?

我刚刚将依赖关系compile group: 'com.sun.mail', name: 'javax.mail', version: '1.5.0'降级为compile group: 'com.sun.mail', name: 'javax.mail', version: '1.4.5'现在它工作了,看起来也没有必要将mailapi从依赖项和中排除

configurations {
all*.exclude group: 'javax.mail'
} 

代码运行良好,有或没有这些不包括

相关内容

最新更新