1、pom.xml添加 spring-boot-starter-mail 依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2、application.properties中添加发送邮件的配置

spring.mail.host=smtp.163.com
spring.mail.port=25
spring.mail.username=发送邮件的账号@163.com
spring.mail.password=授权码
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true mail.fromMail.addr=发送邮件的账号@163.com

3、发送邮件的接口

package com.st.service;

public interface MailService {

    public void sendSimpleMail(String to, String subject, String content);

}

4、发送邮件的实现类

package com.st.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.Component;
import org.springframework.stereotype.Service; import com.st.service.MailService; @Service
@Component
public class MailServiceImpl implements MailService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired
private JavaMailSender mailSender; @Value("${mail.fromMail.addr}")
private String from; @Override
public void sendSimpleMail(String to, String subject, String content) { SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content); try {
mailSender.send(message);
logger.info("发送成功。。。。。。");
} catch (Exception e) {
logger.error("发送失败!!!!!!", e);
}
} }

5、测试类

package com.st;

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.st.service.MailService; @RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMailApplicationTests { @Autowired
private MailService mailService; //生成6位随机验证码
int randNum = 1 + (int)(Math.random() * ((999999 - 1) + 1)); @Test
public void testSimpleMail() throws Exception {
mailService.sendSimpleMail("接收邮件的账号@qq.com", "开发邮件发送功能", "验证码:"+randNum);
} }

SpringBoot发送简单文本邮件的更多相关文章

  1. SpringBoot 发送简单邮件

    使用SpringBoot 发送简单邮件 1. 在pom.xml中导入依赖 <!--邮件依赖--> <dependency> <groupId>org.springf ...

  2. spring boot发简单文本邮件

    首先要去邮箱打开POP3/SMTP权限: 然后会提供个授权码,用来发送邮件.忘记了,可以点生成授权码再次生成. 1.引入spring boot自带的mail依赖,这里版本用的:<spring-b ...

  3. thunderbird发送纯文本邮件

    向邮件列表中发邮件时,要求邮件格式必须是纯文本格式的,在thunderbird中,邮件格式(plain text或者html格式)在[工具->账户设置->[账户名称]->通讯录]下的 ...

  4. 使用python发送简单的邮件

    from:http://blog.csdn.net/zhaoweikid/article/details/125898 前些时间,论坛上有人讨论怎么用python发送需要认证的邮件,我在我的FreeB ...

  5. (转)JavaMail邮件发送-发送一个文本邮件和一些问题说明

    需要下载的JAR包: JavaMail:http://www.oracle.com/technetwork/java/javamail/index.html JAF:http://www.oracle ...

  6. 使用SpringBoot发送mail邮件

    1.前言 发送邮件应该是网站的必备拓展功能之一,注册验证,忘记密码或者是给用户发送营销信息.正常我们会用JavaMail相关api来写发送邮件的相关代码,但现在springboot提供了一套更简易使用 ...

  7. Python通过yagmail和smtplib模块发送简单邮件

    SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件.python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是pytho ...

  8. springboot发送email邮件

    添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  9. SpringBoot系列—简单的邮件系统

    1. 效果发送效果图 2. 邮件开发准备工作 3. springboot引入mail服务 4. 启动应用,开始4种邮件发送测试 1. 效果发送效果图 连续发送了四封邮件:普通文本邮件,带附件的邮件,内 ...

随机推荐

  1. javascript实现数据结构----栈

    //栈是一种遵从后进先出原则的有序集合. //新添加的或待删除的元素都保存在栈的末尾,称作栈顶,另一端就叫栈底 //在栈里,新元素都靠近栈顶,旧元素都叫做栈底 function Stack(){ va ...

  2. 关于内存 转载自http://blog.csdn.net/xluren/article/details/8150723

    首先感谢下原作者,写的真的非常明白,非常详细 1.预备知识—程序的内存分配 一个由C/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局 ...

  3. R语言入门视频笔记--1

    一.数据框简要 可输入来访问mtcars这个系统自带的数据框中的mpg列 mtcars$mpg 或者输入 mtcars[c("mpg","cyl")] 来访问两 ...

  4. MYsql 锁详解 锁 与索引的关系

    原文:http://blog.csdn.net/xifeijian/article/details/20313977#t10   mysql innodb的锁是通过锁索引来实现的.   select ...

  5. 面试题:oracle数据库行转列的问题

    今天我一个学弟问了一个面试题: 有表A,结构如下:A: p_ID p_Num s_id1 10 011 12 022 8 013 11 013 8 03其中:p_ID为产品ID,p_Num为产品库存量 ...

  6. UNIDAC如何驱动MSSQL2000

    UNIDAC如何驱动MSSQL2000 如下图,PROVIDER必须设置为PRSQL.默认的PRAUTO,如果操作系统是XP可以,如果是WIN7以上的,不可以. 原因是MSSQL NATIVE 11已 ...

  7. 使用SourceTree 来管理 Gitcafe 的Pages 发布Blog!

    有个好爹的 SourceTree 是来自 JIRA 的娘家 Bitbucket 的新东家 ATLASSIAN.com 一家成功的,对敏捷软件工程拥有全栈式支持的商业公司, 所推出的 MAC 专用, S ...

  8. jmeter Plugins Manager插件管理

    在 https://jmeter-plugins.org/downloads/all/ 下载插件,放到lib/ext Download plugins-manager.jar and put it i ...

  9. php-cpp用来开发php的拓展

    官网: http://www.php-cpp.com/documentation/install, 找时间要学一下.

  10. Netty3 源代码分析 - NIO server绑定过程分析

    Netty3 源代码分析 - NIO server绑定过程分析      一个框架封装的越好,越利于我们高速的coding.可是却掩盖了非常多的细节和原理.可是源代码可以揭示一切. 服务器端代码在指定 ...