Springboot群发和单发邮件:JavaMailSenderImpl和JavaMailSender

话不多说,直接上代码:

1:pom.xml 引入jar

    <dependency> 

        <groupId>org.springframework.boot</groupId> 

         <artifactId>spring-boot-starter-mail</artifactId>

      </dependency>

2:在application.properties中设置邮箱的信息(126邮箱),springboot会自动读取;也可以保存到数据库中,使用时候读取,稍后讲解。需要给邮箱开启授权登录,不了解的可以百度一下,这里就不在展开

spring.mail.default-encoding=UTF-8  
spring.mail.host=smtp.126.com  
spring.mail.password=授权密码(不是登录邮箱的密码)
spring.mail.port=25  
spring.mail.protocol=smtp  

spring.mail.username=用户名

3:邮箱发送Controller(简单的demo,就不写service了),发送给一个用户

@RestController 
@RequestMapping("mail")  
public class Controller {

@Autowired  
    JavaMailSender jms;  //这是个接口,从配置文件里面读取邮箱设置的相关信息,值发送给一个用户
      
    @GetMapping("/send")  
    public void send(){  
        //建立邮件消息  
        SimpleMailMessage mainMessage = new SimpleMailMessage();  
        //发送者  
        mainMessage.setFrom("xxxxxx");  
        //接收者  
        mainMessage.setTo("xxxxxx");  
        //发送的标题  
        mainMessage.setSubject("嗨喽");  
        //发送的内容  
        mainMessage.setText("hello world");  
        jms.send(mainMessage); 
    }  

}

4:群发(发送给多个用户),这里需要用到JavaMailSender 接口的实现类JavaMailSenderImpl,实现类不会从配置文件里面读取信息,需要自己设置。在springboot context中,也没有这个bean实例,如果需要通过注入的方式,需要自己配置这个bean实例,也可以不用注入的方式直接new一个实例。

@GetMapping("/mail")
public void sendSimpleMail() throws Exception {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();//直接生产一个实例
String users[] = {"xxxxx@qq.com","xxxxx@126.com"};
mailSender.setHost("smtp.126.com");
mailSender.setPassword("xxxxx");
mailSender.setPort(25);
mailSender.setProtocol("smtp");
mailSender.setUsername("xxxxx");
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("xxxxxxx");
message.setTo(users); // 群发
message.setSubject("羽毛球比赛");
message.setText("报名参加羽毛球赛");
mailSender.send(message);

}

Notes:

    1:发送邮件可能会触发发垃圾邮件规则,所以在接受不能邮件时,可以修改主题和内容,或者换个账号.

    2:注意配置文件里面邮件信息上,不能有空格(启动发生错误时应检查)


  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
以下是使用Spring Boot发送邮件的步骤: 1. 导入Spring Boot提供的依赖[^1]: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 2. 在配置文件中设置邮箱信息: ```properties spring.mail.host=smtp.qq.com spring.mail.port=587 spring.mail.user[email protected] spring.mail.password=your-email-password spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` 3. 创建一个邮件服务类,用于发送邮件[^2]: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; @Service public class EmailService { @Autowired private JavaMailSender mailSender; public void sendEmail(String to, String subject, String text) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(text); mailSender.send(message); } } ``` 4. 在需要发送邮件的地方调用邮件服务类的方法发送邮件: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class EmailController { @Autowired private EmailService emailService; @GetMapping("/sendEmail") public String sendEmail() { String to = "[email protected]"; String subject = "Test Email"; String text = "This is a test email."; emailService.sendEmail(to, subject, text); return "Email sent successfully."; } } ``` 以上是使用Spring Boot发送邮件的简单示例。你可以根据自己的需求进行相应的配置和调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值