tapestry mail freemarker的使用

本文介绍了一种使用Java进行邮件发送的方法,并演示了如何利用FreeMarker模板引擎渲染邮件正文。通过注入的方式引入了必要的服务类,实现了参数化的邮件内容生成及发送功能。

1)使用方式

@Inject
@Path("context:/templates/report.ftl")
private Asset template;
       
@Inject
private MailService<Email> mailService;
       
@Inject @FreeMarker
private TemplateService _templateService;

public String getPassengerList() {
    OutputStream os = new ByteArrayOutputStream();
    Map<String, String> parameterMap = new HashMap<String, String>();
    parameterMap.put("name", "xxxx");
    parameterMap.put("content", "hello world is the first program you write!");
    _templateService.mergeDataWithResource(template.getResource(), os, parameterMap);
    return os.toString();
}
    
void onSendMail() {
   MailMessageHeaders headers = new MailMessageHeaders();
   headers.addTo("to@mail.cn");
   headers.addBcc("Bcc@mail.cn");
   headers.addCc("cc@mail.cn");
   headers.setSubject("just for test only");
   mailService.sendPlainTextMail(headers, getPassengerList());
}


1)在context.xml中配置mail session

<Resource name="mail/session"
              auth="Container"
              type="javax.mail.Session"
              factory="org.apache.naming.factory.MailSessionFactory"
              mail.transport.protocol="smtp"
              mail.smtp.host="smtp.xxxxx.xx"
              mail.smtp.port="25"
              mail.smtp.auth="true"
              mail.smtp.user="xxxxxx@xxxxxx.xx"
              password="yyyyy"
              mail.from="xxxxxx@xxxxxx.xx"
              mail.debug="false"
              />


转载于:https://my.oschina.net/u/1453451/blog/278452

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值