This is a simple class for sending email from within Velocity.
Essentially, the body of the email is processed with a
Velocity Context object.
The beauty of this is that you can send email from within your
Velocity template or from your business logic in your Java code.
The body of the email is just a Velocity template so you can use
all the template functionality of Velocity within your emails!
Example Usage (This all needs to be on one line in your
template):
Setup your context:
context.put ("VelocityEmail", new VelocityEmail() );
Then, in your template:
$VelocityEmail.setTo("Jon Stevens", "jon@latchkey.com")
.setFrom("Mom", "mom@mom.com").setSubject("Eat dinner")
.setTemplate("email/momEmail.vm")
.setContext($context)
The email/momEmail.wm template will then be parsed with the
Context that was defined with setContext().
If you want to use this class from within your Java code all you
have to do is something like this:
VelocityEmail ve = new VelocityEmail();
ve.setTo("Jon Stevens", "jon@latchkey.com");
ve.setFrom("Mom", "mom@mom.com").setSubject("Eat dinner");
ve.setContext(context);
ve.setTemplate("email/momEmail.vm")
ve.send();
(Note that when used within a Velocity template, the send method
will be called for you when Velocity tries to convert the
VelocityEmail to a string by calling toString()).
If you need your email to be word-wrapped, you can add the
following call to those above:
ve.setWordWrap (60);
This class is just a wrapper around the SimpleEmail class from
commons-mail using the JavaMail API.
Thus, it depends on having the
mail.server property set in the TurbineResources.properties file.
If you want to use this class outside of Turbine for general
processing that is also possible by making sure to set the path to
the TurbineResources.properties. |