How to send an email to multiple recipients in Spring

前端 未结 8 2218
花落未央
花落未央 2021-02-12 10:48

The email gets sents only to the last email address in the String[] to array. I\'m intending to send to all email addresses added to the array. How can I make that

8条回答
  •  北海茫月
    2021-02-12 11:28

    Add all email ids in a String[] array

    public String[] sendEmailIds() {
    String[] emailIds = new String[4];
    emailIds[0] = "abc@mail.com";
    emailIds[1] = "deg@mail.com";
    emailIds[2] = "sgh@mail.com";
    emailIds[3] = "hht@mail.com";
    return emailIds;
    }
    
    SimpleMailMessage mailMessage = new SimpleMailMessage();
    mailMessage.setTo(sendEmailIds());
    mailMessage.setSubject(subject);
    mailMessage.setText(message);
    mailMessage.setFrom(fromEmailAddress);
    javaMailSender.send(mailMessage);
    

提交回复
热议问题