springboot发送邮件

1. 依赖

java 复制代码
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2. 在application.yml文件中配置JavaMail

java 复制代码
spring:
   mail:
    # 邮箱服务器
    host: smtp.qq.com
    # 邮箱
    username: xxxxxx@qq.com
    # 授权码,不是登录密码
    password: xxxxxxxx

授权码获取

各种邮箱获取授权码



3. 无附件,简单内容发送

java 复制代码
import com.itheima.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
 
@Service
public class SendMailServiceImpl implements SendMailService {
    @Autowired
    private JavaMailSender javaMailSender;
    //发送人
    private String from = "xxx@163.com";
    //接收人
    private String to = "xxx@qq.com";
    //标题
    private String subject = "测试邮件";
    //正文
    private String context = "测试邮件正文内容";
    @Override
    public void sendMail() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from+"(小甜甜)");
        message.setTo(to);
        message.setSubject(subject);
        message.setText(context);
        javaMailSender.send(message);
    }
}

运行测试

4. 带附件,图片发送

java 复制代码
import com.itheima.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.internet.MimeMessage;
import java.io.File;
 
@Service
public class SendMailServiceImpl2 implements SendMailService {
    @Autowired
    private JavaMailSender javaMailSender;
    //发送人
    private String from = "xxx@163.com";
    //接收人
    private String to = "xxx@qq.com";
    //标题
    private String subject = "测试邮件";
    //正文
    private String context = "<img src='https://img-blog.csdnimg.cn/8774575ed57d48159111f8f505235f89.png?x-oss-process=image/resize,m_fixed,h_300,image/format,png'/><a href='https://www.baidu.com/'>点开有惊喜</a>";
 
    @Override
    public void sendMail() {
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            helper.setFrom(from +"(小甜甜)");
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(context,true);
 
            //添加附件
            File f1 = new File("E:\\guigu\\springbooot\\springboot_23_mail\\target\\classes\\logo.png");
            File f2 = new File("E:\\guigu\\test\\src\\mail\\word1.docx");
 
            helper.addAttachment(f1.getName(),f1);
            helper.addAttachment("最靠谱的培训结构.png",f2);
 
            javaMailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

运行测试

相关推荐
追风林4 分钟前
mac m1 docker本地部署canal 监听mysql的binglog日志
java·docker·mac
芒果披萨18 分钟前
El表达式和JSTL
java·el
duration~1 小时前
Maven随笔
java·maven
zmgst1 小时前
canal1.1.7使用canal-adapter进行mysql同步数据
java·数据库·mysql
跃ZHD1 小时前
前后端分离,Jackson,Long精度丢失
java
blammmp2 小时前
Java:数据结构-枚举
java·开发语言·数据结构
暗黑起源喵2 小时前
设计模式-工厂设计模式
java·开发语言·设计模式
WaaTong2 小时前
Java反射
java·开发语言·反射
九圣残炎2 小时前
【从零开始的LeetCode-算法】1456. 定长子串中元音的最大数目
java·算法·leetcode
wclass-zhengge3 小时前
Netty篇(入门编程)
java·linux·服务器