我在尝试使用 javax 邮件依赖项时遇到错误

I'm having an error while trying to use javax mail dependency

我正在尝试在我的项目中使用 javax 邮件。我在 Gmail 中启用了不太安全的应用程序,并制定了一个入站规则来解锁端口 465。

这是我在应用程序属性中使用的配置。

''' 
spring.mail.host=smtp.hmail.com
spring.mail.username=dumitrachesabin@gmail.com  
spring.mail.password=mypass
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.socketFactory.port=465 
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback=false
support.email=dumitrachesabin@gmail.com
'''

允许端口:

我在网上发现需要解锁端口 465,找到了如何解锁的快速指南,您可以在照片中看到。

在控制器中 class 我已设置在用户介绍他的注册凭据(用户名、电子邮件地址)后生成一封带有 link 的电子邮件到编辑个人资料页面。

当我输入用户名和邮件并点击提交时出现错误

"Unknown SMTP host: smtp.hmail.com;",

但是数据发送到数据库。我在网页上收到错误 500。

@RequestMapping(value = "/newUser", method = RequestMethod.POST)
    public String newUserPost(HttpServletRequest request,
            @ModelAttribute("email") String userEmail,
            @ModelAttribute("username") String username, Model model) throws Exception {
        model.addAttribute("classActiveNewAccount", true);
        model.addAttribute("email", userEmail);
        model.addAttribute("username", username);

        if (userService.findByUsername(username) != null) {
            model.addAttribute("usernameExists", true);

            return "myAccount";

        }

        if (userService.findByEmail(userEmail) != null) {
            model.addAttribute("email", true);
            return "myAccount";

        }

        User user = new User();
        user.setUsername(username);
        user.setEmail(userEmail);

        String password = SecurityUtility.randomPassword();

        String encryptedPassword = SecurityUtility.passowrdEncoder().encode(password);
        user.setPassword(encryptedPassword);

        Role role = new Role();
        role.setRoleId(1);
        role.setName("ROLE_USER");
        Set<UserRole> userRoles = new HashSet<>();
        userRoles.add(new UserRole(user, role));
        userService.createUser(user, userRoles);

        String token = UUID.randomUUID().toString();
        userService.createPasswordResetTokenForUser(user, token);
        String appUrl = "http://"+ request.getServerName() + ":" + request.getServerPort() + request.getContextPath();

        SimpleMailMessage email = mailConstructor.constructResetTokenEmail(appUrl, request.getLocale(), token, user,
                password);

        mailSender.send(email);
        model.addAttribute("emailSent", true);
        
        return "myAccount";

这里我有邮件构造函数class。

@Component
public class MailConstructor {
    @Autowired

    private Environment env;

    public SimpleMailMessage constructResetTokenEmail(
            String contextPath, Locale locale, String token, User user,
            String password) {
        String url = contextPath + "/newUser?token=" + token;
        String message = "\nPlease click on this link to verify your email and edit your personal information. Your password is:\n"
                + password;
        SimpleMailMessage email = new SimpleMailMessage();
        email.setTo(user.getEmail());
        email.setSubject("Shop-Ufes - New User");
        email.setText(url + message);
        email.setFrom(env.getProperty("support.email"));
        return email;

    }
}

我在控制台中得到的错误是:

2020-10-03 16:57:22.256 ERROR 6952 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Unknown SMTP host: smtp.hmail.com;
  nested exception is:
    java.net.UnknownHostException: smtp.hmail.com. Failed messages: javax.mail.MessagingException: Unknown SMTP host: smtp.hmail.com;
  nested exception is:
    java.net.UnknownHostException: smtp.hmail.com; message exceptions (1) are:
Failed message 1: javax.mail.MessagingException: Unknown SMTP host: smtp.hmail.com;
  nested exception is:
    java.net.UnknownHostException: smtp.hmail.com] with root cause

java.net.UnknownHostException: smtp.hmail.com

除此之外您的设置似乎有效

spring.mail.host=smtp.hmail.com

主机名似乎有错别字。我觉得应该是

spring.mail.host=smtp.gmail.com