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

NoSuchMethodException while sending email in spring boot

我有 spring 引导应用程序 Gradle。

这是我的依赖项


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 none 中都有其他依赖项包括它,但我仍然遇到此异常。

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

我刚刚将依赖项 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'
} 

代码在有或没有这些排除项的情况下都能正常工作