(五)SpringBoot 配置邮件Mail

1. 添加依赖

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

2.yml文件添加mail配置

 mail:
    host: 
    username: 
    password: 
    default-encoding: utf-8

3.


@Service
public class MailServiceImpl implements MailService {
    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender sender;

    /**
     * @Description //TODO 发送简单邮件
    
     * @return void
     **/
    public void sendEmail(String[] to, String title, String content){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from); //发送者
        message.setTo(to); //接受者
        message.setSubject(title); //发送标题
        message.setText(content);  //发送内容
        //sender.send(message);

    }
    /**
     * @Description //TODO 发送html格式内容的邮件
     * 
     * @return void
     **/
    @Async //异步调用这个方法
    public void sendInlineMail(String[] to, String title, String content) throws Exception {
        MimeMessage mimeMessage = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(title);
        helper.setText(content, true); //注意:调用setText时需要在第二个参数传入true,这样才会使用HTML格式发送邮件
        sender.send(mimeMessage);
    }
    
    
}

如果使用@Async //异步调用这个方法异步;在启动类添加注解:@EnableAsync

 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值