无法从类 javax.activation.MailcapCommandMap- Spring boot 访问类 com.sun.activation.registries.LogSupport

failed to access class com.sun.activation.registries.LogSupport from class javax.activation.MailcapCommandMap- Spring boot

提问人:Vishal Srivastava 提问时间:2/9/2023 最后编辑:Vishal Srivastava 更新时间:2/10/2023 访问量:389

问:

我正在尝试使用 Spring boot 中的邮件服务通过电子邮件发送 OTP,但出现错误 -

无法从类 javax.activation.MailcapCommandMap 访问类 com.sun.activation.registries.LogSupport(com.sun.activation.registries.LogSupport 和 javax.activation.MailcapCommandMap 位于加载程序“app”的未命名模块中)

在 pom.xml- 中的依赖项下添加了

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.1</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.smart</groupId>
    <artifactId>smartcontactmanager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>smartcontactmanager</name>
    <description>Smart Contact Manager</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.sun.mail/javax.mail -->
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>8.0.0.Final</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

控制器-

@PostMapping("/send-otp")
public String sendOtp(@RequestParam("email") String email,Model model, HttpSession session) {

        System.out.println("Email: " + email);
        model.addAttribute("email", email);

        // Generating 4 digit OTP

        int otp = random.nextInt(9999);
        System.out.println("OTP: " + otp);
        
        //Code to send OTP to email
        
        String subject="OTP from Smart Contact Manager";
        String message="<h1> OTP : "+otp+" </h1>";
        String to=email;
        
        boolean flag = this.emailService.sendEmail(subject, message, to);
        
        if(flag) {
            return "verify_otp";
        }else {
            session.setAttribute("message", "Please verify your email address!");
            return "forgot_email_form";
        }

        
}


EmailService.java Class-

package com.smart.service;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.stereotype.Service;

@Service
public class EmailService {

    public boolean sendEmail(String subject, String message, String to) {

        boolean f = false;

        String from = "[email protected]";

        // variable for Gmail host
        String host = "smtp.gmail.com";

        // Get the system properties
        Properties properties = System.getProperties();
        System.out.println("System Properties: " + properties);

        // Set Host
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", "465");
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.auth", "true");

        // Step-1: To get the session object
        Session session = Session.getInstance(properties, new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("[email protected]", "zykpcbzkjcuedoju");
            }

        });

        session.setDebug(true);

        // Step-2: Compose the Message[text, multimedia]
        MimeMessage m = new MimeMessage(session);

        try {

            // From email
            m.setFrom(from);

            // Adding recipient to mail
            m.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

            // Adding subject to mail
            m.setSubject(subject);

            // Adding text to mail
            m.setText(message);

            // Step-3: Send mail
            Transport.send(m);

            System.out.println("Mail sent successfully");
            f = true;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;

    }

}
java spring-boot 电子邮件 jakarta-mail 忘记密码

评论

0赞 life888888 2/9/2023
请完整显示pom.xml
0赞 life888888 2/9/2023
如果,你会展示什么?OTP 会显示还是?int otp = 1<h1> OTP : "+otp+" </h1>";00011
0赞 Vishal Srivastava 2/9/2023
更新了完整的pom.xml和otp将从1000(最小)-9999(最大)开始

答: 暂无答案