Java 如何在 Spring 中向多个收件人发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26867055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:26:30  来源:igfitidea点击:

How to send an email to multiple recipients in Spring

javaspringemail

提问by Program-Me-Rev

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

电子邮件仅发送到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);
}

采纳答案by Jens

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.

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

helper.setTo(InternetAddress.parse("[email protected],[email protected]"))
helper.setTo(new String[]{"[email protected]", "[email protected]"});

enter image description here

在此处输入图片说明

回答by mangukiya mayur

<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.sendpartialtrue. I am sure its work for you

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

回答by Naved Ali

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

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

<property name="to">
    <array>
    <value>[email protected]</value>
    <value>[email protected]</value>
    </array>
</property>

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

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

helper.setTo(to);

回答by Surya Nair

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

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

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

回答by Sreenath S

You can try this, instead of

你可以试试这个,而不是

helper.setTo(to);

String multipleEmailIds = "[email protected], [email protected]"
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(multipleEmailIds ));

回答by Sree

just try like this.

就这样试试。

helper.setTo(InternetAddress.parse("[email protected],[email protected]"))