【Java】SpringBoot邮件发送实现

Springboot3 邮件发送

哔哩哔哩 萌狼蓝天

微信公众号 萌狼蓝天

依赖

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

配置

这里我用的是网易免费企业邮箱。参数请不要照抄,改成适合自己的。

# 邮件配置
# 配置邮件服务器的地址
spring.mail.host=smtp.ym.163.com
# 配置邮件服务器的端口(465或587)
spring.mail.port=465
# 配置用户的账号
spring.mail.username=账号
spring.mail.nickname=显示的名称
# 配置用户的密码(即上面我们申请到的授权码)
spring.mail.password=密码
# 配置默认编码
spring.mail.default-encoding=UTF-8
# SSL 连接配置
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
# 开启 debug,这样方便开发者查看邮件发送日志
spring.mail.properties.mail.debug=true

接口

package com.xrilang.whynotflyserve.emailsent;

import org.springframework.stereotype.Service;

@Service
public interface EmailService {
    void emailSend(String to,String subject,String content);
}

接口具体实现类

package com.xrilang.whynotflyserve.emailsent;
import com.xrilang.whynotflyserve.base.KeyBase64;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

/**
 * 邮件发送的相关设置
 */
@Component
public class EmailServeSetting implements EmailService{
    @Autowired
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String from;
    @Value("${spring.mail.nickname}")
    private String nickname;
    @Override
    public void emailSend(String to,String subject,String content) {
        System.out.println("1:"+nickname);
        nickname = KeyBase64.strToBase64(nickname);
        System.out.println("2:"+nickname);
        nickname = "=?UTF-8?B?"+nickname+"?=";
        System.out.println("3:"+nickname);

        try {
            MimeMessage ms = mailSender.createMimeMessage();
            MimeMessageHelper msi = new MimeMessageHelper(ms,true);
            msi.setFrom(nickname+' '+'<'+from+'>');//From 合法性检查  https://service.mail.qq.com/detail/0/995
            msi.setTo(to);
            msi.setSubject(subject);
            msi.setText(content,true);//true开启HTML解析
            mailSender.send(ms);
        }catch (Exception e){
            System.out.println(e);
        }
    }
}

From的合法性问题

https://service.mail.qq.com/detail/0/995

Base64编码

package com.xrilang.whynotflyserve.base;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Base64;
public class KeyBase64 {
    public static String strToBase64(String str){
        try{
            return Base64.getEncoder().encodeToString(str.getBytes());
        }catch (Exception e){
            System.out.println(e);
        }
        return str;
    }
}

@value读取properties配置文件中文乱码的问题

banner.charset=UTF-8
server.tomcat.uri-encoding=UTF-8
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true
spring.messages.encoding=UTF-8
posted @ 2023-09-27 17:37  萌狼蓝天  阅读(113)  评论(0编辑  收藏  举报