SpringBoot配置发送邮件

1 篇文章 0 订阅
0 篇文章 0 订阅

Java代码

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;

@Component
public class EmailUtil {

    @Autowired
    private JavaMailSender sender;

    @Value("${spring.mail.username}")
    private String from; // 发件人

    @Value("${spring.mail.to}")
    private String toStr; // 收件人

    @Value("${spring.mail.cc}")
    private String ccStr; // 抄送人

    /**
     * 发送纯文本邮件
     * @param subject 标题
     * @param content 内容
     */
    public void sendSimpleMail(String subject, String content) {

        String[] to = toStr.split(",");
        String[] cc = ccStr == null || "".equals(ccStr) ? null : ccStr.split(",");

        SimpleMailMessage message = new SimpleMailMessage();

        message.setFrom(from);
        message.setTo(to);
        if (cc != null && cc.length > 0) {
            message.setCc(cc);
        }
        message.setSubject(subject);
        message.setText(content);

        sender.send(message);

    }

    /**
     * 发送带html的邮件
     * @param subject 标题
     * @param text html片段
     * @param imgPath html片段中图片路径
     */
    public void senderHtmlMail(String subject, String text, String imgPath) throws MessagingException {

        String[] to = toStr.split(",");
        String[] cc = ccStr == null || "".equals(ccStr) ? null : ccStr.split(",");

        MimeMessage msg = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg, true);

        helper.setFrom(from);
        helper.setTo(to);
        if (cc != null && cc.length > 0) {
            helper.setCc(cc);
        }
        helper.setSubject(subject);
        helper.setSentDate(new Date());
        helper.setText(text, true);

        if (imgPath != null && !"".equals(imgPath)) {
            FileSystemResource res = new FileSystemResource(new File(imgPath));
            helper.addInline("imagePicId", res);
        }

        sender.send(msg);

    }
}
html模版(使用freemark)

引入依赖

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

Java代码

// 邮件html拼接
Map<String, Object> model = new HashMap<>();
model.put("timeSpanStr", year + "年 第" + weekNum + "周 " + timeSpan);
//读取 html 模板
Template template = freeMarkerConfigurer.getConfiguration().getTemplate("mail.html");
String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model); // html片段
EmailUtil.senderHtmlMail(... ... // 调用发送邮件方法
配置文件
#邮件配置
spring.mail.host= smtp.126.comspring.mail.username= elf***@126.com
spring.mail.password= *** ***
spring.mail.default-encoding= UTF-8
spring.mail.port= 25
spring.mail.to= elf***@126.com
spring.mail.cc=

注意:部分邮箱需要设置开启POP3/SMTP授权服务,在邮箱设置中进行设置

部分SpringBoot的jar中已整合mail,如未引入jar需在pom.xml中引入依赖

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



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Spring Boot发送邮件配置方法: 1.在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 2.在application.properties或application.yml文件中添加邮件配置信息,例如: ```properties # 邮件服务器主机名 spring.mail.host=smtp.qq.com # 邮件服务器端口号,默认为25 spring.mail.port=465 # 邮件发送者用户名 spring.mail.username=xxx@qq.com # 邮件发送者密码或授权码 spring.mail.password=ooo # 邮件发送者显示名称 spring.mail.properties.mail.smtp.from=xxx@qq.com # 邮件编码格式 spring.mail.default-encoding=UTF-8 # 是否启用SSL spring.mail.properties.mail.smtp.ssl.enable=true ``` 3.在Java代码中使用JavaMailSender发送邮件,例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Component; @Component public class MailService { @Autowired private JavaMailSender mailSender; public void sendSimpleMail(String to, String subject, String content) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("xxx@qq.com"); message.setTo(to); message.setSubject(subject); message.setText(content); mailSender.send(message); } } ``` 以上代码中,我们使用@Autowired注解注入了JavaMailSender对象,然后使用SimpleMailMessage对象设置邮件的发送者、接收者、主题和内容,最后调用mailSender.send()方法发送邮件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值