Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我在尝试使用javax邮件依赖项时出错_Java_Email_Smtp_Jakarta Mail - Fatal编程技术网

我在尝试使用javax邮件依赖项时出错

我在尝试使用javax邮件依赖项时出错,java,email,smtp,jakarta-mail,Java,Email,Smtp,Jakarta Mail,我试图在我的项目中使用javaxmail。我在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.prope

我试图在我的项目中使用javaxmail。我在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端口需要解锁,找到了如何解锁的快速指南,你可以在照片中看到

在控制器类中,我在用户引入其注册凭据(用户名、电子邮件地址)后设置,以生成一封带有编辑配置文件页面链接的电子邮件

当我输入用户名和邮件并单击提交时,会出现错误“未知SMTP主机: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";
控制台中出现的错误是:

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

谢谢,但现在我有了“失败的邮件:javax.mail.MessaginException:无法连接到SMTP主机:SMTP.gmail.com,端口:25”;并且我打开了所有端口465 587 25。然后在“spring.mail.properties.mail.smtp.socketFactory.port=25”@sabinufes中尝试所有这些方法。如果要在另一台服务器上的该端口上连接,则无需打开任何端口。从您的错误消息来看,Google似乎只接受加密连接。所以我需要使用不同的服务?您需要调查您决定使用的特定邮件主机上允许使用哪个端口。我的意思是,它与您的应用程序配置完全无关。它在主机端,因此,您需要对此进行调查。@sabinufes,如果回答了您的问题,请将我的回答标记为已接受:)
spring.mail.host=smtp.gmail.com