Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

org.apache.commons.mail.HtmlEmail embedding/rendering the images again and again in every loop

 
Rakesh Rawat
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic