开发工具:STS

代码下载链接:https://github.com/theIndoorTrain/Springboot/tree/8878e8e89ce01ceb967ef8c1193ac740a6f7dd40

前言:

每当你生日那天,腾讯官方都会给你致上一封精美的生日祝福邮件......

当你在某个网站注册账号时,往往需要去邮箱里激活验证......

我们今天就来探讨这个技术,邮件的发送。

我们往常发邮件的步骤为:

1.登录邮箱网站或者客户端

2.输入账号、密码

3.填写收件人

4.撰写邮件内容

5.发送

在我们的web项目中,我们要发送邮件给指定用户,步骤为:

1.绑定邮箱服务器

2.验证账号、密码(授权码)

3.建立邮件

4.填写接收者、邮件内容

5.发送

下面我们来实现邮件的发送。


一、简单邮件的发送

1.在pom.xml中添加mail依赖

         <!--添加mail依赖  -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2.在application中配置mail

 #配置邮箱
spring:
mail:
host: smtp.qq.com
username: 1373572467@qq.com
password: 邮箱密码(qq邮箱填写授权码)
default-encoding: UTF-8 #配置邮件发送人
mail:
from:
addr: 1373572467@qq.com

3.定义邮件发送业务接口:

 package com.xm.service;

 /**
* 邮件发送业务
* @author xm
*
*/
public interface EmailService { /**
* 发送简单邮件
* @param to :收件人
* @param subject : 标题
* @param content :邮件内容
*/
void sendSimpleEmail(String to,String subject,String content); }

4.实现邮件发送业务:

 package com.xm.service.impl;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service; import com.xm.service.EmailService;
/**
* 邮件发送业务实现
* @author xm
*
*/
@Service
public class EmailServiceImpl implements EmailService { //获取发送者信息
@Value("${mail.from.addr}")
private String from; //定义邮件发送者
@Autowired
private JavaMailSender sender; @Override
public void sendSimpleEmail(String to, String subject,String content) {
//定义简单邮件
SimpleMailMessage message = new SimpleMailMessage();
//把发送者、收件人、标题、邮件内容封装入简单邮件中
System.out.println("from: "+from+",to:"+to+",subject:"+subject);
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
//交给邮件发送者进行转发
sender.send(message);
System.out.println("发送");
} }

5.定义邮件测试类:

 package com.xm;

 import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import com.xm.service.EmailService; @RunWith(SpringRunner.class)
@SpringBootTest
public class EmailTest { @Autowired
private EmailService emailService; @Test
/**
* 测试简单邮件的发送
*/
public void sendSimpleMassage() {
emailService.sendSimpleEmail("1373572467@qq.com", "122", "Hello Mail!");
} }

6.运行结果截图:

二、带附件的邮件发送

1.定义发送带附件的邮件接口:

      /**
* 发送带附件的邮件
* @param to:收件人
* @param subject : 标题
* @param content:邮件内容
* @param attachment:附件
*/
void sendAttachmentEmail(String to,String subject,String content,File attachment);

2.实现此业务:

   @Override
public void sendAttachmentEmail(String to, String subject, String content, File attachment) {
//创建多用途互联网邮件
MimeMessage message = sender.createMimeMessage(); try {
//封装多用途互联网邮件
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content);
helper.addAttachment("附件", attachment);
} catch (Exception e) {
e.printStackTrace();
}
sender.send(message);
}

3.定义测试:

   @Test
/**
* 多用途互联网邮件
*/
public void sendAttachmentEmail() {
File attachment = new File("src/main/resources/static/1.txt");
emailService.sendAttachmentEmail("1373572467@qq.com", "122", "Hello Mail!",attachment);
}

4.运行结果截图:


                                        2018-07-16

最新文章

  1. WPF 设置透明度和圆形图片
  2. Lua截取utf-8编码的中英文混合字符串
  3. Laravel 5 基础(四)- Blade 简介
  4. spring源码分析构建
  5. C# 基础知识 (三).主子对话框数值传递
  6. Buy the Ticket(卡特兰数+递推高精度)
  7. python入门(Python和Pycharm安装)
  8. sql server 生成数据库字典 sql语句
  9. HTML5 本地缓存 window.localStorage
  10. [TensorFlow]TensorFlow安装方法
  11. 使用gitlab, jenkins搭建CI(持续集成)系统(3) -- 根据不同触发条件执行不同的构建任务
  12. z-index的堆叠规则
  13. 回想sql语句中的各种连接
  14. HTML鼠标悬浮显示隐藏 JS方法
  15. OAF_OAF编译代码至应用详解(案例)
  16. jee-oxygen版eclipse的安装与卸载 maven配置
  17. 04.CSS动画示例--&gt;烟花
  18. MySQL----MySQL数据库入门----第五章 多表操作
  19. ubuntu 16.04 镜像下载
  20. STL : List使用时应注意的问题

热门文章

  1. tomcat入门(一)几种常见的使用tomcat部署项目的方式
  2. 树形dp学习
  3. Redis Intro - Dict
  4. container_of 和 offsetof 宏详解
  5. IE浏览器兼容性问题解决方案
  6. mathjax符号
  7. Actor的一生
  8. sass随笔
  9. cf1059D. Nature Reserve(三分)
  10. 通过adb获取应用的Activity堆栈信息