java—如何使用逗号分隔字符串从特定列的列表中获取值

wribegjk  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(388)

我想发送电子邮件给多个人,并希望从电子邮件收件人列表中获取这些电子邮件。

{
  "webId": 1001,
  "externalKey": null,
  "fromEmail": "test@gmail.com",
  "fromName": "TEST",
  "subject": "hello",
  "body": "This is a test mail",
  "status": 1,
  "sentDate": null,
  "sendAttempts": 1,
  "permanentFailure": false,
  "emailRecipientModel": [
  {
    "webId": 1101,
    "emailId": 1001,
    "emailAddress": "tets1@gmail.com",
    "status": "PENDING",
    "smtpStatusCode": 123,
    "recipientType": "TO"
  },
  {
    "webId": 1102,
    "emailId": 1001,
    "emailAddress": "test2@gmail.com",
    "status": "PENDING",
    "smtpStatusCode": 123,
    "recipientType": "TO"
  }
  ],
 }

现在我想得到两个电子邮件收件人的电子邮件地址。
在我的电子邮件模型课上。我有一个电子邮件收件人列表。
现在如何获得电子邮件地址或所有电子邮件收件人。
这是我发送电子邮件的功能。

MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setFrom(email.getFromEmail());
helper.setTo(email.getEmailRecipient().);      // need to add all the recipients here using comma separated list
helper.setSubject(email.getSubject());
helper.setText(email.getBody());

javaMailSender.send(message);
luaexgnf

luaexgnf1#

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

String recipients = email.getEmailRecipient().stream()
                                   .map(e -> e.getEmailAddress())
                                   .collect( Collectors.joining( "," ) );

helper.setTo(recipients);   
helper.setSubject(email.getSubject());
helper.setText(email.getBody());

javaMailSender.send(message);
kokeuurv

kokeuurv2#

假设你的方法 email.getEmailRecipient() 返回可以执行以下操作的字符串列表

helper.setTo(email.getEmailRecipient().stream().toArray(String[]::new));

helper.setTo(email.getEmailRecipient().toArray(new String[0]));

相关问题