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("[email protected]"); //接收人

  mail.setFrom("[email protected]"); //发送人

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

  }

你可能感兴趣的:(spring,xml,Gmail)