读书人

spring mail 范例

发布时间: 2012-08-28 12:37:01 作者: rapoo

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("邮件发送成功!");

  }

读书人网 >网络基础

热点推荐