org.apache.commons.mail.HtmlEmail embedding/rendering the images again and again in every loop
posted 4 years ago
Following program/code is adding the images repetitively. IF I send emails to three users userA, userB and userB and it attached 1, 2, 3 images respectively. Seems email object is embedding/rendering the images again and again in every loop.
Welcome to the Ranch!
Could you please UseCodeTags next time? I've added them for you this time; see how easier it is to read your code.
The problem is that you are creating one single email object. You are adding (through the embed method on line 6) an image for each record. That's why you get an extra image for each user.
The solution is very simple. Move the creation of the email object from line 2 to before line 5, at the start of the loop body. This will create a new email object for each record. This email object is initially empty so only the single image is embedded. After the email has been sent and the loop body ends, the email object will be discarded along with all of its contents. The next record will start anew with an empty email object that you can fill from scratch again.