当前位置:  开发笔记 > 编程语言 > 正文

java.lang.LinkageError:违反加载程序约束:先前启动了名称为“ javax / mail / Session”的其他类型的加载

如何解决《java.lang.LinkageError:违反加载程序约束:先前启动了名称为“javax/mail/Session”的其他类型的加载》经验,为你挑选了1个好方法。

当我尝试使用发送电子邮件时,出现以下错误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.(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)

码:

Public void sendEmail() {
    String to = "abc@abc.com";
    String from = "efg@efg.com";
    final String username = "abc@abc.com";
    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依赖项:


    javax.mail
    javax.mail-api
    1.5.5

请帮忙。



1> Jan..:

如注释中所建议,将您的依赖项作为提供的依赖项添加到javamail中:


        com.sun.mail
        javax.mail
        1.5.1
        provided

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

如果不是由于某种原因被迫使用旧版本的Javamail,则应更新到最新版本


        com.sun.mail
        javax.mail
        1.5.5
        provided

推荐阅读
135369一生真爱_890
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有