一、Spring Mail API

  Spring邮件抽象层的主要包为org.springframework.mail,Spring提供的邮件发送不仅支持简单邮件的发送、添加附件。

1、邮件发送的核心接口:MailSender,实现类JavaMailSenderImpl;
2、邮件对象:SimpleMailMessage 封装了简单邮件的属性如from, to,cc, subject,text;
org.springframework.mail.javamail.JavaMailSender和org.springframework.mail.javamail.MimeMessagePreparator, 用于准备JavaMail的MIME信件。

引入相关jar包:spring 相关jar包  mail.jar 和 activation.jar

二、Spring mail 发送邮件

1、配置

(1)mail.properties

# mail host
mail.host=smtp.163.com
# smtp port
mail.port=25
mail.encoding=UTF-8
mail.smtp.auth=true
# request timeout
mail.smtp.timeout=25000
mail.username=cac2020@163.com
mail.password=123456
mail.from=email@163.com;

(2)spring-mail.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- 扫描该包下的Bean -->
<context:component-scan base-package="com.jeecg.mail" /> <!-- 加载Properties文件 -->
<context:property-placeholder location="classpath:mail.properties" /> <!-- 邮件发送 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>${mail.host}</value>
</property>
<property name="username">
<value>${mail.username}</value>
</property>
<property name="password">
<value>${mail.password}</value>
</property>
<property name="port">
<value>${mail.port}</value>
</property>
<property name="defaultEncoding">
<value>${mail.encoding}</value>
</property>
<property name="protocol">
<value>smtp</value>
</property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
<!-- <prop key="mail.smtp.starttls.enable">true</prop> 取决于使用的邮件服务是否支持STARTTLS加密 -->
<!-- <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop> 取决于是否使用SSL端口发送邮件-->
<prop key="mail.smtp.socketFactory.fallback">false</prop>
<prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
<!-- <prop key="mail.sendTo">${mail.sendTo}</prop> -->
<prop key="mail.debug">true</prop>
</props>
</property>
</bean> <!-- 异步线程 -->
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="10" />
<property name="maxPoolSize" value="30" />
</bean>
</beans>

(3)发送邮件

package com.jeecg.mail.controller;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest; import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView; import com.jeecg.mail.service.MailServiceImpl; @Scope("prototype")
@Controller
@RequestMapping("/MailController")
public class MailController extends BaseController
{
@Autowired
private MailServiceImpl mailService; @RequestMapping(params = { "mail", "page" })
public ModelAndView mail(@RequestParam("page") String page,HttpServletRequest request)
{
ModelAndView modelAndView = new ModelAndView(page);
return modelAndView;
} @RequestMapping(params = "sendTextMail", method = { RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public AjaxJson sendTextMail()
{
AjaxJson json = new AjaxJson();
try
{
mailService.sendTextMail("cac2020@163.com", "530589482@qq.com", "cac2020@126.com", "【测试】发送一个纯文本邮件",
"哈哈哈哈哈哈 这是一个纯文本邮件");
}
catch (Exception e)
{
json.setMsg(e.getMessage());
e.printStackTrace();
}
return json;
} @RequestMapping(params = "sendHTMLMail", method = { RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public AjaxJson sendHTMLMail()
{
AjaxJson json = new AjaxJson();
try
{
mailService.sendHTMLMail("cac2020@163.com", "530589482@qq.com", "cac2020@126.com", "【测试】发送一个HTML邮件",
"<html><head></head><body><h1>hello!!spring html Mail</h1></body></html>");
}
catch (MessagingException e)
{
e.printStackTrace();
json.setMsg(e.getMessage());
}
return json;
} @RequestMapping(params = "sendImageMail", method = { RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public AjaxJson sendImageMail(HttpServletRequest request)
{
AjaxJson json = new AjaxJson();
try
{
String jpgpath = request.getSession().getServletContext().getRealPath("/images/1.jpg");
mailService.sendImageMail("cac2020@163.com", "530589482@qq.com", "cac2020@126.com", "【测试】发送一个含图片邮件",
"<html><head></head><body><h1>hello!!spring image html mail</h1><img src=\"cid:1\"/></body></html>",
"1",
jpgpath);
}
catch (MessagingException e)
{
e.printStackTrace();
json.setMsg(e.getMessage());
}
return json;
} @RequestMapping(params = "sendAttachMail", method = { RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public AjaxJson sendAttachMail(HttpServletRequest request)
{
AjaxJson json = new AjaxJson();
try
{
String filepath = request.getSession().getServletContext().getRealPath("/images/测试.rar");
mailService.sendAttachMail("cac2020@163.com", "530589482@qq.com", "cac2020@126.com", "【测试】发送一个含附件邮件",
"",
"测试.rar",
filepath);
}
catch (MessagingException e)
{
e.printStackTrace();
json.setMsg(e.getMessage());
}
return json;
} }

注意:

1、Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:property-placeholder/>),其余的会被Spring忽略掉

Spring 配置文件中<context:property-placeholder/>标签如果有多个properties文件   location='classpath:db.properties,classpath:default.properties,classpath:default3.properties,classpath:default2.properties'

参考:

http://blog.csdn.net/smcwwh/article/details/7095027

http://blog.csdn.net/zheng0518/article/details/50179213

http://www.oschina.net/code/snippet_2416648_55116

http://blog.csdn.net/zh921112/article/details/38397801

http://blog.csdn.net/houxuehan/article/details/52184736

http://www.zgxue.com/167/1677686.html

Java邮件服务学习之四:邮箱服务客户端Spring Mail的更多相关文章

  1. 搭建服务与负载均衡的客户端-Spring Cloud学习第二天(非原创)

    文章大纲 一.Eureka中的核心概念二.Spring RestTemplate详解三.代码实战服务与负载均衡的客户端四.项目源码与参考资料下载五.参考文章 一.Eureka中的核心概念 1. 服务提 ...

  2. 微服务学习一 微服务session 管理

    集群和分布式架构中: session管理有三种方法: 1: Cookie: 将Session对象保存在Cookie,保存在浏览器端.浏览器发送请求的时候,会把整个session放在请求里一起发送到se ...

  3. spring boot:发送带附件的邮件和html内容的邮件(以163.com邮箱为例/spring boot 2.3.2)

    一,网站哪些情况下需要发送电子邮件? 作为一个电商网站,以下情况需要发邮件通知用户: 注册成功的信息 用邮箱接收验证码 找回密码时发链接 发送推广邮件 下单成功后的订单通知 给商户的对账单邮件 说明: ...

  4. 【Java语言特性学习之四】常用集合

    java中常见的数据结构

  5. Springboot集成邮箱服务发送邮件

    一.前言 Spring Email 抽象的核心是 MailSender 接口,MailSender 的实现能够把 Email 发送给邮件服务器,由邮件服务器实现邮件发送的功能. Spring 自带了一 ...

  6. Java邮件服务学习之三:邮箱服务客户端-Java Mail

    一.java mail的两个JAR包 1.mail.jar:不在JDK中,核心功能依赖JDK4及以上,该jar包已经加入到java EE5: 下载地址:http://www.oracle.com/te ...

  7. Java邮件服务学习之一:邮件服务概述

    java可以提供邮件服务:一般理解的邮件服务就是可以发送和接收邮件的客户端,另外就是使用java编写邮件服务端:两者区别在于客户端只负责给终端客户收发邮件,就相当于小区楼下的那一排排的铁皮邮箱盒,而邮 ...

  8. java springboot activemq 邮件短信微服务,解决国际化服务的国内外兼容性问题,含各服务商调研情况

    java springboot activemq 邮件短信微服务,解决国际化服务的国内外兼容性问题,含各服务商调研情况 邮件短信微服务 spring boot 微服务 接收json格式参数 验证参数合 ...

  9. gRPC学习之四:实战四类服务方法

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

随机推荐

  1. Java根据html模板创建 html文件

    1.创建html的java代码 package com.tydic.eshop.util; import java.io.FileInputStream; import java.io.FileOut ...

  2. nyoj-291 互素数个数 欧拉函数

    LK的数学题 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 LK最近遇到一个问题,需要你帮她一下.一个整数n,求[1,n)中,和n互素的数的个数.   输入 多组测 ...

  3. android移植

    root@phone-desktop:/opt/4418-source/android4.4.2_r1# ./device/nexell/tools/build.sh -b drone2 -t u-b ...

  4. Windows下搭建MySql Master-Master Replication

    1.首先下载最新版的MySql Server (http://dev.mysql.com/downloads/windows/installer/) 2.安装MySql Server到两台机器上 My ...

  5. Android手机拍照

    参考的这个视频教程:http://v.youku.com/v_show/id_XNjI5MzkzMjQ4.html和官方API档file:///D:/Android/androidstudio/sdk ...

  6. 1742. Team building(dfs)

    1742 最小的是找联通块数 最大的找环 一个环算一个 其它的数各算一个 #include <iostream> #include<cstdio> #include<cs ...

  7. treap完全版模板

    这是我综合poj1442 3481 2352的treap操作 得到treap完全版模板.(经测AC) 结构体Tree { int key; //键值 int size; //该子树总节点个数 int ...

  8. 数据库编程与C#编程互译

    今天有一段代码,先是用程序实现. 闲来无聊,又用存储过程实现了一次. 程序中实现. /// <summary> /// 根据区域和用户名获取可访问的国家 /// </summary& ...

  9. LeetCode Best Time to Buy and Sell Stock 买卖股票的最佳时机 (DP)

    题意:给定一个序列,第i个元素代表第i天这支股票的价格,问在最佳时机买入和卖出能赚多少钱?只买一次,且仅1股,假设本钱无限. 思路:要找一个最低价的时候买入,在最高价的时候卖出利润会最大.但是时间是不 ...

  10. Java [Leetcode 111]Minimum Depth of Binary Tree

    题目描述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...