将邮件发送给java中的多个收件人

我想发送消息给多个收件人使用以下方法::

message.addRecipient(Message.RecipientType.TO, String arg1); 

要么

 message.setRecipients(Message.RecipientType.TO,String arg1); 

但有一个混淆的是,在第二个论证中,如何传递多个地址,如:

 message.addRecipient(Message.RecipientType.CC, "abc@abc.com,abc@def.com,ghi@abc.com"); 

要么

 message.addRecipient(Message.RecipientType.CC, "abc@abc.com;abc@def.com;ghi@abc.com"); 

我也可以使用替代方法发送消息,但想知道上述方法的目的。 如果我不能使用它(直到现在我还没有得到任何回答上述要求)那么这个方法需要在邮件API。

如果您多次调用addRecipient ,它会将给定的收件人添加到给定时间(TO,CC,BCC)的收件人列表中

例如:

 message.addRecipient(Message.RecipientType.CC, InternetAddress.parse("abc@abc.com")); message.addRecipient(Message.RecipientType.CC, InternetAddress.parse("abc@def.com")); message.addRecipient(Message.RecipientType.CC, InternetAddress.parse("ghi@abc.com")); 

将3个地址添加到CC


如果您希望一次添加所有地址,则应使用setRecipientsaddRecipients并为其提供一组地址

 Address[] cc = new Address[] {InternetAddress.parse("abc@abc.com"), InternetAddress.parse("abc@def.com"), InternetAddress.parse("ghi@abc.com")}; message.addRecipients(Message.RecipientType.CC, cc); 

您也可以使用InternetAddress.parseparsing地址列表

 message.addRecipients(Message.RecipientType.CC, InternetAddress.parse("abc@abc.com,abc@def.com,ghi@abc.com")); 

嗨每一个这个代码工作对我来说,请尝试与此发送邮件给多个recepients

 private String recipient = "yamabs@gmail.com ,priya@gmail.com "; String[] recipientList = recipient.split(","); InternetAddress[] recipientAddress = new InternetAddress[recipientList.length]; int counter = 0; for (String recipient : recipientList) { recipientAddress[counter] = new InternetAddress(recipient.trim()); counter++; } message.setRecipients(Message.RecipientType.TO, recipientAddress); 

试试这个方法:

 message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("mail3@mail.com")); String address = "mail@mail.com,mail2@mail.com"; InternetAddress[] iAdressArray = InternetAddress.parse(address); message.setRecipients(Message.RecipientType.CC, iAdressArray); 

只需使用以逗号分隔的多个地址的方法message.setRecipients:

 message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("abc@abc.com,abc@def.com,ghi@abc.com")); message.setRecipients(Message.RecipientType.CC, InternetAddress.parse("abc@abc.com,abc@def.com,ghi@abc.com")); 

也只有一个地址工作正常

 message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("abc@abc.com")); 

您可以使用逗号分隔多个地址

 if (cc.indexOf(',') > 0) message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc)); else message.setRecipient(Message.RecipientType.CC, new InternetAddress(cc)); 

InternetAddress.Parse将成为你的朋友! 看下面的工作示例:

 String to = "med@joe.com, maz@frank.com, jezz@jam.com"; String toCommaAndSpaces = "med@joe.com maz@frank.com, jezz@jam.com"; 
  1. parsing逗号分隔的电子邮件地址列表。 严格。 需要用逗号分隔的列表。
  2. 如果严格,则许多(但不是全部)电子邮件的RFC822语法规则被强制执行。

     msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(to, true)); 
  3. parsing逗号/空格分隔的列表。 削减一些松懈。 我们还允许空格分隔列表,以及无效的电子邮件格式。

     msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(toCommaAndSpaces, false)); 

所以…花了好几个月的时间,但仍然…您可以通过使用','作为分隔符和发送电子邮件给多个收件人

 message.setRecipients(Message.RecipientType.CC, "abc@abc.com,abc@def.com,ghi@abc.com"); 

没问题。 至less在JavaMail 1.4.5中

 String[] mailAddressTo = new String[3]; mailAddressTo[0] = emailId_1; mailAddressTo[1] = emailId_2; mailAddressTo[2] = "xyz@gmail.com"; InternetAddress[] mailAddress_TO = new InternetAddress[mailAddressTo.length]; for (int i = 0; i < mailAddressTo.length; i++) { mailAddress_TO[i] = new InternetAddress(mailAddressTo[i]); } message.addRecipients(Message.RecipientType.TO, mailAddress_TO);ress_TO = new InternetAddress[mailAddressTo.length]; 

下面的方法可以使用n个接收者:

  String to[] = {"a@gmail.com"} //Mail id you want to send; InternetAddress[] address = new InternetAddress[to.length]; for(int i =0; i< to.length; i++) { address[i] = new InternetAddress(to[i]); } msg.setRecipients(Message.RecipientType.TO, address); 

如果你想使用MimeMessageHelper作为抄送

 List<String> emails= new ArrayList(); email.add("email1"); email.add("email2"); for (String string : emails) { message.addCc(string); } 

同样,您可以使用添加多个收件人。