简体   繁体   English

HTML中的Java Javamail变量

[英]Java Javamail variables inside html

I'm using JavaMail for Java to send out emails to multiple people and so I need to use variables to make each email personal. 我正在使用JavaMail for Java向多个人发送电子邮件,因此我需要使用变量来使每封电子邮件成为个人。 Now my question is how do I use normal java variables like strings and ints inside the HTML code of an email. 现在,我的问题是如何在电子邮件的HTML代码中使用普通的Java变量(例如字符串和整数)。

Here is my code 这是我的代码

try {                     
  MimeMessage message = new MimeMessage(session);
  message.setFrom(new InternetAddress(from));
  message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));

  MimeMultipart multipart = new MimeMultipart("related");
  BodyPart messageBodyPart = new MimeBodyPart();
  message.setSubject(title + " has reached your desired price!");
  String htmlText = "<H1>Hello</H1> <H2> hi </H2> <img src=\"cid:image\">" + price_desired;

  messageBodyPart.setContent(htmlText, "text/html");
  multipart.addBodyPart(messageBodyPart);
  messageBodyPart = new MimeBodyPart();
  DataSource fds = new FileDataSource(folderPath + image_title);
  messageBodyPart.setDataHandler(new DataHandler(fds));
  messageBodyPart.setHeader("Content-ID", "<image>");
  multipart.addBodyPart(messageBodyPart);
  message.setContent(multipart);
  Transport transport = session.getTransport("smtp");
  transport.connect(host, from, pass);
  transport.sendMessage(message, message.getAllRecipients());
  transport.close();
  System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
  mex.printStackTrace();
}

As you can see I can add variables at the end of the email however how do I do it inside the HTML and how do I make the font size etc same for the variables as the normal code. 如您所见,我可以在电子邮件末尾添加变量,但是如何在HTML中添加变量以及如何使变量的字体大小等与普通代码相同。 I'm sorry if this was asked before but I was unable to find anything and I searched here on Stack Overflow and Google. 很抱歉,如果以前有人问过我,但是我找不到任何东西,我在Stack Overflow和Google上进行了搜索。


EDIT - For anyone wondering the answer is 编辑 -对于任何想知道答案的人

String htmlText = "<H1>Hello. The price desired is " + price_desired + "</H1>

when I initially tried something similar it didn't work but when I copied this code I could then easily change it around. 当我最初尝试类似的操作时,它不起作用,但是当我复制此代码时,便可以轻松地对其进行更改。

Thanks everyone :) 感谢大家 :)

Just concatenate the variable inside the htmlText string instead of at the end. 只需在htmlText字符串内而不是在结尾处连接变量。

String htmlText = "<H1>Hello. The price desired is " + price_desired + "</H1> <H2> hi </H2> <img src=\"cid:image\">";

If you are going to be concatenating several variables you may want to use StringBuilder instead of string concatenation. 如果要串联多个变量,则可能要使用StringBuilder而不是字符串串联。

StringBuilder htmlBuilder = new StringBuilder();
htmlBuilder.append("<H1>").append("Hello ").append(price_desired).append("</H1>");
htmlBuilder.append("<H2>").append(var2).append("</H2>");
messageBodyPart.setContent(htmlBuilder.toString(), "text/html");

You may also want to look into String.format(...) 您可能还需要研究String.format(...)

What about Java's standard String " formatting "? Java的标准字符串formatting ”如何?

String.format ( Locale.US, "<h1>Hi my friend %0$s</h1> <p>This is your pic: <img src=\"%1$s\"></p>", friendName, pictureURL );

Formatter syntax: here 格式化程序语法: 此处

<h2>
Variable Name: "+ Variable Value+" 
</h2> . 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM