本文介绍如何用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. python中的mysql操作

    一. 数据库在自动化测试中的应用 存测试数据 有的时候大批量的数据,我们需要存到数据库中,在测试的时候才能用到,测试的时候就从数据库中读取出来.这点是非常重要的! 存测试结果 二. python中的数 ...

  2. C#和Java接口对比

    C#和java的接口有很多类似之处,对于编程约束和设计模式的实现有重要作用.这里记录几个知识点. 1. C#的接口中不能有字段,但Java的接口中允许有static final修饰的字段/域(fiel ...

  3. c#的DateTime.Now详解

    (转自:http://www.cnblogs.com/lida/archive/2011/01/02/1924197.html) //2008年4月24日 System.DateTime.Now.To ...

  4. 使用VS自带的工具分析.NET程序的性能

    (转自:http://www.cnblogs.com/DebugLZQ/archive/2012/07/10/2585245.html) 这篇博文给大家分享的是,如何使用VS自带的性能分析工具来分析我 ...

  5. Android快速开发-选项卡

    介绍 几行代码实现Android选项卡界面,支持标准底部Tab自定义视图选项卡,头部文字选项卡. 底部自定义视图选项卡 先来看看实现下图中的效果我们代码应该怎么写? 实现上图效果只需以下代码: pub ...

  6. MySQL 的中文乱码问题终结

    中文正确 1:保存sql脚本文件 utf8 !!!!! ANSI<-->GBK 2:打开mysql窗口 SET NAMES UTF8; 每次新打开窗口执行指令 3:SOURCE d:/1. ...

  7. 旧书重温:0day2【9】第六章 攻击c++的虚函数

    不知不觉,我们学到了0day2的第六章形形色色的内存攻击技术!其中,这张很多东西都是理论的东西,不过!我们还是要想办法还原下发生的现场! 其中部分文章截图 http://user.qzone.qq.c ...

  8. 在Ajax.ActionLink的OnBegin,onComplete等事件中使用this【解决办法】

    方法就是修改这个文件[jquery.unobtrusive-ajax.js] options.data.push({ name: "X-Requested-With", value ...

  9. MySql必知必会实战练习(五)存储过程

    1. 为什么使用存储过程? (1) 通过把处理封装在容易使用的单元中,简化复杂操作 (2) 为了保证数据的完整性,不要求反复建立一系列的处理操作,所有开发人员和应用程序都使用同一(试验和测试)存储过程 ...

  10. RabbitMQ学习系列二-C#代码发送消息

    RabbitMQ学习系列二:.net 环境下 C#代码使用 RabbitMQ 消息队列 http://www.80iter.com/blog/1437455520862503 上一篇已经讲了Rabbi ...