【问题标题】:NoClassDefFoundError: javax/mail/SessionNoClassDefFoundError: javax/mail/Session
【发布时间】:2019-07-23 10:40:07
【问题描述】:

我正在尝试发送一封包含表单数据的电子邮件。目前正在尝试使用Gmail,目标是从本地服务器发送。但这给了我一个例外。我已将.jar 添加到库中。对不起,如果这个问题很愚蠢,我是新手。

如何一步步解决?


    public void sendMail(String from, String password, String recipient, FormModel formModel) {
            Properties properties = new Properties();

            properties.put("mail.smtp.auth", "true");
            properties.put("mail.smtp.starttls", "true");
            properties.put("mail.smtp.host", "smtp.gmail.com");
            properties.put("mail.smtp.port", "587");

            Session session = Session.getInstance(properties, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(from, pass);
                }
            });


            Message message = pmessage(session, from, recipient);
            try {
                Transport.send(message);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }

        private Message pmessage(Session session, String myaccount, String recipient) {
            Message message = new MimeMessage(session);
            try {
                message.setFrom(new InternetAddress(myaccount));
                message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
                message.setSubject("formular");
                message.setText("TEST");
                return message;
            } catch (AddressException e) {
                e.printStackTrace();
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            return null;
    }

预期的输出是发送邮件。

【问题讨论】:

  • 1) 粘贴异常 2) 你添加了什么 jar 3) 如果它是正确的 jar,它可能不在类路径上。
  • @Antoniossss thx 我对问题进行了更改
  • 所以它不在类路径上。如何更改取决于您运行应用程序的方式。
  • @Antoniossss 我正在使用 intellij idea 2019,我正在使用 (Shift+F10) 运行

标签: java jakarta-ee jakarta-mail


【解决方案1】:

尝试以下方法之一。之前为我工作过类似的问题:

  • 单击工具栏中的文件
  • 项目结构
  • 在左侧面板中选择模块
  • 依赖项选项卡选择“+”→ JAR 或目录

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-13
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多