Take the 2-minute tour ×

I am sending out an email that gets a variety number of images from a db. The email will be sent from putting Strings together. There is a

 String topEmail;
 String middleEmail: (This needs to be all the images)
 String bottomEmail;

The email message will b

 String emailMessag = topemail + middleEmail + bottomEmail;

I am using this to iterate the images

 while (scdIterator.hasNext()) {
  middleEmail= "<div><img src=\"someimg1.jpg"\" height=\"115\"></div>";

and need no matter how many image there are the outcome to be


 <div><img src=\"someimg2.jpg"\" height=\"115\"></div>
 <div><img src=\"someimg3.jpg"\" height=\"115\"></div>
share|improve this question

closed as not a real question by Ernest Friedman-Hill, Brian Roach, Paul Bellora, bmargulies, Graviton Feb 21 '12 at 2:13


What's "scdIterator" ? Where do the image file names come from? What exactly is your question? What part do you need help with? –  Ernest Friedman-Hill Feb 20 '12 at 23:22
You are iterating over something, but not actually reading the values from the iterator –  DNA Feb 20 '12 at 23:23
You're setting a String over and over to be the same thing, and then wonder why it's ... the same thing? –  Brian Roach Feb 20 '12 at 23:24
scditerator is information about the products that are in a shopping cart. I then get just the image names of the products using a MMObj. I want to get all the images and sent them in an email. The email message is sent as a string. –  user1186453 Feb 21 '12 at 0:42

3 Answers 3

You keep replacing middleEmail with a totally new value, instead of appending new information to the end of the previous value. In other words, you need to gradually build up a String from its parts. In Java, the typical way to build up a String piece-by-piece is to use a StringBuilder.

Try something like this:

StringBuilder sb = new StringBuilder();
while (scdIterator.hasNext()) {
  Object part = scdIterator.next();
  // modify this line to use the value we just got from the iterator somehow
String middleEmail = sb.toString();
share|improve this answer

You don't actually say what your problem is, but... you are iterating over something, but not actually reading the values from the iterator.

Perhaps you want something like:

while (scdIterator.hasNext()) {
  middleEmail= "<div><img src=\""+scdIterator.next()+"\" height=\"115\"></div>";

...if your iterator returns filenames

(and see gteff's answer about StringBuilder - you need that too)

share|improve this answer

Its always better not to construct html strings inside java code, you could use any templating engine like StringTemplates, which can help you.

share|improve this answer
