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. 项目一:第十四天 1.在realm中动态授权 2.Shiro整合ehcache 缓存realm中授权信息 3.动态展示菜单数据 4.Quartz定时任务调度框架—Spring整合javamail发送邮件 5.基于poi实现分区导出

    1 Shiro整合ehCache缓存授权信息 当需要进行权限校验时候:四种方式url拦截.注解.页面标签.代码级别,当需要验证权限会调用realm中的授权方法   Shiro框架内部整合好缓存管理器, ...

  2. 使用Spring整合javaMail发用邮件

    1.导入javamail.jar        自行百度下载 2.使用模板发送邮件架包 freemarker.jar 3.Spring配置文件  以及架包这里就不需要说了吧,如果不明白的发我Email ...

  3. 使用spring的JavaMail发送邮件

    以前我们使用JavaMail发送邮件,步骤挺多的.现在的项目跟Spring整合的比较多.所以这里主要谈谈SpringMail发送. 导入jar包. 配置applicationContext-email ...

  4. 使用Spring整合Quartz轻松完成定时任务

    一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...

  5. 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】

    一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...

  6. spring整合hibernate的详细步骤

    Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...

  7. Spring整合Ehcache管理缓存

    前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...

  8. spring整合hibernate

    spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...

  9. MyBatis学习(四)MyBatis和Spring整合

    MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...

随机推荐

  1. tinyxml解析xml

    基于tinyxml做的简单的xml解析. 1.创建xml bool CreateXmlFile(string& szFileName) {//创建xml文件,szFilePath为文件保存的路 ...

  2. IOS-涂鸦

    // // PaintView.m // IOS_0224_涂鸦 // // Created by ma c on 16/2/24. // Copyright © 2016年 博文科技. All ri ...

  3. day21 git & github + Celery 分布式任务队列

    参考博客: git & github 快速入门http://www.cnblogs.com/alex3714/articles/5930846.html git@github.com:liyo ...

  4. mybatis定义拦截器

    applicationContext.xml <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlS ...

  5. VS展开当前目录

  6. 【HTML5】使用 JavaScript 来获取电池状态(Battery Status API)

    HTML5 规范已经越来越成熟,可以让你访问更多来自设备的信息,其中包括最近提交的 "Battery Status API".如其名称所示,该 API 允许你通过 JavaScri ...

  7. Linux下的网络设定

    一.IP相关介绍 1.IP是internet protocal的简称,也叫网络进程. 2.ipv4全称internet protocal version 4.它是由32个二进制组成:改为十进制的话,一 ...

  8. EasyPusher/EasyDarwin支持H.265 RTSP/RTP直播推流与分发播放

    前言描述 随着大屏时代和高清时代的到来,人们已经不再满足于VGA.CIF这种小分辨率了,取而代之的是720P.1080P.4K级的视频传输,虽然我们国家的基础带宽一直在上升,但普遍情况下,传输高清视频 ...

  9. Echart--百度地图(散点图)

    参考:http://blog.csdn.net/xieweikun7/article/details/52766676 1.首先,下载嘛 Echarts http://echarts.baidu.co ...

  10. C#中PadLeft和PadRight小知识点

    当我们显示字符串数据时,有时候我们需要考虑数据的排列美观. 比如一些人名和一些编号,我们想让他们整齐对齐显示等. C# String类提供了2种操作方法: String.PadLeft(int tot ...