Reputation: 1534
I'm interested which is the best way to send Emails with images in them from a Spring MVC application which uses Velocity.
The image should be in the application, possibly in the same location as the *.vm
template (e.g. src/main/resources/templates
) and no absolute paths should be used (e.g. C:\App...), that's why I can't figure it out how to do it.
Suggestions?
Upvotes: 3
Views: 7361
Reputation: 60205
With Spring you can easily load resources from file system or from classpath (even within jar file) without absolute paths. You should use a Resource
and its implementations, basically FileSystemResource
or ClasspathResource
.
This is an example:
Resource fileResource = new FileSystemResource("resources/templates");
After loading you can directly retrieve inputstream from resource instance.
Also, you can inject your relative path to a bean which has an instance attribute of type Resource:
<bean id="mailer" class="test.Mailer">
<property name="templateResource" value="file:resource/templates" />
</bean>
I don't use Velocity but I usually load templates in this way to generate dynamic content with freemarker. You can also attach an image to an email, once you've loaded it.
Upvotes: 2
Reputation: 4385
You need to understand how images and emails work together. I'm not an expert here, but I think the options are
Have a look at org.springframework.mail.javamail.MimeMessageHelper
Upvotes: 4