spring-boot-mail使用多个发件人

不废话,直接上代码

思路:  继承JavaMailSenderImpl, 通过配置类,  创建bean, 因为后续可能会持续增加发件人,  所以直接将发件人信息配置在数据库, 然后项目启动时查询所有发件人信息, 循环创建bean,

根据类型自动注入,  因为有多个所以使用List<EMaiFormIMainServiceImpl> 接收

然后将LIst 转Map key= 发件人邮箱, value= 当前对象

使用时, 根据发件人邮箱,取出对应的bean实例

调用实例的.send((MimeMessage mimeMessage) -> {

MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, Boolean.TRUE, StandardCharsets.UTF_8.name());  messageHelper.setFrom("发件人邮箱");

);

public class EMaiFormIMainServiceImpl extends JavaMailSenderImpl {



    private static final Properties p = new Properties();

    static {
        p.setProperty("mail.smtp.auth", "true");
        p.setProperty("mail.smtp.starttls.enable", "false");
        p.setProperty("mail.smtp.starttls.required", "false");
    }

    public EMaiFormIMainServiceImpl(String userName,String password, String host, Integer port, String  defaultEncoding) {

        //发件人邮箱
        super.setUsername(userName);
    
        super.setPassword(password);
        //服务器地址
        super.setHost(host);
        //端口
        super.setPort(port);
        super.setDefaultEncoding("UTF-8");


        super.setJavaMailProperties(p);
    }

@Configuration
public class EmailConfig {

    @Autowired
    private 发件人Repository repository;

    @Bean
    public List<EMaiFormIMainServiceImpl> getEMaiFormIMainServiceImplList() {
         List<发件人> list= repository.selectAll();
       return list.stream().map(e->{
           return new EMaiFormIMainServiceImpl(e.getUserName(),e.getPassword(),e.getHost(),e.getPort(),"UTF-8");
        }).collect(Collectors.toList());

    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值