93

次の方法を使用して、複数の受信者にメッセージを送信したい:

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

また

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

しかし、混乱の 1 つは、2 番目の引数で、次のような複数のアドレスを渡す方法です。

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

CCに3つのアドレスを追加します


すべてのアドレスを一度に追加したい場合は、setRecipientsまたはaddRecipientsを使用してアドレスの配列を提供する必要があります

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 に答える
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 に答える
5

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. 電子メール アドレスのコンマ区切りリストを解析します。厳しくしてください。コンマ区切りのリストが必要です。
  2. strict が true の場合、メールのRFC822構文規則の多く (すべてではない) が適用されます。

    msg.setRecipients(Message.RecipientType.CC,
    InternetAddress.parse(to, true));
    
  3. コンマ/スペースで区切られたリストを解析します。たるみをカットします。スペースで区切られたリストと、無効な電子メール形式も許可されます。

    msg.setRecipients(Message.RecipientType.BCC,
    InternetAddress.parse(toCommaAndSpaces, false));
    
于 2014-08-14T15:51:26.953 に答える
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 を使用して Cc として送信する場合

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 に答える