本文介绍如何用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. Html工具类

    import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServletRespons ...

  2. opencv:访问像素

    a.使用指针 #include <opencv.hpp> using namespace cv; using namespace std; int main() { //指针访问每个像素并 ...

  3. 《转》浅谈EJB

    ejb一直是一个让我很纠结的技术,虽然ejb作为sun推荐的最佳实践,在sun的J2EE教程中,推荐jsp和servlet作为view层,ejb作为业务逻辑层. 上述就是J2EE教程讲J2EE体系中J ...

  4. js 复制粘贴功能记录

    最近工作中需要在前端页面中使用代码完成剪贴板的读写,网上搜索了下相应的资料,记录下... 这个功能有两个办法一个是js方式,一个是使用flash 一.JS方法 1.复制 首先复制的过程分为两步曲,无论 ...

  5. Metasploit的基本使用

    Metasploit可以在Linux.Windows和Mac OS X系统上运行.我假设你已安装了Metasploit,或者你使用的系统是Kali Linux.它有命令行接口也有GUI接口. 我使用的 ...

  6. 【转】程序员应该了解的——除了coding我们还有很多事要做

    from : http://www.cnblogs.com/lingyun1120/archive/2011/10/09/2203306.html try { if (you.believe(it) ...

  7. LUAROCKS 报错解决办法

    用luarocks 加载包时报错 Warning: falling back to curl - install luasec to get native HTTPS support 此时先安装 ./ ...

  8. Java 三大特征之--多态

    http://www.cnblogs.com/chenssy/p/3372798.html

  9. Scrapy库安装和项目创建

    Scrapy是一个流行的网络爬虫框架,从现在起将陆续记录Python3.6下Scrapy整个学习过程,方便后续补充和学习.本文主要介绍scrapy安装.项目创建和测试基本命令操作 scrapy库安装 ...

  10. 定时框架quartz的一些问题总结

    1 什么是Quartz Quartz是OpenSymphony开源组织在Job scheduling领域的开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单 ...