SpringBoot发送邮件

本文详细介绍了如何使用SpringBoot发送邮件,包括开启QQ邮箱的授权码,添加Maven依赖,配置邮件服务,定义邮件发送接口及其实现,以及编写测试用例。示例代码展示了发送文本、HTML邮件及带有附件的邮件的完整流程。
摘要由CSDN通过智能技术生成

第一步:QQ邮箱开启邮箱授权码

 

切换到账户选项卡:

 

拉到下方 找到POP3/IMAP/SMTP服务,开启POP3/SMTP服务:

 

 

 

oxdlgmtkqtnbbjdg

第二步:导入依赖
创建SpringBoot项目,导入Maven依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
第三步:配置文件
spring:
  mail:
    host: smtp.qq.com #发送邮件服务器
    username: 369950806@qq.com  #发送邮件的邮箱地址
    password: oxdlgmtkqtnbbjdg   #客户端授权码,不是邮箱密码,这个在qq邮箱设置里面自动生成的
    properties.mail.smtp.port: 465 #端口号465或587
    properties.mail.smtp.starttls.enable: true
    properties.mail.smtp.starttls.required: true
    properties.mail.smtp.ssl.enable: true
    default-encoding: utf-8
第四步:接口
public interface EmailService {

    /**
     * 发送文本邮件
     *
     * @param from    发送人
     * @param to      收件人
     * @param cc      抄送人
     * @param subject 主题
     * @param content 内容
     */
    void sendTextEmail(String from,String to,String cc, String subject, String content);

    /**
     * 发送HTML邮件
     *
     * @param from    发送人
     * @param to      收件人
     * @param subject 主题
     * @param content 内容
     */
    void sendMimeEmail(String from,String to, String subject, String content);

    /**
     * 发送带附件的邮件
     *
     * @param from    发送人
     * @param to       收件人
     * @param subject  主题
     * @param content  内容
     * @param file 附件
     */
    public void sendsAttachmentEmail(String from,String to, String subject, String content, String file);
}

第五步:实现类
@Slf4j
@Service
public class EmailServiceImpl implements EmailService {

    @Resource
    private JavaMailSender sender;

    @Override
    public void sendTextEmail(String from,String to, String cc, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        //发件人
        message.setFrom(from);
        //收件人
        message.setTo(to);
        //抄送人
        message.setCc(cc);
        //邮件主题
        message.setSubject(subject);
        //邮件内容
        message.setText(content);
        //发送邮件
        sender.send(message);
    }

    @Override
    public void sendMimeEmail(String from,String to, String subject, String content) {
        MimeMessage message = sender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            helper.setFrom(from);
            helper.setTo(to);
            message.setSubject(subject);
            helper.setText(content,true);
            sender.send(message);
            log.info("邮件已经发送!");
        } catch (MessagingException e) {
            log.error("发送邮件时发生异常:"+e);
        }
    }

    @Override
    public void sendsAttachmentEmail(String from,String to, String subject, String content, String file) {
        MimeMessage message = sender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content,true);
            //携带附件
            FileSystemResource fsr = new FileSystemResource(file);
            String fileName = file.substring(file.lastIndexOf("/"));
            helper.addAttachment(fileName,fsr);

            sender.send(message);
            log.info("邮件加附件发送成功!");
        } catch (MessagingException e) {
            log.error("发送失败:"+e);
        }
    }

}

第六步:测试代码
@SpringBootTest
class MailDemoApplicationTests {

    @Resource
    private EmailService emailService;

    @Test
    public void sendSimpleEmail(){
        String content = "好好学习,天天向上";
        emailService.sendTextEmail("369950806@qq.com","3433698914@qq.com","hcitlife@hotmail.com","哈哈",content);
    }

    @Test
    public void sendMimeEmail(){
        String content = "<a href='https://blog.csdn.net/lianghecai52171314/'>注册成功,点击链接激活</a>";
        emailService.sendMimeEmail("369950806@qq.com","3433698914@qq.com","激活邮件",content);
    }

    @Test
    public void sendsAttachmentEmail1(){
        emailService.sendsAttachmentEmail("369950806@qq.com","3433698914@qq.com","带附件的邮件","美女","E:/图片/mm01.jpg");
    }

    @Test
    public void sendsAttachmentEmail2(){
        emailService.sendsAttachmentEmail("369950806@qq.com","3433698914@qq.com","带附件的邮件","美女","E:/图片/zbjbxf.mp3");
    }

    @Test
    void fun(){
        File file = new File("E:/图片/mm01.jpg");
        System.out.println(file.getName());
    }

}

原文链接:https://blog.csdn.net/lianghecai52171314/article/details/119810479

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值