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.xsdhttp://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 {@Testpublic 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);}@Testpublic 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);}
}

转载于:https://www.cnblogs.com/moonlightL/p/7271325.html

Spring整合JavaMail相关推荐

  1. ssh项目实战----Spring计时器任务 Spring整合JavaMail(邮件发送)

    一.常用数据频度维护 对于系统使用度较高的数据,客户在查看时希望这些数据最好先出现,此时需要为其添加排序规则.在进行排序时,使用次数成为排序的依据.因此需要设置一个字段用来描述某种数据的使用次数,也就 ...

  2. 【struts2+hibernate+spring项目实战】Spring计时器任务 Spring整合JavaMail(邮件发送)(ssh)

    一.常用数据频度维护 对于系统使用度较高的数据,客户在查看时希望这些数据最好先出现,此时需要为其添加排序规则.在进行排序时,使用次数成为排序的依据.因此需要设置一个字段用来描述某种数据的使用次数,也就 ...

  3. JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试

    Spring框架学习路线 Spring的IOC Spring的AOP,AspectJ Spring的事务管理,三大框架的整合 Spring框架概述 什么是Spring?  Spring是分层的Java ...

  4. Strutsw2与Spring整合流程-简述

    1.      新建WEB工程: 2.      导入struts2开发包,和资源配置文件 ① globalMessages.properties ② struts.properties 3.     ...

  5. 最新Spring整合MyBatis详解教程

    目录 1.导入相关jar包 1. junit 2. mybatis 3. mysql 4. spring相关 5. aop织入 6. mybatis-spring 7. lombok(选用) 2.回顾 ...

  6. Spring整合Struts2

    ①导入Struts2 jar包 ②在web.xml文件中创建过滤器 <?xml version="1.0" encoding="UTF-8"?> & ...

  7. shiro和Spring整合使用注解时没有执行realm的doGetAuthorizationInfo回调方法的解决

    shiro和Spring整合使用注解时没有执行realm的doGetAuthorizationInfo回调方法的解决 from :http://blog.csdn.net/babys/article/ ...

  8. Spring整合CXF,发布RSETful 风格WebService

    这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有很大一部分都是一样的.关于发布CXF WebServer和Spring整合CXF这里就不再多加赘述了.如果你对Spring整合CXF ...

  9. springMvc+mybatis+spring 整合 包涵整合activiti 基于maven

    2019独角兽企业重金招聘Python工程师标准>>> 最近自己独立弄一个activiti项目,写一下整合过程: 环境:jdk1.7 tomcat7.0 maven3.5  ecli ...

  10. activiti自己定义流程之Spring整合activiti-modeler5.16实例(四):部署流程定义

    注:(1)环境搭建:activiti自己定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建         (2)创建流程模型:activiti自己定义流程之Spr ...

最新文章

  1. 晒桌面 | 非主流五线产品汪的简单原木风工作台
  2. python面试题库——3数据库和缓存
  3. rgba的兼容性 IE
  4. spring-在配置文件中配置DAO时直接引用DataSource
  5. ssh 双机互信配置记要
  6. phpstudy中php页面不识别php代码解决方法
  7. python实现logistic增长模型
  8. el 能否定义作用域变量_JS块级作用域和let,const,var区别
  9. 械体叶NUNNECH FEWNE OFUEenTM 17.1+教程与算例
  10. python 加汉明窗_什么是汉明窗?加Hanmming窗的作用?
  11. 风尚云网学习-css实现文字超出隐藏为省略号...
  12. 解决安装MATLAB2018a后出现License Manager Error -8的问题
  13. Alex Fung魔方解法学习记
  14. 推荐交互设计师阅读的一本书
  15. android 键盘遮挡
  16. 【Python学习向】 图片去水印
  17. SQL注入之布尔型盲注
  18. list集合用stream流distinct去重失效问题
  19. book mac pro怎么重装系统_macbook pro怎么重装mac系统
  20. java实现如何定时给微信群中发送消息

热门文章

  1. jdbc连接orcle数据库_java连接Oracle数据库
  2. Java线程池ThreadPoolExecutor使用与解析
  3. Vue.js 5 @慕课网
  4. 纯c++实现之滚动窗口
  5. 设计思想之高内聚低耦合
  6. 设备、线程-Android音频系统之AudioFlinger(二)-by小雨
  7. RHEL 6.2 Error: Cannot create GC thread. Out of system resources.
  8. lua与c若干问题 - 专职C++ - C++博客
  9. auto, auto, const auto以及其它形式的auto变种在for-range loop的选择
  10. MATLAB: 你不知道的12个基础知识