How to send an email to multiple recipients in Spring

旧街凉风 提交于 2021-02-06 14:59:27

问题


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);
}

回答1:


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"});




回答2:


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

    MimeMessageHelper helper = new MimeMessageHelper( message, true );
    helper.setTo( String[] to );



回答3:


just try like this.

helper.setTo(InternetAddress.parse("email1@test.com,email2@test.com")) 



回答4:


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);



回答5:


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 ));



回答6:


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);



回答7:


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) {
     //...
}   



回答8:


<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



来源:https://stackoverflow.com/questions/26867055/how-to-send-an-email-to-multiple-recipients-in-spring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!