Springboot之发送简单邮件

Springboot之发送简单邮件

添加依赖

在 pom.xml 添加依赖

<!-- springboot 发送邮件 start -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- springboot 发送邮件 end -->

邮件配置

在 application.yml 中配置

spring:
  #springboot发送邮件的配置
  mail:
      host: smtp.qq.com(邮箱服务器地址)
      username: 邮箱地址
      password: 邮箱密码
      properties:
          mail:
              smtp:
                  auth: true
                  timeout: 25000

代码

package top.simba1949.service.impl;

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.Service;

/**
 * @author simba@onlying.cn
 * @date 2018/7/6 20:00
 */
@Service
public class EmailServiceImpl {
    /**
     * 获取配置文件中邮箱地址
     */
    @Value("${spring.mail.username}")
    private String emailFrom;

    @Autowired
    private JavaMailSender javaMailSender;

    public void sendSimpleMail(String sendTo, String title, String content) {
        // 创建邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        // 发送邮件地址
        message.setFrom(emailFrom);
        // 接收邮件地址
        message.setTo(sendTo);
        // 邮件主题
        message.setSubject(title);
        // 邮件内容
        message.setText(content);

        // 发送邮件
        javaMailSender.send(message);
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Simba1949

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值