title: How to send an email to multiple recipients in Spring
body: <p>The email gets sents only to the last email address in the <code>String[] to</code> array. I'm intending to send to all email addresses added to the array. How can I make that work?</p>
  
  <pre><code>public void sendMail(String from, String[] to, String subject, String msg, List attachments) throws MessagingException {
  
      // Creating message  
      sender.setHost("smtp.gmail.com");
      MimeMessage mimeMsg = sender.createMimeMessage();
      MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, true);
      Properties props = new Properties();
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.port", "425");
  
      Session session = Session.getDefaultInstance(props, null);
  
      helper.setFrom(from);
  
      helper.setTo(to);
  
      helper.setSubject(subject);
      helper.setText(msg + "&lt;html&gt;&lt;body&gt;&lt;h1&gt;hi welcome&lt;/h1&gt;&lt;body&gt;&lt;/html", true);
  
      Iterator it = attachments.iterator();
  
      while (it.hasNext()) {
          FileSystemResource file = new FileSystemResource(new File((String) it.next()));
          helper.addAttachment(file.getFilename(), file);
      }
  
      // Sending message  
      sender.send(mimeMsg);
  }
  </code></pre>