Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

in Java HV000030: No validator could be found for constraint 'javax.validation.constraints.Email'

Ask Question

I am trying to send html email notification when user submits a form but I am getting an error. Can you please look at it?

There was an unexpected error (type=Internal Server Error, status=500).HV000030: No validator could be found for constraint 'javax.validation.constraints.Email' validating type 'java.lang.String'. Check configuration for 'email'

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Email' validating type 'java.lang.String'. Check configuration for 'email'

Models:

import javax.validation.constraints.Email;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
  public class MailRequest {
  private String name;
  @Email
  private String to;
  @Email
  private String from;
  private String subject;
  public MailRequest(String to) {
      this.name = "Some name";
      this.to = to;
      this.from = "example@gmail.com";
      this.subject = "Confirmation";
   import org.springframework.context.annotation.Configuration;
  import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean;
  @Configuration
  public class EmailConfig {
    public FreeMarkerConfigurationFactoryBean factoryBean() {
       FreeMarkerConfigurationFactoryBean bean = new FreeMarkerConfigurationFactoryBean();
bean.setTemplateLoaderPath("classpath:/templates");
      return bean;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.validation.UnexpectedTypeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import com.hostmanagement.email.model.MailRequest;
import com.hostmanagement.email.model.MailResponse;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
@Service
public class EmailService {
@Autowired
private JavaMailSender sender;
@Autowired
private Configuration config;
 public MailResponse sendEmail(MailRequest request, Map<String, Object> model) {
    MailResponse response = new MailResponse();
    MimeMessage message = sender.createMimeMessage();
    try {
        // set mediaType
        MimeMessageHelper helper = new MimeMessageHelper(message, MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,
                StandardCharsets.UTF_8.name());
        Template t = config.getTemplate("email-template.ftl");
        String html = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
        helper.setTo(request.getTo());
        helper.setText(html, true);
        helper.setSubject(request.getSubject());
        helper.setFrom(request.getFrom());
        sender.send(message);
        response.setMessage("mail send to : " + request.getTo());
        response.setStatus(Boolean.TRUE);
    } catch (UnexpectedTypeException | MessagingException | IOException | TemplateException e) {
        response.setMessage("Mail Sending failure : "+e.getMessage());
        response.setStatus(Boolean.FALSE);
    return response;

This is in my main Service and I call this method from Controller.

public MailResponse sendConfirmationEmail(String email) throws UnexpectedTypeException {
    Map<String, Object> modelsMap = new HashMap<>
    modelsMap.put("name", "Tural");
    modelsMap.put("Location", "Baku");
    MailRequest mailRequest = new MailRequest(email);
    return emailservice.sendEmail(mailRequest, modelsMap);

From my Controller I am passing email address to my method:

 @Email @RequestParam("email") String email,
 DAOServiceImpl.sendConfirmationEmail(email);

I did some research and upgraded hibernate-validator in pom.xml as well but doesn't work

 <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-validator</artifactId> 
  <version>6.0.11.Final</version>
</dependency>

Thank you for your help!!

Could you check if adding a message the error continues? Something like this @Email(message = "Email should be valid") – Andres Sacco Dec 30, 2020 at 23:26

The solution for this problem is to add a message in the annotation @Email.

@Email(message = "Email should be valid") 

Depends on the implementation that you use this attribute is required or not.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.