springboot简单模拟邮箱发送

springboot简单模拟邮箱发送

  1. 在pom.xml中添加依赖

    org.springframework.boot
    spring-boot-starter-mail
  2. 然后可以通过springboot提供的工具类进行模拟邮件发送。
    接口:
    public interface MailService {
    /**
    • 发送文本邮件
    • @param to 收件人
    • @param subject 主题
    • @param content 内容
      */
      void sendSimpleMail(String to, String subject, String content);
/**
 * 发送HTML邮件
 * @param to 收件人
 * @param subject 主题
 * @param content 内容
 */
public void sendHtmlMail(String to, String subject, String content);

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

}
3. 具体发送接口实现
@Service
public class MailServiceImpl implements MailService {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

/**
 * Spring Boot 提供了一个发送邮件的简单抽象,使用的是下面这个接口,这里直接注入即可使用
 */
@Autowired
private JavaMailSender mailSender;

/**
 * 配置文件中的邮箱
 */
@Value("${spring.mail.from}")
private String from;

/**
 * 简单文本邮件
 * @param to 收件人
 * @param subject 主题
 * @param content 内容
 */
@Override
public void sendSimpleMail(String to, String subject, String content) {
    //创建SimpleMailMessage对象
    SimpleMailMessage message = new SimpleMailMessage();
    //邮件发送人
    message.setFrom(from);
    //邮件接收人
    message.setTo(to);
    //邮件主题
    message.setSubject(subject);
    //邮件内容
    message.setText(content);
    //发送邮件
    mailSender.send(message);
}

/**
 * html邮件
 * @param to 收件人
 * @param subject 主题
 * @param content 内容
 */
@Override
public void sendHtmlMail(String to, String subject, String content) {
    //获取MimeMessage对象
    MimeMessage message = mailSender.createMimeMessage();
    try{
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        //邮件发送人
        helper.setFrom(from);
        //邮件接收人
        helper.setTo(to);
        //邮件主题
        helper.setSubject(subject);
        //邮件内容,html格式
        helper.setText(content, true);
        //发送
        mailSender.send(message);
        //日志信息
        logger.info("邮件已经发送。");
    }catch (MessagingException e){
        logger.error("发送邮件时发生异常!",e);
    }
}

/**
 * 带附件的邮件
 * @param to 收件人
 * @param subject 主题
 * @param content 内容
 * @param filePath 附件
 */
@Override
public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
    //获取MimeMessage对象
    MimeMessage message = mailSender.createMimeMessage();
    try{
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        //邮件发送人
        helper.setFrom(from);
        //邮件接收人
        helper.setTo(to);
        //邮件主题
        helper.setSubject(subject);
        //邮件内容,html格式
        helper.setText(content, true);
        //附件文件
        FileSystemResource file = new FileSystemResource(new File(filePath));
        //附件名字
        String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
        //添加附件
        helper.addAttachment(fileName, file);
        //发送
        mailSender.send(message);
        //日志信息
        logger.info("邮件已经发送。");
    }catch (MessagingException e){
        logger.error("发送邮件时发生异常!",e);
    }
}

}
4. 对了,记得在spring的核心配置文件中进行相关邮件配置。
#邮件配置
#发送邮件服务器
spring.mail.host=smtp.163.com
#发送邮件的邮箱地址
spring.mail.username=@163.com
#客户端授权码,不是邮箱密码
spring.mail.password=RJUNSTDKWDMYWRLB
#465或994
spring.mail.properties.mail.smtp.port=994
#发送邮件地址,和username一致
spring.mail.from=
@163.com
#可以任意
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.default-encoding=utf-8
5. 可以在main方法或者使用@test注解进行测试。
String to = “****@163.com”;
mailService.sendSimpleMail(to, “预警邮件” ,“主子,系统又炸了,请求紧急救援!”);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值