【问题标题】:How to send an email to multiple recipients in Spring如何在 Spring 中向多个收件人发送电子邮件
【发布时间】:2015-01-08 03:06:28
【问题描述】:

电子邮件仅发送到String[] to 数组中的最后一个电子邮件地址。我打算发送到添加到数组中的所有电子邮件地址。我怎样才能做到这一点?

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

【问题讨论】:

  • 假设调用了正确的方法(setTo(String[]) 之一)它应该可以正常工作。我不明白为什么您要在此方法中设置会话属性和主机,这应该在配置中。

标签: java spring email


【解决方案1】:

您可以选择使用以下 4 种方法。我提供了在这种情况下有用的两种方法的示例。我从下面的评论员那里整合了这些信息。

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

【讨论】:

  • 第二种方法不起作用(在字符串中使用逗号分隔的值)。
  • @naXa 你为什么这么认为?
  • 我刚刚测试过了。我得到AddressException: Illegal address
  • @naXa 原因不能是to 中的逗号分隔值问题一定是其他原因
  • @Downvoter。感谢您在没有解释的情况下投反对票。
【解决方案2】:

更好的方法是创建一个包含多个收件人地址的数组。

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

【讨论】:

  • 你好。添加字符串数组是否适用于向多个收件人发送邮件?当我添加这行代码时,我看到了合成错误“意外令牌”
【解决方案3】:

在 String[] 数组中添加所有电子邮件 ID

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

【讨论】:

    【解决方案4】:

    就这样试试吧。

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

    【讨论】:

      【解决方案5】:

      你可以试试这个,而不是

      helper.setTo(to);
      
      String multipleEmailIds = "abc@abc.com, abc@abc.com"
      mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(multipleEmailIds ));
      

      【讨论】:

        【解决方案6】:

        我认为更好的方法是在 spring.xml 文件中将“to”属性声明为数组,传递值并使用 Deinum 在评论中建议的方法setTo(string[])。流程在 xml 文件中定义为 'to'

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

        现在为这个包含多个收件人地址的数组生成getter setter方法并将其传递给setTo(String[])方法:-

        helper.setTo(to);
        

        【讨论】:

          【解决方案7】:

          它与 SimpleMailMessage 配合得很好。 喜欢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>
            

            设置mail.smtp.sendpartial 为真。我相信它对你有用

            【讨论】:

              猜你喜欢
              • 2019-03-01
              • 2012-05-18
              • 2019-04-22
              • 2013-11-10
              • 1970-01-01
              • 1970-01-01
              • 2017-12-05
              相关资源
              最近更新 更多