0

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. Now my question is how do I use normal java variables like strings and ints inside the HTML code of an email.

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. I'm sorry if this was asked before but I was unable to find anything and I searched here on Stack Overflow and 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 :)

3
  • Dude. Change you profile picture. I tried reading this so many times. It is so distracting. Ha. Commented Jan 22, 2014 at 17:34
  • 1
    have you looked @velocity? Commented Jan 22, 2014 at 17:35
  • :DDD sorry Dut I can't when I try to change it I get distracted :D Commented Jan 23, 2014 at 12:56

3 Answers 3

1

Just concatenate the variable inside the htmlText string instead of at the end.

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 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(...)

Sign up to request clarification or add additional context in comments.

Comments

0

What about Java's standard String "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

1 Comment

This is a great solution as is java.text.MessageFormat docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html
0
<h2>
Variable Name: "+ Variable Value+" 
</h2> . 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.