SpringBoot 发送邮件开启SSL

SpringBoot 发送邮件默认使用JavaMailSenderImpl实例进行操作

application.yml配置:

spring:
    mail:
      host: smtp.exmail.qq.com
      port: 465
      username: xxxx
      password: xxx
      default-encoding: UTF-8

如果邮件服务器配置了SSL,端口465,则需要自定义配置JavaMailSenderImpl实例


    @Bean
    public JavaMailSenderImpl mailSender(MailProperties mailProperties) {
        JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
        javaMailSenderImpl.setHost(mailProperties.getHost());
        javaMailSenderImpl.setUsername(mailProperties.getUsername());
        javaMailSenderImpl.setPassword(mailProperties.getPassword());
        javaMailSenderImpl.setDefaultEncoding(StandardCharsets.UTF_8.name());
        javaMailSenderImpl.setPort(mailProperties.getPort());
        if (mailProperties.getPort() == 465) {
            Properties properties = new Properties();
            //如果连接25端口以外的端口就必须有这个参数设置,且配置文件中的配置需要手动导入到JavaMailSenderImpl才能在其中使用
            properties.put("mail.smtp.ssl.enable", true);
            javaMailSenderImpl.setJavaMailProperties(properties);
        }
        return javaMailSenderImpl;
    }

好的,下面是使用 Spring Boot 发送邮件的步骤: 1. 在 `pom.xml` 文件添加 Spring Boot 发送邮件所需的依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 2. 在 `application.yml` 文件配置邮件服务器信息,例如: ``` spring: mail: host: smtp.qq.com username: your_email@qq.com password: your_password properties: mail.smtp.auth: true mail.smtp.starttls.enable: true mail.smtp.starttls.required: true mail.smtp.ssl.trust: smtp.qq.com default-encoding: UTF-8 ``` 3. 创建一个邮件服务类,例如: ``` @Service public class MailService { @Autowired private JavaMailSender mailSender; /** * 发送简单邮件 * @param to 收件人邮箱 * @param subject 邮件主题 * @param content 邮件内容 * @throws MessagingException */ public void sendSimpleMail(String to, String subject, String content) throws MessagingException { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("your_email@qq.com"); message.setTo(to); message.setSubject(subject); message.setText(content); mailSender.send(message); } /** * 发送 HTML 邮件 * @param to 收件人邮箱 * @param subject 邮件主题 * @param content 邮件内容 * @throws MessagingException */ public void sendHtmlMail(String to, String subject, String content) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom("your_email@qq.com"); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); mailSender.send(message); } } ``` 4. 在需要发送邮件的地方调用邮件服务类的方法即可。 示例代码: ``` @RestController public class MailController { @Autowired private MailService mailService; @GetMapping("/sendSimpleMail") public String sendSimpleMail() throws MessagingException { mailService.sendSimpleMail("recipient_email@qq.com", "测试邮件", "这是一封测试邮件"); return "发送成功"; } @GetMapping("/sendHtmlMail") public String sendHtmlMail() throws MessagingException { String content = "<html><body><h1>这是一封测试邮件</h1></body></html>"; mailService.sendHtmlMail("recipient_email@qq.com", "测试邮件", content); return "发送成功"; } } ``` 以上就是使用 Spring Boot 发送邮件的步骤,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值