java 邮件模版_模板发送java邮件

Creating email content using a templating library

The code in the previous examples explicitly created the content of the email message, using methods calls such as message.setText(..). This is fine for simple cases, and it is okay in the context of the aforementioned examples, where the intent was to show you the very basics of the API.

In your typical enterprise application though, you are not going to create the content of your emails using the above approach for a number of reasons.

Creating HTML-based email content in Java code is tedious and error prone

There is no clear separation between display logic and business logic

Changing the display structure of the email content requires writing Java code, recompiling, redeploying…​

Typically the approach taken to address these issues is to use a template library such as FreeMarker or Velocity to define the display structure of email content. This leaves your code tasked only with creating the data that is to be rendered in the email template and sending the email. It is definitely a best practice for when the content of your emails becomes even moderately complex, and with the Spring Framework’s support classes for FreeMarker and Velocity becomes quite easy to do. Find below an example of using the Velocity template library to create email content.

A Velocity-based example

To use Velocity to create your email template(s), you will need to have the Velocity libraries available on your classpath. You will also need to create one or more Velocity templates for the email content that your application needs. Find below the Velocity template that this example will be using. As you can see it is HTML-based, and since it is plain text it can be created using your favorite HTML or text editor.

# in the com/foo/package

Hi ${user.userName}, welcome to the Chipping Sodbury On-the-Hill message boards!

Your email address is ${user.emailAddress}.

Find below some simple code and Spring XML configuration that makes use of the above Velocity template to create email content and send email(s).

package com.foo;

import org.apache.velocity.app.VelocityEngine;

import org.springframework.mail.javamail.JavaMailSender;

import org.springframework.mail.javamail.MimeMessageHelper;

import org.springframework.mail.javamail.MimeMessagePreparator;

import org.springframework.ui.velocity.VelocityEngineUtils;

import javax.mail.internet.MimeMessage;

import java.util.HashMap;

import java.util.Map;

public class SimpleRegistrationService implements RegistrationService {

private JavaMailSender mailSender;

private VelocityEngine velocityEngine;

public void setMailSender(JavaMailSender mailSender) {

this.mailSender = mailSender;

}

public void setVelocityEngine(VelocityEngine velocityEngine) {

this.velocityEngine = velocityEngine;

}

public void register(User user) {

// Do the registration logic...

sendConfirmationEmail(user);

}

private void sendConfirmationEmail(final User user) {

MimeMessagePreparator preparator = new MimeMessagePreparator() {

public void prepare(MimeMessage mimeMessage) throws Exception {

MimeMessageHelper message = new MimeMessageHelper(mimeMessage);

message.setTo(user.getEmailAddress());

message.setFrom("webmaster@csonth.gov.uk"); // could be parameterized...

Map model = new HashMap();

model.put("user", user);

String text = VelocityEngineUtils.mergeTemplateIntoString(

velocityEngine, "com/dns/registration-confirmation.vm", model);

message.setText(text, true);

}

};

this.mailSender.send(preparator);

}

}

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

resource.loader=class

class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

附录:

org.apache.velocity

velocity

1.7

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值