问题
I am trying to gain access for images on an email template. I've tried following this guide. Spring: refering the resources/static folder and this Spring 4 - addResourceHandlers not resolving the static resources
I am storing the templates in this location
my-project\complete\src\main\resources\templates
http://localhost:8080/static/templates/forgotemail/images/bottompodmiddleb.jpg
^ I want to gain access to this image -- I have a reactjs build and I am unsure if the reactjs routing is interfering with this.
I have access to this image for example.
http://localhost:8080/static/media/-bach.4f9c4b25.jpg
I create a configuration class -but I am unsure if I need to invoke it or what.
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
//@Configuration
@ComponentScan("Application")
public class Configuration extends WebMvcConfigurerAdapter {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// I tried these many combinations separately.
ResourceHandlerRegistration resourceRegistration = registry
.addResourceHandler("resources/**");
resourceRegistration.addResourceLocations("/resources/**");
registry.addResourceHandler("/templates/**").addResourceLocations("/templates/**");
//registry.addResourceHandler("/img/**").addResourceLocations("/img/**");
//registry.addResourceHandler("/js/**").addResourceLocations("/js/**");
registry.addResourceHandler("/resources/**")
.addResourceLocations("classpath:/resources/");
// do the classpath works with the directory under webapp?
}
}
here is the code that launches the email. "fmConfiguration.getTemplate("email-template.html")" appears to get access to the html email.
public void sendEmail(JSONObject model, JavaMailSender mailSender, Configuration fmConfiguration) throws Exception{
System.out.println("type>>>"+model);
System.out.println("mailSender"+ mailSender);
if(mailSender != null){
try {
MimeMessage mimeMessage = mailSender.createMimeMessage();
System.out.println("mimeMessage >>>"+ mimeMessage);
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setSubject("Hi");
mimeMessageHelper.setFrom("test2@gmail.com");
mimeMessageHelper.setTo("test@gmail.com");
//Map < String, Object > model = new HashMap < String, Object > ();
model.put("firstName", "Yashwant");
model.put("lastName", "Chavan");
model.put("imgPath", "resources/static/images/");
mimeMessageHelper.setText(geContentFromTemplate(fmConfiguration, model), true);
mailSender.send(mimeMessageHelper.getMimeMessage());
} catch (MessagingException e) {
System.out.println("ERROR - mimeMessage>>>");
e.printStackTrace();
}
}
}
public String geContentFromTemplate(Configuration fmConfiguration, Map < String, Object > model) {
StringBuffer content = new StringBuffer();
try {
content.append(FreeMarkerTemplateUtils
.processTemplateIntoString(fmConfiguration.getTemplate("email-template.html"), model));
} catch (Exception e) {
e.printStackTrace();
}
return content.toString();
}
来源:https://stackoverflow.com/questions/46128893/freemarker-templates-unable-to-gain-access-to-images-java-spring-boot