spring 邮箱简单使用

    技术2022-07-10  128

    yml配置

    mail: default-encoding: utf-8 host: smtp.qq.com username: 邮箱号码 password: 请在邮箱设置中获得对应key protocol: smtp

    具体实现

    import cn.hutool.extra.template.TemplateEngine; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.mail.MailProperties; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; import java.util.Date; @Service public class MailService { @Value("${spring.mail.username}") private String from; @Autowired private JavaMailSender mailSender; public void sendSimpleMail(String to,String title,String content){ SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setSubject(title); message.setText(content); mailSender.send(message); } }

    调用sendSimpleMail方法即可使用,to为发送邮箱地址,title为标题,content为邮箱内容

    Processed: 0.010, SQL: 12