Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reset recipient list from javax.mail.internet.MimeMessage

I have a scenario in which I need to send bulk emails to senders. I can send only 10 mails at a time. So I set all my mail contents in variable and then add only ten recipients at a time. Then after I send email, I again add ten next receipients to it.

private void addRecipients(Message pMessage, List pRecipients, Message.RecipientType pType, int pNum, int pOffset, int pBulkSize){
for (int i = 0; i < pRecipients.size(); i++) {
        int offset = pNum + i;
        if (pBulkSize != 0 && (offset < pOffset || offset >= pOffset + pBulkSize)) {
            continue;
        }
Address a;
Object r = pRecipients.get(i);
pMessage.addRecipient(pType, a);
}

But the problem is I am not able to remove the last ten receipients from the variable.

Transport.send(message);

After this line, my loop restarts and again the addRecipients method is called. But now the object

MimeMessage message = getIntialEmailConfiguration();

message has previous recipients as well.

I want to know how to reset that one property and remove existing recipients.

like image 458
Romil Gaurav Avatar asked Dec 08 '25 10:12

Romil Gaurav


1 Answers

Use the Message.setRecipients which will remove and apply multiple addresses.

Your code example won't compile because 'Address a' is never assigned a value.

like image 52
jmehrens Avatar answered Dec 09 '25 22:12

jmehrens