33

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 work?

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 + "<html><body><h1>hi welcome</h1><body></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);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Program-Me-Rev
  • 6,184
  • 18
  • 58
  • 142
  • 1
    Assuming that the correct method is invoked (the `setTo(String[])` one) it should just work. What I don't get is why you are setting the session properties and host in this method, that should be in the configuration. – M. Deinum Nov 11 '14 at 14:33

9 Answers9

47

You have the choice to use the following 4 methods. I have provided examples of the two methods useful in this case. I have consolidated this information from the commentators below.

helper.setTo(InternetAddress.parse("email1@test.com,email2@test.com"))
helper.setTo(new String[]{"email1@test.com", "email2@test.com"});

From the Javadoc:

enter image description here

xlm
  • 6,854
  • 14
  • 53
  • 55
Jens
  • 67,715
  • 15
  • 98
  • 113
11

The better approach is to create an array containing the address of multiple recipients.

    MimeMessageHelper helper = new MimeMessageHelper( message, true );
    helper.setTo( String[] to );
Surya Nair
  • 111
  • 1
  • 3
  • Hello. Is adding String array working for sending mail to multiple recipients? When I added this line of code I see synthax error "unexpected token" – Orkhan Hasanli Nov 23 '18 at 23:11
6

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);
Sri
  • 71
  • 1
  • 3
5

just try like this.

helper.setTo(InternetAddress.parse("email1@test.com,email2@test.com")) 
Sree
  • 921
  • 2
  • 12
  • 31
2

You can try this, instead of

helper.setTo(to);

String multipleEmailIds = "abc@abc.com, abc@abc.com"
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(multipleEmailIds ));
Jens
  • 67,715
  • 15
  • 98
  • 113
Sreenath S
  • 669
  • 6
  • 9
0

I think better approach is to declare "to" attribute as array in spring.xml file , pass values and use method setTo(string[]) as suggested by Deinum in comment. Process is define 'to' in xml file as

<property name="to">
    <array>
    <value>abc@gmail.com</value>
    <value>xyz@gmail.com</value>
    </array>
</property>

Now generate getter setter method for this array containing address of multiple recipient and pass it to setTo(String[]) method as :-

helper.setTo(to);
Naved Ali
  • 616
  • 1
  • 14
  • 31
0

It's working well with the SimpleMailMessage. like the answer of Siri

String[] to = {"user1@gmail.com", "user2@gmail.com"};
        
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setTo(to);
simpleMailMessage.setSubject("subject of mail");
simpleMailMessage.setText("content of mail");

try {
    javaMailSender.send(simpleMailMessage);
} catch (MailSendException e) {
     //...
}   
Yehouda
  • 112
  • 1
  • 6
0

I had the same issue, I used this and it worked for me. Set your receiptants in a String like receitants = "xxx@gmail, xqz@gmail.com";

MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);

helper.setSubject(subject);
helper.setFrom(sender);
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receipants));
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccrecepients));

helper.setText(body, true);
javaMailSender.send(message);
-3
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
      <property name="host" value="smtp.ccc.corp"/>
      <property name="port" value="25"/>
      <property name="javaMailProperties"><props>
        <prop key="mail.smtp.sendpartial">true</prop>
      </props></property>
</bean>

set mail.smtp.sendpartial true. I am sure its work for you

Saeid
  • 4,147
  • 7
  • 27
  • 43