首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

spring mail 范例

2012-08-28 
spring mail 实例??啥也不说了,见附件:?发送ftl模板实例:private Configuration cfg  new Configuration(

spring mail 实例

?

?啥也不说了,见附件:

?发送ftl模板实例:

private Configuration cfg = new Configuration();

  public static void main(String[] args) throws Exception {

  ApplicationContext ctx = new FileSystemXmlApplicationContext(

  "src/applicationContext.xml");

  JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");

  SpringMail springMail = new SpringMail();

  springMail.sendMail(sender);

  }

  private void sendMail(JavaMailSender sender) throws Exception {

  SimpleMailMessage mail = new SimpleMailMessage();

  mail.setTo("sucre.xu@gmail.com"); //接收人

  mail.setFrom("dufangxu@sina.com"); //发送人

  mail.setSubject("test by amigo");

  //嵌入ftl模版

  cfg.setClassForTemplateLoading(getClass(), "/mail");

  Map root = new HashMap();

  root.put("username", "sucre"); //模板变量

  Template t = cfg.getTemplate("notify-mail.ftl");

  StringWriter writer = new StringWriter();

  t.process(root, writer);

  //把模版内容写入邮件中

  mail.setText(writer.toString());

  sender.send(mail);

  System.out.println("邮件发送成功!");

  }

热点排行