93

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

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 中。

4

12 回答 12

123

如果您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


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

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.parse来解析地址列表

message.addRecipients(Message.RecipientType.CC, 
                      InternetAddress.parse("abc@abc.com,abc@def.com,ghi@abc.com"));
于 2012-12-13T06:19:00.117 回答
30

大家好,此代码对我有用,请尝试使用此代码向多个收件人发送邮件

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);
于 2014-04-02T14:54:23.937 回答
17

只需使用带有逗号分隔的多个地址的方法 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"));
于 2014-11-13T18:58:57.563 回答
12

试试这个方法:

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);
于 2014-06-12T15:34:58.693 回答
11

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

if (cc.indexOf(',') > 0)
    message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));   
else
    message.setRecipient(Message.RecipientType.CC, new InternetAddress(cc));
于 2012-12-13T06:56:41.730 回答
7

Internet 电子邮件地址格式 ( RFC 822)

(,)逗号分隔的地址序列

javax.mail - 1.4.7parse( String[] )是不允许的。所以我们必须将逗号分隔的地址序列赋予InternetAddress对象。地址必须遵循 RFC822 语法。

String toAddress = "mail@mail.com,mail2@mail.com";
InternetAddress.parse( toAddress );

(;)分号分隔的地址序列 « 如果地址列表组带有分隔符,则为“;” 然后使用 split 方法转换为 String 数组以使用以下函数。

String[] addressList = { "mail@mail.com", "mail2@mail.com" };

String toGroup = "mail@mail.com;mail2@mail.com";
String[] addressList2 = toGroup.split(";");

setRecipients(message, addressList);
public static void setRecipients(Message message, Object addresslist) throws AddressException, MessagingException {
    if ( addresslist instanceof String ) { // CharSequence
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse( (String) addresslist  ));
    } else if ( addresslist instanceof String[] ) { // String[] « Array with collection of Strings/
        String[] toAddressList = (String[]) addresslist;
        InternetAddress[] mailAddress_TO = new InternetAddress[ toAddressList.length ];
        for (int i = 0; i < toAddressList.length; i++) {
            mailAddress_TO[i] = new InternetAddress( toAddressList[i] );
        }
        message.setRecipients(Message.RecipientType.TO, mailAddress_TO);
    }
}

完整示例:

public static Properties getMailProperties( boolean addExteraProps ) {
    Properties props = new Properties();
    props.put("mail.transport.protocol", MAIL_TRNSPORT_PROTOCOL);
    props.put("mail.smtp.host", MAIL_SERVER_NAME);
    props.put("mail.smtp.port", MAIL_PORT);

    // Sending Email to the GMail SMTP server requires authentication and SSL.
    props.put("mail.smtp.auth", true);
    if( ENCRYPTION_METHOD.equals("STARTTLS") ) {
        props.put("mail.smtp.starttls.enable", true);
        props.put("mail.smtp.socketFactory.port", SMTP_STARTTLS_PORT); // 587
    } else {
        props.put("mail.smtps.ssl.enable", true);
        props.put("mail.smtp.socketFactory.port", SMTP_SSL_PORT); // 465
    }
    props.put("mail.smtp.socketFactory", SOCKETFACTORY_CLASS);
    return props;
}

public static boolean sendMail(String subject, String contentType, String msg, Object recipients) throws Exception {

    Properties props = getMailProperties( false );
    Session mailSession = Session.getInstance(props, null);
    mailSession.setDebug(true);

    Message message = new MimeMessage( mailSession );
    message.setFrom( new InternetAddress( USER_NAME ) );

    setRecipients(message, recipients);

    message.setSubject( subject );

    String htmlData = "<h1>This is actual message embedded in HTML tags</h1>";
    message.setContent( htmlData, "text/html");

    Transport transport = mailSession.getTransport( MAIL_TRNSPORT_PROTOCOL );
    transport.connect(MAIL_SERVER_NAME, Integer.valueOf(MAIL_PORT), USER_NAME, PASSWORD);
    message.saveChanges(); // don't forget this

    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
}

使用 Appache SimpleEmail-commons-email-1.3.1

例子:email.addTo( addressList );

public static void sendSimpleMail() throws Exception {
    Email email = new SimpleEmail();
    email.setSmtpPort(587);

    DefaultAuthenticator defaultAuthenticator = new DefaultAuthenticator( USER_NAME, PASSWORD );

    email.setAuthenticator( defaultAuthenticator );
    email.setDebug(false);
    email.setHostName( MAIL_SERVER_NAME );
    email.setFrom( USER_NAME );
    email.setSubject("Hi");
    email.setMsg("This is a test mail ... :-)");

    //email.addTo( "mail@mail.com", "Yash" );
    String[] toAddressList = { "mail@mail.com", "mail2@mail.com" }
    email.addTo( addressList );

    email.setTLS(true);
    email.setStartTLSEnabled( true );
    email.send();
    System.out.println("Mail sent!");
}
于 2019-01-16T14:09:40.947 回答
6

所以...花了好几个月,但仍然...您可以使用“,”作为分隔符和

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

没关系。至少在 JavaMail 1.4.5 中

于 2013-07-03T16:05:29.123 回答
5

InternetAddress.Parse is going to be your friend! See the worked example below:

String to = "med@joe.com, maz@frank.com, jezz@jam.com";
String toCommaAndSpaces = "med@joe.com maz@frank.com, jezz@jam.com";
  1. Parse a comma-separated list of email addresses. Be strict. Require comma separated list.
  2. If strict is true, many (but not all) of the RFC822 syntax rules for emails are enforced.

    msg.setRecipients(Message.RecipientType.CC,
    InternetAddress.parse(to, true));
    
  3. Parse comma/space-separated list. Cut some slack. We allow spaces seperated list as well, plus invalid email formats.

    msg.setRecipients(Message.RecipientType.BCC,
    InternetAddress.parse(toCommaAndSpaces, false));
    
于 2014-08-14T15:51:26.953 回答
4
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]; 
于 2015-07-25T05:41:10.070 回答
2

简单的方法

String[] listofIDS={"ramasamygms@gmail.com","ramasamycse94@gmail.com"};

for(String cc:listofIDS) {
    message.addRecipients(Message.RecipientType.CC,InternetAddress.parse(cc));
}
于 2019-05-18T18:21:54.710 回答
1

如果您想使用 MimeMessageHelper 作为抄送发送

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

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

于 2014-11-03T08:04:52.960 回答
1

您可以使用以下方法的 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);
于 2012-12-13T06:59:03.743 回答