使用Spring整合javaMail发用邮件
1.导入javamail.jar 自行百度下载
2.使用模板发送邮件架包 freemarker.jar
3.Spring配置文件 以及架包这里就不需要说了吧,如果不明白的发我Email :xkjava@sina.com
//邮件信息设置类 main.java
package cn.jbit.email; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility; import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException; public class Mail {
private JavaMailSender mailSender;
private Configuration configuration; public Configuration getConfiguration() {
return configuration;
} public void setConfiguration(Configuration configuration) {
this.configuration = configuration;
} public JavaMailSender getMailSender() {
return mailSender;
} public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
} //模板設置
private String getMailText() { String htmtext = "";
// 发信内容
String mailcontent = "你好,欢迎使用尊云服务器,在这里,你的数据更加安全、性能更加稳定!";
try {
// 获取实例模板
Template template = configuration.getTemplate("Template02.ftl"); // 通过map传递动态数据
Map map = new HashMap();
map.put("name", "简单");
map.put("content", mailcontent);
htmtext = FreeMarkerTemplateUtils.processTemplateIntoString(
template, map); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TemplateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return htmtext; } //发送邮件
public void send() throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true,"UTF-8");
try {
helper.setFrom("xkjava@163.com");
helper.setTo("xkjava@sina.com");
helper.setSubject("歡迎來到員工社區");
helper.setText(getMailText(),true);
//多个附件发送,先放进集合,注意这里的文件路径
List<String> strFilePath = new ArrayList<String>();
//文件路径
strFilePath.add("cn/jbit/template/1.doc");
strFilePath.add("cn/jbit/template/he_header.png"); for (String strfile : strFilePath) {
//带附件发送邮件
ClassPathResource file = new ClassPathResource(strfile);
helper.addAttachment(file.getFilename(), file.getFile());
} //发送
mailSender.send(message); } catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
/* simpleMailMessage.setFrom("kzhan8082@163.com");
simpleMailMessage.setTo("xkjava@sina.com");
simpleMailMessage.setText("HelloJavaMail!!!!");
simpleMailMessage.setSubject("Hello");*/ //mailSender.send(simpleMailMessage); // 发送邮件 } }
//Spring applicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!--通过模板发送邮件-->
<bean id="freeMarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
<!-- 模板路徑 --> <!--模板路径,这里根据自己的模板路径来,模板的后缀名为 .ftl-->
<property name="templateLoaderPath" value="cn/jbit/template"></property>
<!-- 设置FreeMarke环境变量 -->
<property name="freemarkerSettings">
<props>
<!--这里暂时不配,防止中文乱码-->
</props>
</property>
</bean> <!-- 设置邮件信息 这里就是用163的邮箱做demo,方便-->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.163.com"></property>
<property name="port" value="25"></property>
<property name="username" value="邮箱账户"></property>
<property name="password" value="邮箱密码"></property>
<property name="defaultEncoding" value="UTF-8"></property>
<property name="protocol" value="smtp"></property>
<property name="javaMailProperties">
<props>
<!-- 设置smtp服务器需要用户验证 -->
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>
<!-- IOC注入 -->
<bean id="mailsend" class="cn.jbit.email.Mail">
<property name="mailSender" ref="mailSender"></property>
<property name="configuration" ref="freeMarkerConfiguration"></property>
</bean>
</beans>
//测试类MailTest.java
package cn.jbit.email; import javax.mail.MessagingException; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MialTest { /**
* @param args
* @throws MessagingException
*/
public static void main(String[] args) throws MessagingException {
// TODO Auto-generated method stub
/*Mail mail = new Mail();
mail.send();*/
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Mail mail02 = (Mail)applicationContext.getBean("mailsend");
mail02.send();
System.out.println("success");
} }
这里的功能比较多,注意看设置邮件信息的 mail.java 类。里面涉及了多个附件的发送,是用模板发送。注意导入命名空间的架包。
使用Spring整合javaMail发用邮件的更多相关文章
- 项目一:第十四天 1.在realm中动态授权 2.Shiro整合ehcache 缓存realm中授权信息 3.动态展示菜单数据 4.Quartz定时任务调度框架—Spring整合javamail发送邮件 5.基于poi实现分区导出
1 Shiro整合ehCache缓存授权信息 当需要进行权限校验时候:四种方式url拦截.注解.页面标签.代码级别,当需要验证权限会调用realm中的授权方法 Shiro框架内部整合好缓存管理器, ...
- Spring整合JavaMail
1.添加jar包 #此处省略spring基础相关jar包描述,以下是发送邮件相关jar包 <dependency> <groupId>org.springframework&l ...
- Selenium 2自动化测试实战38(整合自动发邮件功能)
整合自动发邮件功能 解决了前面的问题后,现在就可以将自动发邮件功能集成到自动化测试项目中了.下面重新编辑runtest.py文件 #runtest.py #coding:utf-8 from HTML ...
- Spring MVC+javamail实现邮件发送
Spring MVC+javamail实现邮件发送 开启邮箱的POP3/SMTP服务(这里以QQ邮箱举例) 设置 --> 账户 -- > 开启POP3/STMP服务,然后得到一个授权码. ...
- 通过spring实现javamail发送邮件功能
以前很早的时候大家都用javamail实现发送邮件的功能,而且我们也一直沿用至今,代码拷过来用用就行了,现在我们改为用spring来实现,这样一来减少代码的复杂度,也能更好的契合spring理念 首先 ...
- Spring的JavaMail实现异步发送邮件
具体背景就不说了,可以网上搜索相关知识,或者直接看Sping MailSender的官坊网页.这里就直接实战了(Java实现异步发送电子邮件,包含中文无乱码). Maven: <dependen ...
- 使用spring的JavaMail发送邮件
以前我们使用JavaMail发送邮件,步骤挺多的.现在的项目跟Spring整合的比较多.所以这里主要谈谈SpringMail发送. 导入jar包. 配置applicationContext-email ...
- 初识quartz 并分析 项目中spring整合quartz的配置【原创+转载】
初识quartz 并分析 项目中spring整合quartz的配置[原创+转载]2018年01月29日 12:08:07 守望dfdfdf 阅读数:114 标签: quartz 更多个人分类: 工具 ...
- Hibernate+Spring整合开发步骤
Hibernate是一款ORM关系映射框架+Spring是结合第三方插件的大杂烩,Hibernate+Spring整合开发效率大大提升. 整合开发步骤如下: 第一步:导入架包: 1.Hibernate ...
随机推荐
- dr.wondr博士随笔之三星某古董智能机GTXXXX 的取证恢复一例
大家好!欢迎来到我dr.wonde博士的微博! 这是dr.wonde的第一篇微博,不足之处,还请见谅. 今天dr.wonde给你们带来不可能的数据恢复任务之三星非智能机古董机GT-E1088C 的恢复 ...
- 一个注解方式webSocket demo
前段时间在研究websocket.其中遇到了一些bug.这里跟大家分享这过程. 首先介绍一下websocket WebSocket是HTML5的一种新协议,实现了浏览器和服务器的双全工通信,能更好的节 ...
- LeetCode----172. Factorial Trailing Zeroes(Java)
package singlenumber136; //Given an array of integers, every element appears twice except for one. F ...
- hasOwnProperty
var Person = function(){ this.name = "nike"; this.age = "20" } var person = new ...
- 微信 xml 转 Map
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; xml + ...
- Redis - 作为 LRU 缓存
一.简介 LRU 实际上是被唯一支持的数据移除方法,同时也是 memcached 默认支持的缓存算法. 二.配置内存大小 在 redis.conf 文件中使用 maxmemory 指令能够配置内存大小 ...
- jodaTime 的使用说明
<dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifact ...
- 翻译:在Ubuntu 14.04上安装FTP服务器的方法
说明: 1.原文地址:http://www.krizna.com/ubuntu/setup-ftp-server-on-ubuntu-14-04-vsftpd/ 2.今天要做一个网络日志的迁移程序,搬 ...
- 微内核架构(Microkernel Architecture)
微内核架构(Microkernel Architecture) 微内核架构有时也被成为插件架构模式(plug-in architecture pattern),通常用于实现基于产品的应用,如Eclip ...
- 浅谈JavaScript中的闭包
浅谈JavaScript中的闭包 在JavaScript中,闭包是指这样一个函数:它有权访问另一个函数作用域中的变量. 创建一个闭包的常用的方式:在一个函数内部创建另一个函数. 比如: functio ...