Using XML to create email templates in Java

Source: Internet
Author: User
Tags mail wrapper knowledge base

Sending mail is a basic function of Web application system. Generally speaking, mail has specific types, such as password reminders, welcome information, order confirmation or receipt confirmation. Although the content of different application messages varies, the process of sending messages is essentially the same. Build the message, send it to the mail server, send it.

When using Java development, we often use the JavaMail API to connect mail servers to send mail. But this approach is cumbersome (mainly due to the flexibility of the message), so when you need to send a message multiple times in this way, It's better to write a wrapper. Depending on how it is used, wrapper can send a particular message, such as a password reminder, or as a generic pattern, accept the subject, recipient, and message content as parameters.

Once you use wrapper to send a message, you need a system that constructs messages on your own. Let's use a password reminder as an example. Basically all messages contain topics, content, and recipients. When we send a password reminder email, the user address and password are extracted from the knowledge base of a log login information. The subject and content need to be merged with the data extracted by the database and stored somewhere. The biggest problem with system design is where to store this type of string. In many cases, strings are stored in the properties file, which separates the data from the source code and makes localization easier. I have used this storage mechanism in many Web application systems, but unfortunately there are many flaws in this approach.

The following are the reasons why you cannot store a message string using a property file:

• The property file uses a very simple data structure-name and value combination. This structure is not appropriate when you need a lot of values to correspond to a name. For example, a message has 4 recipients, 3 cc people, using a property file is difficult to solve this problem.

• The format of the property file is very strict. The name and value must be on the same line, so long strings are difficult to handle when you edit the file. For example, it is painful to put all the content of a message into a property file. If you want the content of the value to include a newline, you must use the

Another option is to use XML as a mail template, which is what this article is all about. XML provides a great deal of flexibility for you to build templates, and it does not have all the formatting limitations of a property file, so it's easy to handle long strings in this way. The main disadvantage of XML is that it is more complex to handle than a property file. When using a property file, it is easy to load files and access files after loading. Loading XML files and using one of the multiple XML processing libraries provided by Java to process XML files requires more work.

This article provides a generic template that enables you to create templates using XML files and send emails, hoping to mitigate the pain of this process. In this template, I will use the Commons Digester package in the Jakarta project to process the XML and send the mail using the JavaMail API.

Mail templates

Let's take a look at the format of the mail template. A template is an XML file that contains a root element and a set of child elements of a series of roots. The root element is. The necessary child elements are,,, and. The optional child elements are,,, and. If you've ever used a mail system, you can deduce what these elements actually contain. There are multiple instances of an optional element, so you can specify multiple addresses for each type of receiver. I will explain the operation mechanism when describing the message processing later. The following is an example of a template file.

rafe@rafe.us
someone@example.com
someoneelse@example.com
rafe@rafe.us
This is the subject
This is the body of an email message.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.