Send Mail to multiple Recipients in java

后端 未结 12 725
孤城傲影
孤城傲影 2020-12-02 08:23

I want to send a message to multiple Recipients using following the method :

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

<

相关标签:
12条回答
  • 2020-12-02 08:44

    Try this way:

    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);
    
    0 讨论(0)
  • 2020-12-02 08:44

    Just use the method message.setRecipients with multiple addresses separated by commas:

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

    works fine with only one address too

    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("abc@abc.com"));
    
    0 讨论(0)
  • 2020-12-02 08:46

    Easy way to do

    String[] listofIDS={"ramasamygms@gmail.com","ramasamycse94@gmail.com"};
    
    for(String cc:listofIDS) {
        message.addRecipients(Message.RecipientType.CC,InternetAddress.parse(cc));
    }
    
    0 讨论(0)
  • 2020-12-02 08:49

    If you want to send as Cc using MimeMessageHelper

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

    Same you can use to add multiple recipient.

    0 讨论(0)
  • 2020-12-02 08:50
    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]; 
    
    0 讨论(0)
  • 2020-12-02 08:52

    If you invoke addRecipient multiple times it will add the given recipient to the list of recipients of the given time (TO, CC, BCC)

    For example:

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

    Will add the 3 addresses to CC


    If you wish to add all addresses at once you should use setRecipients or addRecipients and provide it with an array of addresses

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

    You can also use InternetAddress.parse to parse a list of addresses

    message.addRecipients(Message.RecipientType.CC, 
                          InternetAddress.parse("abc@abc.com,abc@def.com,ghi@abc.com"));
    
    0 讨论(0)
提交回复
热议问题