Skip to content Skip to sidebar Skip to footer

Thymeleaf-unable To Add A Logo To Email

I'm currently working on an email template with Thymeleaf and JHipster. I'm trying to add a logo image. I tried many methods, but I'm confused, where to save images and how to add

Solution 1:

The source of your image should point to an absolute URL with hostname that can be reached by mail reader.

In JHipster, this URL can be configured in application*.yml in jhipster.mail.base-url property. You can see it being used in src/main/resources/templates/mail/activationEmail.html for registration link.

In your case, your template should refer to your image like this: th:src="@{|${baseUrl}/logo.png|}".

Solution 2:

Images are static resources. Thymeleaf (Spring Boot) loads static resources from the class-path and from org.webjars. Static resources should be located in src/main/resources/static folder.

Example structure:

  myProject
  - src
    -main
      - java
      - resources
        - static
          - css
            - style.css
          - img
            - logo.png
          - js
        - templates
          - home.html

Typically in a spring boot application, you can ignore static resource location configurations, because it’s done automatically. But you can customize static resources loading, using the following configuration.

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration@EnableWebMvcpublicclassWebConfigextendsWebMvcConfigurerAdapter{

    @Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/webjars/**", "/img/**", "/css/**", "/js/**").addResourceLocations("classpath:/META-INF/resources/webjars/", "classpath:/static/img/", "classpath:/static/css/", "classpath:/static/js/");
    }
}

Post a Comment for "Thymeleaf-unable To Add A Logo To Email"