springboot成神之——发送邮件
本文介绍如何用spring发送邮件
目录结构

依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
MailConfig
package com.springlearn.learn.config;
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
@Configuration
public class MailConfig {
@Bean
public JavaMailSender getjavamailsender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.qq.com");
mailSender.setPort(587);
mailSender.setUsername("你的qq邮箱");
mailSender.setPassword("你的授权码");
Properties props = mailSender.getJavaMailProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.from", "你的qq邮箱");
return mailSender;
}
}
TestController
package com.springlearn.learn.controller;
import java.io.File;
import java.io.IOException;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.http.HttpServletResponse;
import com.springlearn.learn.DemoApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
public JavaMailSender emailSender;
@ResponseBody
@RequestMapping(value = "/sendmail", method = RequestMethod.GET)
public String Test(HttpServletResponse response) throws IOException, MessagingException {
// 发送简单短信
// SimpleMailMessage message = new SimpleMailMessage();
// message.setTo("756029960@qq.com");
// message.setSubject("测试邮件");
// message.setText("发送成功");
// this.emailSender.send(message);
// 发送附件
// MimeMessage message = emailSender.createMimeMessage();
// MimeMessageHelper helper = new MimeMessageHelper(message, true,"UTF-8");
// helper.setTo("你要发送人的邮件");
// helper.setSubject("测试邮件,有附件!");
// helper.setText("给你发一份附件!", true);
// System.out.println();
// FileSystemResource file1 = new FileSystemResource(new File(DemoApplication.class.getResource("").getPath(), "demo.txt"));
// helper.addAttachment("Txt1 file", file1);
// FileSystemResource file2 = new FileSystemResource(new File(DemoApplication.class.getResource("").getPath(), "demo1.txt"));
// helper.addAttachment("Txt1 file", file2);
// emailSender.send(message);
return "Ok!";
}
}
测试
http://localhost:9001/sendmail
springboot成神之——发送邮件的更多相关文章
- springboot成神之——ioc容器(依赖注入)
springboot成神之--ioc容器(依赖注入) spring的ioc功能 文件目录结构 lang Chinese English GreetingService MyRepository MyC ...
- springboot成神之——springboot入门使用
springboot创建webservice访问mysql(使用maven) 安装 起步 spring常用命令 spring常见注释 springboot入门级使用 配置你的pom.xml文件 配置文 ...
- springboot成神之——mybatis和mybatis-generator
项目结构 依赖 generator配置文件 properties配置 生成文件 使用Example 本文讲解如何在spring-boot中使用mybatis和mybatis-generator自动生成 ...
- springboot成神之——swagger文档自动生成工具
本文讲解如何在spring-boot中使用swagger文档自动生成工具 目录结构 说明 依赖 SwaggerConfig 开启api界面 JSR 303注释信息 Swagger核心注释 User T ...
- springboot成神之——log4j2的使用
本文介绍如何在spring-boot中使用log4j2 说明 依赖 日志记录语句 log4j2配置文件 本文介绍如何在spring-boot中使用log4j2 说明 log4j2本身使用是非常简单的, ...
- springboot成神之——mybatis在spring-boot中使用的几种方式
本文介绍mybatis在spring-boot中使用的几种方式 项目结构 依赖 WebConfig DemoApplication 方式一--@Select User DemoApplication ...
- springboot成神之——application.properties所有可用属性
application.properties所有可用属性 # =================================================================== # ...
- springboot成神之——springboot+mybatis+mysql搭建项目简明demo
springboot+mybatis+mysql搭建项目简明demo 项目所需目录结构 pom.xml文件配置 application.properties文件配置 MyApplication.jav ...
- springboot成神之——websocket发送和请求消息
本文介绍如何使用websocket发送和请求消息 项目目录 依赖 DemoApplication MessageModel WebConfig WebSocketConfig HttpHandshak ...
随机推荐
- Appium移动自动化测试(三)--自动化测试demo
使用java语言编写并实现自动化测试实例 前面我已经搭建好了appium所需要的环境,本文我以系统自带的计算器app为测试对象,进行学习. 一.将测试机与PC连接 本人使用的是Android模拟器,测 ...
- GAN作用——在我做安全的看来,就是做数据拟合、数据增强
from:https://www.zhihu.com/question/56171002/answer/155777359 GAN的作用,也就是为什么GAN会火了(有部分原因可能是因为Lecun的赞赏 ...
- 转: OLAP引擎——Kylin介绍
本文转自:http://blog.csdn.net/yu616568/article/details/48103415 ,如有侵犯,立刻删除. Kylin是ebay开发的一套OLAP系统,与Mond ...
- IDL语言开发规范
一.支持的类型 1.IDL支持常见的基本类型,常量,枚举,容器,结构体,服务.不支持多态和重载,参数.返回值不能为空,各个基本类型的标识如下: bool:对应java的boolean,布尔类型(tru ...
- 018对象——对象 get_class get_declared_classes get_declared_interfaces
<?php /** * 对象 get_class get_declared_classes get_declared_interfaces */ //get_class() 获得对象的类名,区分 ...
- 012-对象——魔术常量__CLASS__ __METHOD__ __FUNCTION__ __DIR__ __FILE__
<?php /** *魔术常量__CLASS__ __METHOD__ __FUNCTION__ __DIR__ __FILE__ */ //魔术常量:__CLASS__ 得到类名. /*cla ...
- Ti CC2540蓝牙模块学习笔记整理
接触CC2540几天,终于有了初步的理解,现将笔记整理如下,只是皮毛,如有错误,还请指正,还有好多没闹明白的地方,以后应该还会继续向里面更新~ 一.整体 1.TI的蓝牙平台支持2种协议栈/应用配置:单 ...
- LeetCode OJ:Add Binary(二进制相加)
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- cmd 操作WinService
1.运行--〉cmd:打开cmd命令框 2.在命令行里定位到InstallUtil.exe所在的位置 InstallUtil.exe 默认的安装位置是在C:\Windows\Microsoft.NET ...
- 安装使用lynis扫描Linux的安全漏洞
Lynis是Linux平台上的一款安全漏洞扫描工具.它可以扫描系统的安全漏洞.收集系统信息.安装的软件信息.配置问题.没有设置密码的用户和防火墙等等. Lynis是流行可靠的安全扫描工具. 前不久,L ...