Anyway, in email templates, it's rarely necessary to write loops, methods, or branch conditions; instead, more often, the need is to replace single variables with values provided programmatically. In this article, I am going to show how I implemented a wrapper for NVelocity functionalities, which simplifies merging templates coming from a file system, assembly resources, or in-memory objects, and will build a simple web project for demonstration. The source code zip file contains the wrapper project, while the sample project zip file contains a web project which shows how to use it to merge templates in ASP.
As a note, keep in mind that the wrapper can be used in any other project type, from console applications to Windows Forms and WebServices as well. I have chosen to build the demo in ASP. NET because it is a good compromise between a nice user interface and ease of creation. The main access point is the class NVelocityEngineFactory , which exposes three static methods. Each of these methods return an object that implements the INVelocityEngine interface, which can respectively be used to retrieve templates from embedded resources, file system, or memory.
The boolean parameter required by each of the methods is used to specify whether the result of the merge has to be cached or not. All of them inherit from the NVelocityEngineBase base class, which defines a common constructor and a static protected method. Since implementing the INVelocityEngine interface, the three engine classes expose a public method with an overload called Process , which take two or three parameters and return a string or void.
In the first case, the two input parameters are the IDictionary object containing the values to be merged in the template and the template itself, while the return value is the merged template put in a string object.
In the second case, the additional parameter is a TextWriter object which, after the processing, will contain the merged template. To use the three types of engines, we need to create a template to be merged. Using VTL syntax, we write a simple template which can be used to render a message containing a simple personalized greeting, along with the date when the merging was done. To test all of the three engines, the demo project contains a template file placed on the file system, a template file embedded in the assembly as a resource, and an in-memory template a string created at runtime.
To write a template file, we just need to open a text editor and save the file with the. This is a very simple template, but it's not difficult to imagine in its place an entire HTML email with nice formatting and some images. Supposing we have placed the template file in a subdirectory called Templates in the root folder of our web project and named it SimpleTemplate. The LabelMergedFile Text attribute will contain the text of the template and, in place of the variables, the values supplied in the code.
Supposing we have placed the template file in a subdirectory called EmbeddedResources in the root folder of our web project, named it SimpleTemplate. Note that when calling the Process method, you need to specify the path of the resource, like in file system paths, but with dots in place of slashes.
Differently from the previous cases, now the template must reside in memory, so we create a string object that contains the template:. Note that the keys of the objects you place in the IDictionary object must correspond to the variable's names specified in the template if you want NVelocity to merge them. I wrote this brief tutorial because I myself have been looking for something like this while I was implementing a newsletter service and after getting crazy at trying to merge the templates by hand.
The sad thing about NVelocity is that the project is almost dead; the latest release, 0. The positive thing is that CastleProject developers have made a fork of the project, fixed some bugs, and killed a tedious assembly dependence on an old Log4Net distribution, which NVelocity used as the internal logger engine. Actually, this NVelocity fork has reached version 0. Sign in Email. Forgot your password? Search within: Articles Quick Answers Messages.
Tagged as. Stats Simone Busoli Rate me:. Please Sign up or sign in to vote. Download source files - KB Download demo project - KB Introduction If you have ever tried to implement a newsletter system or a service for communicating with website users, you have probably faced the requisite to send multiple email messages with a common template and some differences, like a personalized greeting in the message header, or an unsubscribe link with a personalized querystring.
NVelocity will do the rest. Background In order to write NVelocity templates, a basic knowledge of the language it uses would be helpful. Using the code The main access point is the class NVelocityEngineFactory , which exposes three static methods. The template To use the three types of engines, we need to create a template to be merged. Copy Code. Simone Busoli Software Developer. Pawel Krakowiak Nov Simone Busoli Nov Configuration objects are managed as Spring beans within the container, imported configurations are used to injected using Autowired or Inject.
Define Gmail SMTP configurations like host, port number, username and password, also configure the mail properties. This is simple pojo class which contains different email attributes like mailFrom, mailTo, mailCc, mailBcc, mailSubject, mailContent, contentType, attachments and model. Create email message instance using new Email. Set respected mail values along with model map placeholders attributes, Which is used by velocity template.
To create standalone Spring Application Context, We are using AnnotationConfigApplicationContext which helps to register all the beans generated by the configuration class Configuration at Spring runtime. Use AbstractApplicationContext. Compile and run the App.
Here is the email Which I have received!! Hi there! I am founder of technicalkeeda. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail. Name" variable will be replaced with your store name in an HTML email template in this example. The best practice for creating new email templates is to copy similar default email templates and make your customizations as needed. This will ensure that you're calling the right variables.
This example uses an " if" statement that requires an " end" directive to stop the statement. Otherwise, the customer gets a generic "Thank you" message. The " if" statement is one of many conditional statements you can use in an email template. You can also use the " eslseif" and " else" statements with " if" statements. The next most common statement is the " foreach" loop.
In this tutorial we are going to discuss, how to send email using Spring Framework with the help of Velocity base Email template. Velocity is used to define Email body template with dynamic or place holder parameters. This Spring Velocity Email Template Example tutorial is useful for beginners and experience developers.
It will helps you to learn step by step with the help of below code. Create spring application bean definition file spring-beans. Define respected bean definations like mailSender, mailer and velocityEngine.
0コメント