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. pandas dataframe在指定的位置添加一列, 或者一次性添加几列,re

    相信有很多人收这个问题的困扰,如果你想一次性在pandas.DataFrame里添加几列,或者在指定的位置添加一列,都会很苦恼找不到简便的方法:可以用到的函数有df.reindex, pd.conca ...

  2. rabbitmq&&erlang 安装

    # yum install epel-release CentOS and Red Hat Enterprise Linux 6.x wget https://dl.fedoraproject.org ...

  3. [SQL]会引起全表扫描的10种SQL语句

    1.模糊查询效率很低: 原因:like本身效率就比较低,应该尽量避免查询条件使用like:对于like ‘%...%’(全模糊)这样的条件,是无法使用索引的,全表扫描自然效率很低:另外,由于匹配算法的 ...

  4. 【微软混合现实】开始使用Unity-第一章:创建一个新的项目

    使用Unity开发App,第一步需要创建一个项目.项目具有一系列组织好文件夹,其中最重要的是你的附件文件夹(Assets folder).在这个文件夹中,存储了从其他工具中创建的数字内容,比如Maya ...

  5. 重温MVC基础入门

    重温MVC基础入门   简介 本文主要是作者回顾MVC基础的文章,整合个人认为基础且重点的信息,通过简单实践进行复习. 相关代码地址:https://github.com/OtherRuan/Revi ...

  6. CMDB配置资源管理数据库(理解)

    CMDB是运维自动化的基础,它为日志系统,发布系统,监控系统等运维系统(ELK,zabbix,open-falcon)提供接口函数, 第一种方式:Agent方法实现,agent不能直接访问数据库,因为 ...

  7. LeetCode OJ : Different Ways to Add Parentheses(在不同位置增加括号的方法)

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  8. week13《java程序设计》第13次作业总结

    week13<java程序设计>第13次作业总结 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 答: 1.IP与端口:ip和域名相对应,可找 ...

  9. SpringInAction--面向切片的Spring以及如何使用注解创建切面

    什么叫做切片..什么叫做AOP... 与大多数技术一样,AOP已经形成了自己的术语.描述切面的常用术语有通知(advice).切点(pointcut)和连接点(join point). (一大串书上的 ...

  10. openssl编译出错解决

    tar -jxvf trafficserver-3.0.2.tar.bz2 ./configure --prefix=/usr/install/trafficserver --with-user=ca ...