Martin Spa
Martin Spa

Reputation: 1534

Sending Velocity templated Emails with images in them from a Spring MVC application

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

Answers (2)

javanna
javanna

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

three-cups
three-cups

Reputation: 4385

You need to understand how images and emails work together. I'm not an expert here, but I think the options are

  • include the image as an attachment in the email (lame, IMHO; and your bandwidth usage will be very high)
  • send an HTML email and link to an image hosted on the Internet

Have a look at org.springframework.mail.javamail.MimeMessageHelper

Upvotes: 4

Related Questions