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

huangapple go评论76阅读模式
英文:

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

允许端口:
我在尝试使用javax邮件依赖时遇到了错误。

我在网上找到了端口465需要解锁的信息,找到了一个快速指南,可以在照片中看到。

在控制器类中,我已经设置了在用户输入注册凭据(用户名、电子邮件地址)后生成一封电子邮件,其中包含指向编辑个人资料页面的链接。

当我输入用户名和电子邮件并点击提交时,我收到以下错误:
>"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 {
    // ...
}

这里是邮件构造器类。

@Component
public class MailConstructor {
    @Autowired
    private Environment env;

    public SimpleMailMessage constructResetTokenEmail(
            String contextPath, Locale locale, String token, User user,
            String password) {
        // ...
    }
}

在控制台中出现的错误如下:

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
英文:

I'm trying to use javax mail in my project. I have enabled less secure apps in Gmail and made an inbound rule to unlock the port 465.

This the configuration which I use in application properties.

''' 
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
'''

Allowing the port:
我在尝试使用javax邮件依赖时遇到了错误。

I found on web that the port 465 needs to be unlocked, found a quick guide how to do that, you can see in the photo.

In the controller class I have set after the user is introducing his register credentials ( username , email address) to generate an email with a link to the edit profile page.

When I enter the username and mail and click submit I get the error
>"Unknown SMTP host: smtp.hmail.com;",

but the data is sent to the DB. I get error 500 on the web page.

@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";

Here I have the mail constructor 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;

	}
}

The error which I get in console is:

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

答案1

得分: 1

你的设置看起来是有效的,除了这一部分:

spring.mail.host=smtp.hmail.com

似乎与主机名有关的地方有一个拼写错误。我相信应该是这样的:

spring.mail.host=smtp.gmail.com
英文:

Your setup seems to be valid except of this

spring.mail.host=smtp.hmail.com

Seems like there is a typo related to the host name. I believe it should be

spring.mail.host=smtp.gmail.com

huangapple
  • 本文由 发表于 2020年10月3日 22:19:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64185209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定