Java迭代器通过电子邮件发送字符串图像?

发布于 2025-01-07 07:03:31 字数 730 浏览 1 评论 0原文

我正在发送一封电子邮件,从数据库获取各种数量的图像。电子邮件将通过将字符串放在一起发送。有一个

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

电子邮件消息将 b

 String emailMessag = topemail + middleEmail + bottomEmail;

我正在使用它来迭代图像

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

,并且无论有多少图像都需要作为

示例

 <div><img src=\"someimg1.jpg"\" height=\"115\"></div>
 <div><img src=\"someimg2.jpg"\" height=\"115\"></div>
 <div><img src=\"someimg3.jpg"\" height=\"115\"></div>

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

example

 <div><img src=\"someimg1.jpg"\" height=\"115\"></div>
 <div><img src=\"someimg2.jpg"\" height=\"115\"></div>
 <div><img src=\"someimg3.jpg"\" height=\"115\"></div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄人书 2025-01-14 07:03:31

您不断用全新的值替换 middleEmail,而不是将新信息附加到前一个值的末尾。换句话说,您需要从各个部分逐渐构建一个字符串。在 Java 中,逐段构建字符串的典型方法是使用 StringBuilder

尝试这样的事情:

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
  sb.append("<div><img src=\"someimg1.jpg"\" height=\"115\"></div>");
}
String middleEmail = sb.toString();

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
  sb.append("<div><img src=\"someimg1.jpg"\" height=\"115\"></div>");
}
String middleEmail = sb.toString();
小兔几 2025-01-14 07:03:31

您实际上并没有说出您的问题是什么,但是...您正在迭代某些内容,但实际上并未从迭代器中读取值。

也许你想要这样的东西:

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

...如果你的迭代器返回文件名

(并查看 gteff 关于 StringBuilder 的答案 - 你也需要它)

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)

§对你不离不弃 2025-01-14 07:03:31

最好不要在 java 代码中构造 html 字符串,您可以使用任何模板引擎,例如 StringTemplates,它可以帮助您。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文