本文介绍如何用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成神之——发送邮件的更多相关文章

  1. springboot成神之——ioc容器(依赖注入)

    springboot成神之--ioc容器(依赖注入) spring的ioc功能 文件目录结构 lang Chinese English GreetingService MyRepository MyC ...

  2. springboot成神之——springboot入门使用

    springboot创建webservice访问mysql(使用maven) 安装 起步 spring常用命令 spring常见注释 springboot入门级使用 配置你的pom.xml文件 配置文 ...

  3. springboot成神之——mybatis和mybatis-generator

    项目结构 依赖 generator配置文件 properties配置 生成文件 使用Example 本文讲解如何在spring-boot中使用mybatis和mybatis-generator自动生成 ...

  4. springboot成神之——swagger文档自动生成工具

    本文讲解如何在spring-boot中使用swagger文档自动生成工具 目录结构 说明 依赖 SwaggerConfig 开启api界面 JSR 303注释信息 Swagger核心注释 User T ...

  5. springboot成神之——log4j2的使用

    本文介绍如何在spring-boot中使用log4j2 说明 依赖 日志记录语句 log4j2配置文件 本文介绍如何在spring-boot中使用log4j2 说明 log4j2本身使用是非常简单的, ...

  6. springboot成神之——mybatis在spring-boot中使用的几种方式

    本文介绍mybatis在spring-boot中使用的几种方式 项目结构 依赖 WebConfig DemoApplication 方式一--@Select User DemoApplication ...

  7. springboot成神之——application.properties所有可用属性

    application.properties所有可用属性 # =================================================================== # ...

  8. springboot成神之——springboot+mybatis+mysql搭建项目简明demo

    springboot+mybatis+mysql搭建项目简明demo 项目所需目录结构 pom.xml文件配置 application.properties文件配置 MyApplication.jav ...

  9. springboot成神之——websocket发送和请求消息

    本文介绍如何使用websocket发送和请求消息 项目目录 依赖 DemoApplication MessageModel WebConfig WebSocketConfig HttpHandshak ...

随机推荐

  1. appium自动化测试(三)

    一. 层级定位和list 先通过find_element_by_XXX找到父级元素webelement,再通过webelement.find_element_by_XXX寻找子元素 二. 滑动屏幕 滑 ...

  2. CodeForces - 197D

    开场连wa三发A题,差点心态崩了,还好坚持打完了,一共A了三题 A题,判断能不能放第一个圆,能放的话,先手比赢 #include<map> #include<set> #inc ...

  3. Four-operations: 使用node.js实现四则运算程序

    一. 项目基本信息 项目成员: 陈旭钦, 郭鹏燕 项目仓库: https://github.com/Yanzery/Four-operations 二. PSP2.1表格 PSP2.1 Persona ...

  4. TI IPNC Web网页之GoDB开发环境

    介绍 下面介绍DM8127/DM385 IPNC RDK中网页制作相关的东东. 具体来说,各位获得这个RDK包时有以下文件: IPNC_RDK_DM812x_DM385_Version3.5.0.ta ...

  5. *Fragment与Activity生命周期的协调

    本文出处:http://blog.csdn.net/think_soft/article/details/7272869 拥有Fragment的Activity的生命周期直接影响了其中的Fragmen ...

  6. ng 实现插入和删除

    结果: 代码: <!DOCTYPE html> <html ng-app="myApp"> <head lang="en"> ...

  7. (四)js数组方法一

    ES5数组方法: Array.prototype.filter()   对数组元素进行过滤 三个参数:元素值,下标,原数组 返回:过滤后符合条件的数组,不会改变原数组 let arr = [2,4,6 ...

  8. Biology(湖南集训)

    题目大意:n个字符串,m个操作,可以插入字符串,也可以询问某T个字符串的最长后缀 题解:Trie+lca Trie树的插入与查询操作.把字符串反转就相当于求公共前缀. lca的深度就是公共前缀的长度. ...

  9. Ubantu中安装sublime

    Sublime-text-3的安装步骤   添加Sublime-text-3软件包的软件源 sudo add-apt-repository ppa:webupd8team/sublime-text-3 ...

  10. AzureStack混合云大数据解决方案

    AzureStack是Azure的私有云解决方案.AzureStack可以帮助用户实现混合云的部署模式. 本文将介绍混合云的模式下,Azure作为计算资源,AzureStack作为存储资源.如下图: ...