添加新Service

新建一个类EmailService, package为net.wendal.nutzbook.service ,内容如下

  1. package net.wendal.nutzbook.service;
  2. public interface EmailService {
  3. boolean send(String to, String subject, String html);
  4. }

再新建一个实现类 EmailServiceImpl

  1. package net.wendal.nutzbook.service;
  2. import org.apache.commons.mail.HtmlEmail;
  3. import org.nutz.ioc.Ioc;
  4. import org.nutz.ioc.loader.annotation.Inject;
  5. import org.nutz.ioc.loader.annotation.IocBean;
  6. import org.nutz.log.Log;
  7. import org.nutz.log.Logs;
  8. @IocBean(name="emailService")
  9. public class EmailServiceImpl implements EmailService {
  10. private static final Log log = Logs.get();
  11. @Inject("refer:$ioc")
  12. protected Ioc ioc;
  13. public boolean send(String to, String subject, String html) {
  14. try {
  15. HtmlEmail email = ioc.get(HtmlEmail.class);
  16. email.setSubject(subject);
  17. email.setHtmlMsg(html);
  18. email.addTo(to);
  19. email.buildMimeMessage();
  20. email.sendMimeMessage();
  21. return true;
  22. } catch (Throwable e) {
  23. log.info("send email fail", e);
  24. return false;
  25. }
  26. }
  27. }

内容很简单,就是取出HtmlEmail然后发送出去.

如果是QQ邮箱,请开通smtp服务!!!

http://service.exmail.qq.com/cgi-bin/help?subtype=1&&id=26&&no=308

否则永远发不出去

如果还是发不了,请换163或阿里云邮!!