Spring整合JavaMail
1.添加jar包
#此处省略spring基础相关jar包描述,以下是发送邮件相关jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
2.配置文件
1.mail.properties
mail.smtp.host=smtp.163.com
mail.username=邮箱帐号(例如:abc)
mail.password=邮箱密码(例如:123456)
mail.smtp.auth=true
mail.from=发送邮箱(例如:abc@163.com)
2.applicationContext-mail.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:property-placeholder location="classpath:mail.properties"/>
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="${mail.from}"></property>
</bean>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.smtp.host}"></property>
<property name="username" value="${mail.username}"></property>
<property name="password" value="${mail.password}"></property>
<property name="defaultEncoding" value="UTF-8"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
<prop key="mail.debug">true</prop>
<prop key="mail.smtp.timeout">0</prop>
</props>
</property>
</bean>
</beans>
3.Java代码
import java.io.File;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
public class MailTest {
@Test
public void test() throws MessagingException {
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext-mail.xml");
SimpleMailMessage message = (SimpleMailMessage) act.getBean("mailMessage");
message.setSubject("约会");
message.setText("2017年5月10日在西湖边见面");
message.setTo("xxxx@163.com");
JavaMailSender sender = (JavaMailSender) act.getBean("mailSender");
sender.send(message);
}
@Test
public void test2() throws MessagingException {
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext-mail.xml");
JavaMailSender sender = (JavaMailSender) act.getBean("mailSender");
MimeMessage message = sender.createMimeMessage();
//使用工具类设置附件
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom("aaa@163.com");
helper.setTo("bbb@163.com");
helper.setSubject("来自百度的主题");
helper.setText("<html><head></head><body><h2>你好</h2>"
+ "<a href='http://www.baidu.com'>百度</a>"
+ "<img src=cid:image></body></html>",true);
//带图片
FileSystemResource jpg = new FileSystemResource(new File("d:\\test.jpg"));
helper.addInline("image", jpg);//此处的image必须与html代码段中的<img src=cid:image>一致
//带附件
FileSystemResource attach = new FileSystemResource(new File("d:\\intro.csv"));
helper.addInline("intro.csv", attach);
sender.send(message);
}
}
Spring整合JavaMail的更多相关文章
- 项目一:第十四天 1.在realm中动态授权 2.Shiro整合ehcache 缓存realm中授权信息 3.动态展示菜单数据 4.Quartz定时任务调度框架—Spring整合javamail发送邮件 5.基于poi实现分区导出
1 Shiro整合ehCache缓存授权信息 当需要进行权限校验时候:四种方式url拦截.注解.页面标签.代码级别,当需要验证权限会调用realm中的授权方法 Shiro框架内部整合好缓存管理器, ...
- 使用Spring整合javaMail发用邮件
1.导入javamail.jar 自行百度下载 2.使用模板发送邮件架包 freemarker.jar 3.Spring配置文件 以及架包这里就不需要说了吧,如果不明白的发我Email ...
- 使用spring的JavaMail发送邮件
以前我们使用JavaMail发送邮件,步骤挺多的.现在的项目跟Spring整合的比较多.所以这里主要谈谈SpringMail发送. 导入jar包. 配置applicationContext-email ...
- 使用Spring整合Quartz轻松完成定时任务
一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...
- 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】
一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...
- spring整合hibernate的详细步骤
Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...
- Spring整合Ehcache管理缓存
前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...
- spring整合hibernate
spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...
- MyBatis学习(四)MyBatis和Spring整合
MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...
随机推荐
- springboo 添加logback日志
springboot默认引入logback相关的jar包 1.在 Application.properties里添加 logging.config=classpath:logback-spring.x ...
- mail_location not set and autodetection failed 解决方案[devecot, sendmail]
安装dovecot比较简单, 但是也需要配置, 如果不进行任何配置时,在测试时会出现如下的提示: dovecot: pop3(wwufengg): Error: user wwufengg: Init ...
- linux oracle11g 数据 导入到10g数据库
说明: 原用户名和密码:test/test 目标用户名和密码:test01/test 11G 服务器: 1.创建dmp文件存储目录 # mkdir -p /tmp/backup # sqlplus ...
- SQL语句往Oracle数据库中插入日期型数据(to_date的用法)
Oracle 在操作数据库上相比于其他的 T-sql 有微小的差别,但是在插入时间类型的数据是必须要注意他的 to_date 方法,具体的情况如下: --SQL语句往Oracle数据库中插入日期型数据 ...
- Vue项目上线后刷新报错404问题(apache,nginx,tomcat)
转自:https://www.cnblogs.com/sxshaolong/p/10219527.html 很简单,需要 服务器端 加个配置文件,然后 重启服务就好了,记住一定要 重启服务,否则无效!
- PHP工作笔记:遍历文件夹返回文件数组
直接输入文件夹的路径,调用函数即可返回文件夹里面的文件数组,不返回文件夹 <?php function scanfiles($folder){ $folder = $folder."* ...
- Hibernate主键生成策略详解
转载自:http://blog.csdn.net/wanghuan203/article/details/7562395 hibernate提供的主键生成策略,使我们可以在实体类的映射xml文件中设定 ...
- 关于$_SERVER['PHP_AUTH_USER']
http://www.cnblogs.com/thinksasa/p/3421379.html PHP 的 HTTP 认证机制仅在 PHP 以 Apache 模块方式运行时才有效,因此该功能不适用于 ...
- losetup命令
Linux losetup命令用于设置循环设备. 循环设备可把文件虚拟成区块设备,籍以模拟整个文件系统,让用户得以将其视为硬盘驱动器,光驱或软驱等设备,并挂入当作目录来使用. 语法 losetup [ ...
- 使用jQuery操作DOM(1)
1.常见方法 css(“属性”,”属性值”); //设置单个样式 css({属性1:属性值1,属性2:属性值3...}); //设置多个样式 addClass(“样式名”); //追加单个样式 add ...