- 浏览: 305480 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
di1984HIT:
不错,不错。
Jquery.LazyLoad.js插件修正版下载,实现图片延迟加载特效 -
fncj:
好东西,
freemarker常用技巧 -
xwy55555:
说得很清楚
大型网站框架的演变 -
gxz1989611:
这个东西好,我转载了~~
40 个轻量级 JavaScript 库 -
gxz1989611:
恩,这个标题真是考虑到了SEO啊!!!
这才是真正的JQuery.ajax传递中文参数的解决方法
Spring邮件发送(可带附件,模板,群发,异步发送等功能)
- 博客分类:
- JAVA/J2EE
-
博客分类:
- JavaEE
以下是我对spring发送邮件的总结:
分别使用了两种方法:单例模式和属性全注入的方法。
发送模式又分为:单发,群发。
可发送:text,html模板,附件等。
1、单例模式(单发,群发text)
在classpath底下新建application-mail.xml,内容如下:
Xml代码:
- <?xml version= "1.0" encoding= "UTF-8" ?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <!-- 注意:这里的参数(如用户名、密码)都是针对邮件发送者的 -->
- <bean id="mailSender"
- class = "org.springframework.mail.javamail.JavaMailSenderImpl" >
- <property name="host" >
- <value>smtp.163 .com</value>
- </property>
- <property name="javaMailProperties" >
- <props>
- <prop key="mail.smtp.auth" > true </prop>
- <prop key="mail.smtp.timeout" > 25000 </prop>
- </props>
- </property>
- <property name="username" >
- <value>username@163 .com</value>
- </property>
- <property name="password" >
- <value>password</value>
- </property>
- </bean>
- </beans>
或者把以上的Beans配置到applicaiont.xml里面也可以。
发送text格式的Email类:
- /
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.mail.SimpleMailMessage;
- import org.springframework.mail.javamail.JavaMailSender;
- public class EmailUtil {
- private static EmailUtil emailUtil = null ;
- public ApplicationContext ctx = null ;
- private EmailUtil() {
- //获取上下文
- ctx = new ClassPathXmlApplicationContext( "applicationContext-mail.xml" );
- }
- /**
- *
- * @function:获得单例
- */
- public static EmailUtil getInstance()
- {
- if (emailUtil== null )
- {
- synchronized (EmailUtil. class )
- {
- if (emailUtil== null ) {
- emailUtil = new EmailUtil();
- }
- }
- }
- return emailUtil;
- }
- public void sentEmails(String emails,String subject,String text)
- {
- //获取JavaMailSender bean
- JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender" );
- //SimpleMailMessage只能用来发送text格式的邮件
- SimpleMailMessage mail = new SimpleMailMessage();
- String email[] = emails.split(";" );
- for ( int i= 0 ;i<email.length;i++) {
- try {
- mail.setTo(email[i]);//接受者
- mail.setFrom("username@163.com" );
- mail.setSubject(subject);//主题
- mail.setText(text);//邮件内容
- sender.send(mail);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * @function:测试邮件发送
- */
- public static void main(String[] args) {
- String mail_title = "注册成功" ;
- String mail_content = "恭喜!您已经注册成功.<BR>欢迎使用本网站,主页地址:<a href='http://www.baidu.com'/>" ;
- EmailUtil email = EmailUtil.getInstance();
- email.sentEmails("username@163.com" ,mail_title,mail_content);
- }
- }
2.属性全注入的方法(单发,群发html模板)
FreeMarker是一个被广泛使用的模板框架,Spring可以很好的支持该框架。Spring为FreeMarker提供了一个
FreeMarkerConfigurer类,通过此类可方便地创建FreeMarker的基础环境,Spring提供
FreeMarkerTemplateUtils工具类来完成解析模板的任务。下面以用户注册成功后发送的模板文件
registerUser.ftl,${content}标签代表一个可被替换的动态属性。FreeMarker模板的标签支持级联属性,
如${user.id}则表示user对象的id属性。在配置文件中已设置好模板目录,所以可在类中直接用模板文件名来定位模板文件。模板文件用UTF-
8编码格式,避免中文乱码。通过设置template_update_delay属性,可让FreeMarker定期刷新模板,从而使应用程序在不重启下
更新模板。
- < html >
- < head >
- < meta http-equiv = "content-type" content = "text/html;charset=utf8" >
- </ head >
- < body >
- 恭喜您成功注册!您的用户名为:< font color = 'red' size = '30' > ${content} </ font >
- </ body >
- </ html >
- <? xml version = "1.0" encoding = "UTF-8" ?>
- < beans xmlns = "http://www.springframework.org/schema/beans"
- xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop = "http://www.springframework.org/schema/aop"
- xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <!-- 注意:这里的参数(如用户名、密码)都是针对邮件发送者的 -->
- < bean id = "mailSender"
- class = "org.springframework.mail.javamail.JavaMailSenderImpl" >
- < property name = "host" >
- < value > smtp.163.com </ value >
- </ property >
- < property name = "javaMailProperties" >
- < props >
- < prop key = "mail.smtp.auth" > true </ prop >
- < prop key = "mail.smtp.timeout" > 25000 </ prop >
- </ props >
- </ property >
- < property name = "username" >
- < value > username@163.com </ value >
- </ property >
- < property name = "password" >
- < value > password </ value >
- </ property >
- </ bean >
- </ beans >
- < bean id = "freeMarker" class = "org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" >
- < property name = "templateLoaderPath" value = "classpath:mailTemplate" /> <!--指定模板文件目录-->
- < property name = "freemarkerSettings" > <!-- 设置FreeMarker环境属性-->
- < props >
- < prop key = "template_update_delay" > 1800 </ prop > <!--刷新模板的周期,单位为秒-->
- < prop key = "default_encoding" > UTF-8 </ prop > <!--模板的编码格式 -->
- < prop key = "locale" > zh_CN </ prop > <!-- 本地化设置-->
- </ props >
- </ property >
- </ bean >
- <!-- ************************ Email Service配置 ********************************* -->
- < bean id = "emailService" class = "EmailService" >
- < property name = "sender" ref = "mailSender" > </ property >
- < property name = "freeMarkerConfigurer" ref = "freeMarker" > </ property >
- </ bean >
- </ beans >
- import javax.mail.MessagingException;
- import org.springframework.mail.javamail.JavaMailSender;
- import org.springframework.ui.freemarker.*;
- import org.springframework.web.servlet.view.freemarker.*;
- import freemarker.template.*;
- public abstract class EmailAbstract {
- protected String from;
- protected String subject;
- protected JavaMailSender sender;
- protected FreeMarkerConfigurer freeMarkerConfigurer= null ; //FreeMarker的技术类
- public String getFrom() {
- return from;
- }
- public void setFrom(String from) {
- this .from = from;
- }
- public String getSubject() {
- return subject;
- }
- public void setSubject(String subject) {
- this .subject = subject;
- }
- public JavaMailSender getSender() {
- return sender;
- }
- public void setSender(JavaMailSender sender) {
- this .sender = sender;
- }
- public void setFreeMarkerConfigurer(FreeMarkerConfigurer freeMarkerConfigurer) {
- this .freeMarkerConfigurer = freeMarkerConfigurer;
- }
- /**
- * 发送单个html格式邮件
- */
- public abstract void sendEmail(String content,String address);
- /**
- * 批量发送html格式邮件
- */
- public abstract void sendBatchEmail(String content,List<String> address);
- }
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import org.springframework.mail.javamail.MimeMessageHelper;
- import org.springframework.ui.freemarker.*;
- import org.springframework.web.servlet.view.freemarker.*;
- import freemarker.template.*;
- public class EmailService extends EmailAbstract {
- /**
- * 发送带模板的单个html格式邮件
- */
- public void sendMessage(String content,String address) throws MessagingException {
- MimeMessage msg = sender.createMimeMessage();
- //设置utf-8或GBK编码,否则邮件会有乱码,true表示为multipart邮件
- MimeMessageHelper helper = new MimeMessageHelper(msg, true , "utf-8" );
- helper.setTo(address); //邮件接收地址
- helper.setFrom(from); //邮件发送地址,这里必须和xml里的邮件地址相同一致
- helper.setSubject(subject); //主题
- String htmlText = getMailText(content); //使用模板生成html邮件内容
- helper.setText(htmlText, true ); //邮件内容,注意加参数true,表示启用html格式
- sender.send(msg); //发送邮件
- }
- /**
- * 批量发送带附件的html格式邮件
- */
- public void sendBatchEmail(String content,List<String> address) throws MessagingException {
- MimeMessage msg = sender.createMimeMessage();
- MimeMessageHelper helper = new MimeMessageHelper(msg, true , "utf-8" );
- String toList = getMailList(address);
- InternetAddress[] iaToList = new InternetAddress().parse(toList);
- msg.setRecipients(Message.RecipientType.TO,iaToList);
- helper.setFrom(from);
- helper.setSubject(subject);
- helper.setText(content, true );
- //添加内嵌文件,第1个参数为cid标识这个文件,第2个参数为资源
- helper.addInline("a" , new File( "E:/logo_a.jpg" )); //附件内容
- helper.addInline("b" , new File( "E:/logo_b.png" ));
- File file=new File( "E:/测试中文文件.rar" );
- // 这里的方法调用和插入图片是不同的,使用MimeUtility.encodeWord()来解决附件名称的中文问题
- helper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
- //如果主题出现乱码,可以使用该函数,也可以使用下面的函数
- //helper.setSubject(MimeUtility.encodeText(String text,String charset,String encoding))
- //第2个参数表示字符集,第三个为目标编码格式。
- //MimeUtility.encodeWord(String word,String charset,String encoding)
- sender.send(msg);
- }
- /**
- * 集合转换字符串
- */
- public String getMailList(List<String> to){
- StringBuffer toList = new StringBuffer();
- int length = to.size();
- if (to!= null && length < 2 ){
- toList.append(to.get(0 ));
- }else {
- for ( int i= 0 ;i<length;i++){
- toList.append(to.get(i));
- if (i!=(length- 1 )){
- toList.append("," );
- }
- }
- }
- return toList.toString();
- }
- //通过模板构造邮件内容,参数content将替换模板文件中的${content}标签。
- private String getMailText(String content) throws Exception {
- String htmlText = "" ;
- //通过指定模板名获取FreeMarker模板实例
- Template tpl = freeMarkerConfigurer.getConfiguration().getTemplate("registerUser.ftl" );
- Map map = new HashMap(); //FreeMarker通过Map传递动态数据
- map.put("content" ,content); //注意动态数据的key和模板标签中指定的属性相匹配
- //解析模板并替换动态数据,最终content将替换模板文件中的${content}标签。
- htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(tpl,map);
- return htmlText;
- }
-
}
发表评论
-
Kaptcha使用
2012-07-04 10:18 1801简单的装备起来很简单,网上一大把,就不说了。 ... -
kaptcha 验证码在spring mvc 中的使用
2012-07-04 10:11 2054... -
Java编程中“为了性能”尽量要做到的一些地方
2012-05-29 16:09 954下面是参考网络资源总结的一些在Java编程中尽可能要做到的 ... -
freemarker常用技巧
2012-05-03 10:21 310591. freemarker 简介 ... -
Quartz
2011-09-14 16:35 1451博客 ... -
eclipse下启动tomcat出现Setting property 'source' to 'org.eclipse.jst.jee.server: '错误的
2011-06-08 16:08 3383在eclipse中启动tomcat时 ... -
Maven常用插件使用说明之deploy
2011-06-02 23:37 5307Maven的deploy插件用于把Maven自动构 ... -
Hudson+Maven+SVN 快速搭建持续集成环境
2011-06-01 10:35 1670hudson 是一个可扩展的持 ... -
struts2 中使用 chain Result 时 actionMessage 和 actionError 未复制的原因
2011-05-24 22:43 2155在网上查询N久,未解决。 一说必须使用 chain Res ... -
Quartz时间规则
2011-03-07 16:26 1948* * * * * ... -
Spring Quartz 任务调度
2011-03-07 16:07 1471关 ... -
可以设置你提交代码是必须写注释,可以规定长度至少为多少
2010-12-23 13:58 3759不少开发员提交修改的时候都不写注释,导致查看历史时很费劲,也不 ... -
Tomcat UTF-8 乱码问题解决办法
2010-12-20 16:13 2198关于java的乱码,一直是双字节字符环境开发者的问题,不同 ... -
eclipse中将java项目转换为web项目
2010-11-19 16:03 28171、进入项目目录,找到.project文件,打开。 2、 ... -
poi 新手教程
2010-10-28 15:10 1243poi 新手教程 Jakarta_POI 使用Ja ... -
Subversion Windows迁移到Linux服务器的步骤
2010-06-01 09:59 1442首先在Windows上,使用svnadmin dump res ... -
JAVA读写Properties文件
2009-04-30 21:05 3341JAVA读写Properties文件 ... -
Tomcat out of memory解决
2009-03-01 23:09 6841Tomcat 默认可以使用的内存为 128MB ,在较大型 ... -
Tomcat加入到系统服务中
2009-01-20 09:55 1471命令行使用sc命令.关于sc命令的详解,请自 ... -
web.xml的配置顺序
2008-12-30 14:31 1024filter,filter-mapping,listener, ...
相关推荐
3、Spring Boot 发送邮件(文本格式的邮件、发送HTML格式的邮件、发送带附件 的邮件、发送带静态资源的邮件) 个人觉得Springboot的开发简单的归纳为三步jar包引入,配置,应用。 (一)简单使用 1)JSP的使用...
使用Spring Boot 开发支持多附件邮件发送微服务平台代码
本例为完整的采用spring 发送带 附件的邮件。 下载后需要改动的地方: 1.在mail.properties中将接收者邮箱地址(username),邮箱密码换成真实的。 2.在SpringJavaMailDemo类中把发送者和接收者邮箱地址换成真实地址...
邮件模板发送邮件模板发送 为邮件定义HTML模板
Spring邮件发送服务(java邮件发送),支持多附件、多人发送/抄送/暗送
java 发送邮件 spring发送邮件Mail
Spring 封装的JavaMail,使邮件发送变的相当简单,我通过Spring的邮件发送实现了发纯文本形的内容,HTMl,带附件,带图片等示例代码,并自行测试过.希望对初学者有帮助
Spring实现各种邮件的发送 简单配置就可发送邮件
基于spring mvc 框架,用freemarker创建邮件模板,包括详细邮件发送功能的spring配置和实现代码
使用SpringBoot发送邮件功能,可以发送文本邮件,图片邮件,模板邮件,html邮件
ActiveMQ与Spring整合之异步发送邮件。
spring boot发送普通文本邮件/HTML邮件/附件邮件/图片邮件完整代码.zip
NULL 博文链接:https://chaoyi.iteye.com/blog/2171158
利用spring下的一个类进行邮件发送,内附具体用法
Spring Boot通过集成spring-boot-starter-mail依赖和相关配置,可以方便地实现邮件发送功能。具体的步骤: 添加依赖:在Spring Boot项目的pom.xml文件中加入spring-boot-starter-mail依赖,如果需要发送模板邮件,还...
数据库:sqlserver CreateHtmlTable中的生成路径和SendService中的上传路径改成自己的
Velocity模板邮件发送例子(Spring框架)(可通用,支持多扩展)Velocity模板邮件发送例子(Spring框架)(可通用,支持多扩展)
这个java project的源代码 博文链接:https://kukuqiu.iteye.com/blog/161771
Spring整合Quartz定时发送邮件
主要为大家详细介绍了Spring Boot实现邮件发送功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下