java.lang.LinkageError:加载程序约束冲突:之前启动了名为“javax/mail/Session”的不同类型的加载

2024-03-25

当我尝试使用发送电子邮件时收到以下错误javax.mail-api:

Caused by: java.lang.LinkageError: loader constraint violation: loader (instance of org/apache/felix/framework/BundleWiringImpl$BundleClassLoaderJava5) previously initiated loading for a different type with name "javax/mail/Session"
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
        at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.findClass(BundleWiringImpl.java:2279)
        at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1501)
        at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
        at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        at com.sun.mail.util.PropUtil.getBooleanSessionProperty(PropUtil.java:86)
        at javax.mail.internet.MimeMessage.initStrict(MimeMessage.java:320)
        at javax.mail.internet.MimeMessage.<init>(MimeMessage.java:195)
        at sendEmail(manage.java:216)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:408)
        at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:279)
        at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:252)

Code:

Public void sendEmail() {
    String to = "[email protected] /cdn-cgi/l/email-protection";
    String from = "[email protected] /cdn-cgi/l/email-protection";
    final String username = "[email protected] /cdn-cgi/l/email-protection";
    final String password = "password";
    Properties properties = new Properties();
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", "smtp.office365.com");
    properties.put("mail.smtp.port", "587");
    Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });
    try {
        System.out.println("Inside test");
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        message.setSubject("Testing Subject");
        message.setText("This is message body");
        Transport.send(message);
        System.out.println("Sent message successfully....");
     } catch (MessagingException e) {
         throw new RuntimeException(e);
     }
}

我的 Maven 依赖项:

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>javax.mail-api</artifactId>
    <version>1.5.5</version>
</dependency>

请帮忙。


正如评论中所建议的,将您的依赖项添加到 javamailprovided依赖性:

<dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.5.1</version>
        <scope>provided</scope>
</dependency>

这将跳过添加重复的 jar,然后由不同的类加载器加载。

如果不是以某种方式被迫使用旧版本的javamail,您应该更新到当前的最新版本

<dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.5.5</version>
        <scope>provided</scope>
</dependency>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

java.lang.LinkageError:加载程序约束冲突:之前启动了名为“javax/mail/Session”的不同类型的加载 的相关文章

随机推荐