- 浏览: 554909 次
- 性别:
- 来自: 长沙
文章分类
- 全部博客 (145)
- apache-struts (3)
- apache-shiro (4)
- apache-wicket (1)
- spring (34)
- spring-data-jpa (2)
- spring-mvc (20)
- spring-security (1)
- spring-webflow (1)
- hibernate (2)
- mongodb (1)
- ibatis (5)
- mysql (4)
- 开源组件 (18)
- java (3)
- maven (7)
- jBPM (1)
- EJB (1)
- JMS (2)
- servlet / jsp (9)
- javascript / jquery (10)
- 工作技巧 (12)
- ubuntu (6)
- bootstrap (10)
- javaee (1)
- 他山石 (7)
- jetbrick (1)
最新评论
-
yubao2008:
[size=x-small]为什么我也这样试了, 就是没有生效 ...
javax.servlet.http.HttpServletResponse 得到 status_code -
chenrl:
...
SpringMVC杂记(十五) spring-mvc controller 的切面 -
LONGTENGLONG:
你好,这样配置的,得到的集合为空,是什么原因?
apache-shiro杂记(一) 统计在线用户数目 -
xiafengfeiwu:
[flash=200,200][url]引用[/url][/f ...
apache-shiro 学习笔记 -
3108493554:
你好 ,有些问题想请教下,加下我qq310849354,你这上 ...
SpringMVC杂记(十二) 自定义Interceptor从Active Directory得到域信息
一) 现在项目中用的javamail和org.springframework.mail.javamail.JavaMailSender来发送电子邮件的,而邮件正文的模板是在spring的配置文件中完成的。
修改起来比较麻烦,并缺乏一些逻辑控制。我决定改造一下,把这些模板性质的东西都用freemarker管理起来。于是就有了这篇文章。
二) 代码和配置
修改起来比较麻烦,并缺乏一些逻辑控制。我决定改造一下,把这些模板性质的东西都用freemarker管理起来。于是就有了这篇文章。
二) 代码和配置
package mail; import java.io.IOException; import java.util.Collection; import javax.mail.internet.MimeMessage; import org.springframework.beans.factory.InitializingBean; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.mail.javamail.MimeMessagePreparator; import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; import org.springframework.util.Assert; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; public class FreeMarkerConfiguredMailSender implements InitializingBean { private Configuration freeMarkerConfiguration; private JavaMailSender javaMailSender; private String encoding = "UTF-8"; // constructor // ----------------------------------------------------------------------- public FreeMarkerConfiguredMailSender() { super(); } public FreeMarkerConfiguredMailSender( Configuration freeMarkerConfiguration, JavaMailSender javaMailSender) { this(); this.freeMarkerConfiguration = freeMarkerConfiguration; this.javaMailSender = javaMailSender; } // send html email, exception thrown maybe // ----------------------------------------------------------------------- public void sendMail(final String[] to, final String from, final String[] cc, final String[] bcc, final String subject, String templateName, Object model) throws IOException, TemplateException { Template template = freeMarkerConfiguration.getTemplate(templateName); final String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model); javaMailSender.send(new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, encoding); message.setTo(to); message.setFrom(from); message.setCc(cc != null ? cc : new String[0]); message.setBcc(bcc != null ? bcc : new String[0]); message.setSubject(subject != null ? subject : ""); message.setText(text, true); } }); } public void sendMail(final String to, final String from, final String[] cc, final String[] bcc, final String subject, String templateName, Object model) throws IOException, TemplateException { sendMail(new String[]{to}, from, cc, bcc, subject, templateName, model); } public void sendMail(final Collection<String> to, final String from, final Collection<String> cc, final Collection<String> bcc, final String subject, String templateName, Object model) throws IOException, TemplateException { sendMail(_toArray(to), from, _toArray(cc), _toArray(bcc), subject, templateName, model); } public void sendMail(final String to, final String from, final Collection<String> cc, final Collection<String> bcc, final String subject, String templateName, Object model) throws IOException, TemplateException { sendMail(to, from, _toArray(cc), _toArray(bcc), subject, templateName, model); } // send html email quietly // ----------------------------------------------------------------------- public boolean sendMailQuietly(final String[] to, final String from, final String[] cc, final String[] bcc, final String subject, String templateName, Object model) { try { sendMail(to, from, cc, bcc, subject, templateName, model); } catch (IOException e) { return false; } catch (TemplateException e) { return false; } return true; } public boolean sendMailQuietly(final String to, final String from, final String[] cc, final String[] bcc, final String subject, String templateName, Object model) { try { sendMail(new String[]{to}, from, cc, bcc, subject, templateName, model); } catch (IOException e) { return false; } catch (TemplateException e) { return false; } return true; } public boolean sendMailQuietly(final Collection<String> to, final String from, final Collection<String> cc, final Collection<String> bcc, final String subject, String templateName, Object model) { try { sendMail(_toArray(to), from, _toArray(cc), _toArray(bcc), subject, templateName, model); } catch (IOException e) { return false; } catch (TemplateException e) { return false; } return true; } public boolean sendMailQuietly(final String to, final String from, final Collection<String> cc, final Collection<String> bcc, final String subject, String templateName, Object model) { try { sendMail(to, from, _toArray(cc), _toArray(bcc), subject, templateName, model); } catch (IOException e) { return false; } catch (TemplateException e) { return false; } return true; } // inner util method // ------------------------------------------------------------------------------------------------------ private String[] _toArray(Collection<String> collection) { if (collection == null || collection.isEmpty()) return new String[0]; return collection.toArray(new String[0]); } // // ------------------------------------------------------------------------------------------------------ public void afterPropertiesSet() throws Exception { Assert.notNull(encoding); Assert.notNull(freeMarkerConfiguration); Assert.notNull(javaMailSender); freeMarkerConfiguration.setDefaultEncoding(encoding); } // setter and getter // ------------------------------------------------------------------------------------------------------ public JavaMailSender getJavaMailSender() { return javaMailSender; } public void setJavaMailSender(JavaMailSender javaMailSender) { this.javaMailSender = javaMailSender; } public Configuration getFreeMarkerConfiguration() { return freeMarkerConfiguration; } public void setFreeMarkerConfiguration(Configuration freeMarkerConfiguration) { this.freeMarkerConfiguration = freeMarkerConfiguration; } public String getEncoding() { return encoding; } public void setEncoding(String encoding) { this.encoding = encoding; } }
<bean class="mail.FreeMarkerConfiguredMailSender"> <property name="freeMarkerConfiguration"> <bean class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean"> <property name="templateLoaderPath" value="classpath:email-templates" /> <property name="freemarkerSettings"> <props> <prop key="tag_syntax">auto_detect</prop> <prop key="template_update_delay">0</prop> <!-- <prop key="default_encoding">UTF-8</prop> <prop key="output_encoding">UTF-8</prop> --> <prop key="locale">zh_CN</prop> <prop key="date_format">yyyy-MM-dd</prop> <prop key="time_format">HH:mm:ss</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> </props> </property> </bean> </property> <property name="javaMailSender"> <bean class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.xxx.com" /> <property name="port" value="25" /> <property name="username" value="foo@xxx.com" /> <property name="password" value="password" /> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> </props> </property> </bean> </property> </bean>
发表评论
-
SpringMVC杂记(十八) ServletRequestAttributes的应用
2014-02-28 12:38 14131看了一下SpringMVC的源代码,原来SpringMVC也提 ... -
SpringWebflow杂记(一) 框架初探,与SpringMVC的集成
2013-10-23 17:18 1658今日研究了一下SpringWebFlow这个项目,作为Spri ... -
SpringMVC杂记(十六) spring-mvc 与 openid4java
2013-10-12 15:25 3811SpringMVC杂记(十六) spring-mvc 与 op ... -
SpringMVC杂记(十五) spring-mvc controller 的切面
2013-08-01 19:42 6057SpringMVC杂记(十五) spring-mvc cont ... -
Spring集成CXF
2013-06-24 15:53 1522Spring集成CXF 零) jar依赖 <dep ... -
SpringMVC杂记(十四) Ajax方式的JSR303认证
2013-06-13 07:29 4477自己定义一个Exception,用来表示数据绑定失败 im ... -
SpringMVC杂记(十三) 使用FreeMarker作为视图层
2013-06-09 11:55 3344实在没什么好说的,直接上配置文件好了 <bean i ... -
SpringMVC杂记(十二) 自定义Interceptor从Active Directory得到域信息
2013-06-04 14:04 3144一)最近项目中要求实现Web应用的SSO(Single Sig ... -
SpringMVC杂记(十一) 使用Excel视图
2013-04-06 16:06 6574SpringMVC杂记(十一) 使用Excel视图 一) 其 ... -
Spring集成junit
2012-12-24 10:43 1451package junit; import org. ... -
SpringMVC杂记(十) 验证码生成
2012-11-06 10:18 2722以前写过一篇关于这个的博客,现在用SpringMVC了,重写一 ... -
SpringMVC杂记(九) 模拟其他类型(非GET,POST)的请求
2012-10-22 10:49 26281) 以前一个小兄弟问我,SpringMVC是否可以使用很多浏 ... -
SpringMVC杂记(八) 使用阿里巴巴的fastjson
2012-07-21 08:27 102251) 国产开源软件要支持的 <dependency& ... -
ActiveMQ学习笔记(二) JMS与Spring
2012-06-24 10:21 7341上文可见,JMS Native API使用起来不是特别方便。好 ... -
我的SpringSecurity实践
2012-04-08 07:49 8905我的SpringSecurity实践 (一) 数据库与实体类 ... -
SpringMVC杂记(七) Jackson与Hibernate LazyLoding无法正常工作解决办法
2012-03-21 13:35 6999SpringMVC杂记(七) Jackson与Hibernat ... -
SpringMVC杂记(六) 下载文件
2012-03-21 09:04 4210SpringMVC杂记(六) 下载文件 1) jar依赖 ... -
SpringMVC杂记(五) JSR303数据验证
2012-03-16 16:30 12378SpringMVC杂记(五) JSR303数据验证 1) 首 ... -
SpringMVC杂记(四) 数据绑定
2012-03-15 13:44 2205SpringMVC杂记(四) 数据绑定 1) 使用java. ... -
SpringMVC杂记(三) 向导型Controller的实现(基于SpringMVC 3.1.1)
2012-03-14 14:59 4600SpringMVC杂记(三) 向导型Controller的实现 ...
相关推荐
导入到Myeclipse 即可运行 测试使用的是QQ邮箱 在application.xml中配置的发送邮箱 要到邮箱中设置 POP3/SMTP服务为开。项目不部署到tomcat后启动服务,然后运行test.java 就发送了,。绝对可以发送
spring整合freemarker实现发送邮件, html中内嵌图片,添加附件, 解决乱码问题.
在spring利用javamail,quartz定时发送邮件
spring javamail 来发送动态生成的3D图象
采用javamail实现的通用邮件发送模块,邮件内容采用Freemarker技术实现,多线程实现邮件发送,观察者模式来定制邮件的错误处理,设计思路参考作者博客 http://blog.csdn.net/crazycoder2010/article/details/6629076
Spring Use JavaMail SendEmail(spring使用javamail发送邮件的例子)
Spring整合JavaMail发送qq邮件,极简单的例子。看完就会。。。
此源码是使用javamail发送邮件,可以实现静默发送邮件,可以发送附件资源等。有相应的使用说明,快捷好用
JSP与JavaMail发送HTML格式... java,javamail发送html邮件..
数据库:sqlserver CreateHtmlTable中的生成路径和SendService中的上传路径改成自己的
NULL 博文链接:https://lihongtai.iteye.com/blog/2178787
JavaMail实现邮件发送的简单封装(包括文本、图片、html、附件) 详见http://blog.csdn.net/lyisray/article/details/42002215
Spring提供了非常好用的JavaMailSender接口实现邮件发送。在Spring Boot的Starter模块中也为此提供了自动化配置。下面通过实例看看如何在Spring Boot中使用JavaMailSender发送邮件。
本资源是一个集合发送图片,附件,内容,HTML等得JAVAMAIL代码,并且可以同时发送多个附件多个图片,只需填充VO类,即可直接调用并发送邮件,例子已经写好,只要填充上必要值即可,经163,qq,sina测试没有问题,有...